有没有办法在您运行程序时将char显示的符号转换为字符串时的97 char等于“ a”的方式?
#include <iostream>
#include <ctime>
#include <windows.h>
#include <random>
#include <string>
using namespace std;
int main()
{
int max = 122, min = 97;
int range = max - min + 1;
srand (time(0));
char letter = rand() % range + min;
cout << letter << endl;
string letter2;
cin >> letter2;
if(letter2 == letter)
{
cout << letter2 << endl;
}
}
这是我尝试的代码,但是没有用。提前致谢。
首先,您可以将std :: string类型的对象用作字母。例如
std::string letter( 1, rand() % range + min );
其次,您可以使用一个字符数组作为示例
char letter[2] = { char( rand() % range + min ), '\0' };
或者您可以原样离开,但情况可能看起来像
if ( letter2.size() == 1 && letter2[0] == letter )
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句