我需要这个字符显示为数字,但我不断收到笑脸、心形和其他 ASCII 符号。这部分是我认为问题所在:
s = prefix + ch + '.';
完整代码如下:
int main()
{
int levels = 2;
string prefix = "Recursion:";
sections(prefix, levels);
system("pause");
return 0;
}
void sections(string prefix, int levels)
{
if (levels == 0)
{
cout << prefix << endl;
}
else
{
for (char ch = 1; ch <= 9; ch++)
{
string s;
s = prefix + ch + '.';
sections(s, levels - 1);
}
}
}
最佳答案
您正在为您的字符使用 int 值,而不是字符,因此您将获得字符集中恰好具有这些代码的任何字符。在字符周围使用 '
来获取特定字符的字符代码:
for (char ch = '1'; ch <= '9'; ch++)
sections(prefix + ch + '.', levels - 1);
请注意,这取决于数字字符在您的字符集中都是连续的且按升序排列(实现已定义),但我能想到的每个字符集都是这种情况...
关于c++ - 如何将 char 字符显示为 int,而不是 ASCII 符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32877087/