游戏的 C++ 输入验证

标签 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(),但无法正常工作。

谢谢, 约翰

最佳答案

你需要一些测试来判断是不是一个数字,试试这个:

#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++?

关于游戏的 C++ 输入验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14166016/

相关文章:

c++ - 如何在 C++ 中删除换行符

c++ - 为什么可以将整数分配给字符串变量?

c++ - 使用 g++ 的 TCP 服务器客户端

c# - 在 Mac 上从 C++ 代码调用时 `coreclr_create_delegate` 不起作用

php - Symfony2 验证器想成为 ValidatorInterface 的一个实例

c# - 用于检查我的模型属性中的重复项的自定义验证属性未触发

ios - iOS 中的电子邮件验证问题?

c++ - Quad 无法正确生成?

javascript - 如果我需要在主函数启动之前对每个数值进行数学检查,那么验证表单的最佳做法是什么?

javascript - 在javascript中更改控件背景颜色 - 但是如何在提交表单之前通过验证时重置颜色