C++ 如何检查输入是否为字母?

C++ 如何检查输入是否为字母?,c++,C++,当用户输入字母而不是数字时,我试图显示“无效选项”。我尝试使用isalpha()函数,但得到一个无限循环,显示0无效选项!请重试:。输入字母时,将显示输出中的0。当我实际键入数字0时,将显示消息并退出循环 #include <iostream> #include <cstdlib> #include <ctime> #include <cctype> using namespace std; int

当用户输入字母而不是数字时,我试图显示“无效选项”。我尝试使用
isalpha()
函数,但得到一个无限循环,显示
0无效选项!请重试:
。输入字母时,将显示输出中的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;
    }
#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
//Vaules不会改变
常数int minNum=1;
常量int maxNum=100;
int用户输入;
//每秒钟都在变化
srand(时间(0));
//存储随机数
常量int answer=minNum+(rand()%maxNum);

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

  • 阅读一行行的文本,找出当没有更多输入时该做什么
  • 使用自定义逻辑处理每行文本
  • 在您的情况下,您可以使用:

    #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;
    }
    
    #包括
    #包括
    #包括
    #包括
    使用名称空间std;
    int main(){
    //Vaules不会改变
    常数int minNum=1;
    常量int maxNum=100;
    int用户输入;
    //每秒钟都在变化
    srand(时间(0));
    //存储随机数
    常量int answer=minNum+(rand()%maxNum);
    
    cout提示:由于你正在获取一个
    int
    值,你永远不会得到任何alpha值。可能是@tadman的重复,认为类型转换会起作用,或者是将它提取到一个char-instead中。我不明白行在做什么,它是在读取用户输入的内容吗?@lbonillaii,它读取一行输入。有关详细信息,请参阅。@R Sahu不确定为什么要获取行没有被识别为函数抛出xcode并用make文件编译它。@lbonillaii,我不知道。我从未使用过xcode。在SO上搜索xcode和getline会发现。我希望其中一个可以帮助您解决问题。我只是要重写它,因为某些原因,即使使用正确的库,它也无法识别它为函数包括