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

时间:2015-03-19 00:42:34

标签: c++

实验室专注于测试角色。我应该输入一个字符,如果是大写字母或其他类型的大写字母,它将会读取。我的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;
    }

1 个答案:

答案 0 :(得分:1)

删除system("PAUSE");然后重试。

用&#39; q&#39;退出while循环后或者&#39; Q&#39;程序/控制台仍然坐在那里等待输入/输出。