gpt4 book ai didi

c++ - 我可以将字符符号转换为字符串吗

转载 作者:行者123 更新时间:2023-11-30 03:46:52 25 4
gpt4 key购买 nike

有没有办法把运行程序时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 )

关于c++ - 我可以将字符符号转换为字符串吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33927127/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com