[英]C++ Character Test; How can I make program end properly once I enter a specific letter?
该实验室专注于测试字符。 我应该输入一个字符,如果它是大写字母,小写字母或其他类型,它将读取。 我的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循环后,程序/控制台仍坐在那里等待输入/输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.