如何使用C ++验证用户输入?

时间:2019-01-25 01:24:42

标签: c++

嘿,这真的让我感到不安。

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

我需要用户输入介于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 << "\n\n";
        }

    return 0;
}

这是我的代码^

现在我已经尝试了很多东西。我浏览了我的教科书,并用Google搜索了所有内容,但似乎找不到答案。

我尝试将错误消息放入do-while循环中 我用过if-else语句 我用过while循环 我想我实际上已经使用了每种不同类型的循环,但我仍然似乎无法弄清楚,这让我感到非常沮丧。

预先感谢

3 个答案:

答案 0 :(得分:0)

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

//#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语句,因为我确信此站点上的许多其他语句都会告诉您,不建议这样做,因为这会导致臭名昭著的“意大利面条代码”。

答案 1 :(得分:-1)

只需在验证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--
                }
            }

答案 2 :(得分:-2)

尝试:

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);
相关问题