C ++游戏的输入验证

时间:2013-01-04 22:26:09

标签: c++ validation

我正在努力为我的游戏进行输入验证。

#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(),但是我无法完成它。

谢谢, 约翰

3 个答案:

答案 0 :(得分: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++?

答案 1 :(得分:1)

Yo可以使用if(cin)来检查输入流的状态,因为运算符&gt;&gt;将返回传递给它的输入流,您可以使用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;
}

答案 2 :(得分:1)

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

- 首先读取字符串并尝试Typecasting it并使用try-catch处理错误 - 然后使用条件确保输入在范围内 - 如果输入无效,您可以编写错误消息进行显示。