我可以将char符号转换为字符串

时间:2015-11-25 21:58:42

标签: c++ string char

有没有办法转换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;
}
}

这是我尝试的代码,但它没有用。 提前谢谢。

1 个答案:

答案 0 :(得分:2)

首先,您可以使用std :: string类型的对象作为字母。例如

if ( letter2.size() == 1 && letter2[0] == letter )

其次,您可以使用字符数组,例如

{{1}}

或者您可以原样离开但条件可能看起来像

{{1}}
相关问题