1

我正在努力让输入验证适用于我的游戏。

#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() 但我无法让它完全正常工作。

谢谢,约翰

4

3 回答 3

2

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

#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;
}

我的回答是基于一个相关问题:How to determine if a string is a number with C++?

于 2013-01-04T22:44:23.530 回答
1

Yo 可用于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 again\n\n";
                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;
}
于 2013-01-04T23:03:39.453 回答
1

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

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

于 2013-01-04T23:10:59.703 回答