如何检查输入是否是字母?

时间:2017-10-23 02:33:03

标签: c++

当用户输入字母而不是数字时,我正在尝试显示“无效选项”。我尝试使用isalpha()函数,但我得到一个显示0 Invalid option!try again:的无限循环。输入字母时会显示输出中的0。当我实际输入数字0时,将显示消息并退出循环。

    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    #include <cctype>
    using namespace std;

    int main() {
        // Vaules do not change
        const int minNum = 1;
        const int maxNum = 100;
        int userInput;

        // Changes every second
        srand(time(0));

        // Random number is stored
        const int answer = minNum + (rand() % maxNum);
        cout << answer << endl; // For testing purposes

        cout << "Guess a number 1-100: ";
        cin >> userInput;
        cout << endl;

        if(userInput == answer) {
          cout << "Correct!\n\n";
        }
        while(userInput != answer) {
          if(userInput < 1 || userInput > 100 || isalpha(userInput))  {
            cout << userInput << " Invalid option!\ntry again: ";
            cin >> userInput;
            cout << endl;
          }
          else if(userInput < answer) {
            cout << userInput << " is too low!\ntry again: ";
            cin >> userInput;
            cout << endl;
          }
          else if(userInput > answer) {
            cout << userInput << " is too high!\ntry again: ";
            cin >> userInput;
            cout << endl;
          }
        }
        cout << userInput << " is correct!\n\n";
        return 0;
    }

1 个答案:

答案 0 :(得分:1)

当您需要根据某些逻辑以不同方式处理用户输入时,最佳选择是:

  1. 阅读文字行。找出没有更多输入时该怎么做。
  2. 使用自定义逻辑处理每行文本。
  3. 在您的情况下,您可以使用:

    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    #include <cctype>
    
    using namespace std;
    
    int main() {
    
       // Vaules do not change
       const int minNum = 1;
       const int maxNum = 100;
       int userInput;
    
       // Changes every second
       srand(time(0));
    
       // Random number is stored
       const int answer = minNum + (rand() % maxNum);
       cout << answer << endl; // For testing purposes
    
       std::string line;
       cout << "Guess a number 1-100: ";
       while ( getline(std:::cout, line ) )
       {
          // Deal with empty lines.
          if ( line.size() == 0 )
          {
             continue;
          }
    
          // If the first character is a letter ...
          if(isalpha(line[0]))  {
             cout << line << "\n Invalid option!\ntry again: ";
             continue;
          }
    
          // Extract the number from the line using a stringstream.
          // If there is a problem extracting the number ...
          std::istringstream str(line);
          if ( !(str >> userInput ) )
          {
             cout << line << "\n Invalid option!\ntry again: ";
             continue;
          }
    
          cout << endl;
    
          // Check the user input against the random answer.
          if(userInput == answer) {
             cout << "Correct!\n\n";
          }
          else if(userInput < 1 || userInput > 100 )  {
             cout << userInput << " Invalid option!\ntry again: ";
          }
          else if(userInput < answer) {
             cout << userInput << " is too low!\ntry again: ";
          }
          else if(userInput > answer) {
             cout << userInput << " is too high!\ntry again: ";
          }
    
          cout << "Guess a number 1-100: ";
       }
       cout << userInput << " is correct!\n\n";
       return 0;
    }