一个游戏的输入验证

C++ Input validation for a game

本文关键字:输入 验证 游戏 一个      更新时间:2023-10-16

我正在努力为我的游戏获得输入验证。

#include <iostream>
#include <ctime>
using namespace std;
int main()
{
    int iGumballs;
    int iUserguess;
    int iGuesses = 0;
    while (true)
    {
        system("CLS");
        cin.clear();
        iGuesses = 0;
    srand(static_cast<unsigned int>(time(0)));
    iGumballs = rand()%1000+1;
    cout << "How many gumballs are in the bumball jar? You guess! 1-1000" << endl;
    do
    {
        cout << "Enter your guess: ";

    cin >> iUserguess;

    if(iUserguess > iGumballs)
    {
        cout << "Too high!" << endl << endl;
    }
    if(iUserguess < iGumballs)
    {
        cout << "Too low!" << endl << endl;
    }
    iGuesses ++;
    }
    while(iUserguess > iGumballs || iUserguess < iGumballs);
    cout << "You guessed the right amout of gumballs" << endl << endl;
    cout << "You took " << iGuesses << " guesses" << endl << endl;
    system ("pause");
    }
    return 0;
}

我想让程序显示

Your Guess: Sorry, incorrect input - try again

当用户输入小于1且大于1000的数字时,以及某种类型的验证,以确保输入的是数字而不是字母或符号。我试过cin.fail(),但是我不能使它完全工作。

谢谢,约翰。

你将需要一些测试来看看是否是一个数字,试试这个:

#include <iostream>
#include <string>
#include <ctime>
#include <boost/lexical_cast.hpp> //dependency that can be optional
using namespace std;
bool is_number(const std::string& s)
{
    std::string::const_iterator it = s.begin();
    while (it != s.end() && std::isdigit(*it)) ++it;
    return !s.empty() && it == s.end();
}
int main()
{
    int iGumballs;
    std::string iUserguessStr;
    int iUserguess;
    int iGuesses = 0;
    while (true)
    {
        system("CLS");
        cin.clear();
        iGuesses = 0;
    srand(static_cast<unsigned int>(time(0)));
    iGumballs = rand()%1000+1;
    cout << "How many gumballs are in the bumball jar? You guess! 1-1000" << endl;
    do
    {
        cout << "Enter your guess: ";

    cin >> iUserguessStr;
    if(is_number(iUserguessStr))
        iUserguess = boost::lexical_cast<int>(iUserguessStr); //you can make your own or maybe use lexical cast to transform a string into integer
    else
        continue; //put some fancy message here warning the user

    if(iUserguess > iGumballs)
    {
        cout << "Too high!" << endl << endl;
    }
    if(iUserguess < iGumballs)
    {
        cout << "Too low!" << endl << endl;
    }
    iGuesses ++;
    }
    while(iUserguess > iGumballs || iUserguess < iGumballs);
    cout << "You guessed the right amout of gumballs" << endl << endl;
    cout << "You took " << iGuesses << " guesses" << endl << endl;
    system ("pause");
    }
    return 0;
}

我的答案是基于一个相关的问题:如何确定一个字符串是否是一个数字与c++ ?

您可以使用if(cin)来检查输入流的状态,因为operator>>将返回传递给它的输入流,您可以使用if(cin>>iUserguess)

如果cin处于失败状态-可能是因为用户输入了一个非数字-表达式if(cin>>iUserguess)将计算为false。

如果用户输入一个非数字,你需要调用cin.clear()来清除流状态和cin.ignore()来丢弃输入,然后再尝试读取一个数字。

所以在你的例子中,它可以改成这样:

#include <iostream>
#include <ctime>
#include <limits>
using namespace std;
int main()
{
    int iGumballs;
    int iUserguess;
    int iGuesses = 0;
    while (true)
    {
        system("CLS");
        iGuesses = 0;
        srand(static_cast<unsigned int>(time(0)));
        iGumballs = rand()%1000+1;
        cout << "How many gumballs are in the bumball jar? You guess! 1-1000" << endl;
        do
        {
            cout << "Enter your guess: ";
            if(cin >> iUserguess)
            {
                iGuesses ++;
                if(iUserguess > iGumballs)
                {
                    cout << "Too high!" << endl << endl;
                    continue;
                }
                if(iUserguess < iGumballs)
                {
                    cout << "Too low!" << endl << endl;
                    continue;
                }
            }
            else
            {
                cout<<"incorrect input - try againnn";
                cin.clear();
                cin.ignore(numeric_limits<streamsize>::max(),'n');
                continue;
            }
        }
        while(iUserguess > iGumballs || iUserguess < iGumballs);
        cout << "You guessed the right amout of gumballs" << endl << endl;
        cout << "You took " << iGuesses << " guesses" << endl << endl;
        system ("pause");
    }
    return 0;
}

要验证字符,可以使用try-catch结构。

-首先读入一个字符串并尝试进行类型转换,然后使用try-catch处理错误。
-然后使用条件来确保输入在范围内。
-如果输入无效,你可以写一个错误信息来显示