gpt4 book ai didi

C++字符测试;输入特定字母后如何使程序正常结束?

转载 作者:行者123 更新时间:2023-11-28 06:22:14 25 4
gpt4 key购买 nike

实验室专注于测试角色。我应该输入一个字符,如果它是大写字母、小写字母或其他类型,它将读取。我的 if 语句是正确的。我的提示和阅读我输入内容的语句也可以。问题是当我输入应该使程序结束的 q 或 Q 时,程序仍然读取我输入的 q 或 Q。程序不应该在我输入后结束并且不阅读那些字母吗?

 int main()
{
char input = ' ';

//set up loop such that all tasks below is done as long as input
// is not a q or a Q (that is, quit)
while (toupper(input) != 'Q' || tolower(input) != 'q')
{
cout << "Enter any character: ";
cin.get(input);
cin.ignore();
cout << "The character you entered is: " << input << endl;

if (isalpha(input))
cout << "That's an alphabetic character.\n";
if (isdigit(input))
cout << "That's a numeric digit.\n";
if (islower(input))
cout << "The letter you entered is lowercase.\n";
if (isupper(input))
cout << "The letter you entered is uppercase.\n";
if (isspace(input))
cout << "That's a whitespace character.\n";

//add code to test for alphanumeric
if (isalnum(input))
cout << "The character you entered is alphanumeric.\n";

//add code for a punctuation
if (ispunct(input))
cout << "The character you entered is a punctuation.\n";
}

system("PAUSE");
return 0;
}

最佳答案

删除 system("PAUSE"); 并重试。

使用“q”或“Q”退出 while 循环后,程序/控制台仍坐在那里等待输入/输出。

关于C++字符测试;输入特定字母后如何使程序正常结束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29135302/

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