如何使用 c++ 验证用户输入

How do I validate user input with c++?

本文关键字:用户 输入 验证 c++ 何使用      更新时间:2023-10-16

嘿,这真的让我很紧张。

我正在尝试在循环中验证用户输入。

我需要用户输入介于 0 和 60 之间。我可以毫无问题地验证它,但我希望它做的是重新询问上一个问题,如果输入不正确,你知道吗?就像重复循环,如果这有意义的话

int main()
{
    //Constants 
    const int MAXROUNDS = 4;
    const int NUMARCHERS = 3;
    //Variables
    int archerNum;
    int roundNum;
    double score;
    double total;
        //Start of outer loop, this loop displays each Archer 
        for (archerNum = 1; archerNum <= NUMARCHERS; archerNum++)
        {
            total = 0; //This clears the total for the archer 
            //Start of second loop, this loop displays each round
            for (roundNum = 1; roundNum <= MAXROUNDS; roundNum++)
            {
                cout << "Enter Round " << roundNum << " score for Archer "
                    << archerNum << ": ";
                cin >> score;
                if (score < 0 | score > 60)
                {
                    cout << "ERROR! Number must be between 0 and 60!";
                }
            }
            total = score + score + score + score; //This calculates the total score for the tournament 
            cout << "nThe total score for archer " << archerNum << " is: "
                << total << "nn";
        }
    return 0;
}

这是我的代码^

现在我尝试了很多事情。我翻阅了我的教科书,我用谷歌搜索了所有内容,但我似乎找不到答案。

我试过将错误消息放在一个 do-while 循环中我使用了 if-else 语句我用过 while 循环我想我已经使用了每种不同类型的循环,但我似乎仍然无法弄清楚,我变得非常沮丧。

提前致谢

以下是数字输入和错误消息的简单版本:

//#include "pch.h" if using Visual Studio 2017
#include <iostream>
using namespace std;
int main(){
  cout << "Please enter a number between 0 and 60." << endl;
  int input;
  cin >> input;
  while(input < 0 || input > 60 || cin.fail()){ //Using cin.fail() here in case the user enters a letter or word
    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(), 'n');
    cerr << "Number must be between 0 and 60. Please try again." << endl;
    cin >> input;
  }
  return 0;
}`

或者,您可以使用goto语句,我相信本网站上的许多其他人会告诉您,不建议这样做,因为它会导致臭名昭著的"意大利面条代码"。

只需在验证 If 语句中添加roundNum -= 1;即可。这将使计数器减少 1,并重新询问上一个问题

//Start of second loop, this loop displays each round
            for (roundNum = 1; roundNum <= MAXROUNDS; roundNum++)
            {
                std::cout << "Enter Round " << roundNum << " score for Archer "
                    << archerNum << ": ";
                std::cin >> score;
                if (score < 0 || score > 60)
                {
                    std::cout << "ERROR! Number must be between 0 and 60!"<<endl;
                    roundNum -= 1; //or roundNum--
                }
            }

尝试:

bool inputError;
do {
    inputError = false;
    std::out << "Enter Round "
             << roundNum 
             << " score for Archer "
             << archerNum << ": ";
    if (std::cin >> score) {
        if (score < 0 || score > 60)
        {
            std::cout << "ERROR! Number must be between 0 and 60!";
            inputError = true;
        }
    }
    else {
        std::cout << "ERROR! Your input must be a number";
        inputError = true;
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max());
    }
} while(inputError == true);