如何验证整数输入?

时间:2015-10-10 02:29:00

标签: c++ validation integer

所以我只是因为我很无聊而为我哥哥玩的游戏制作这个小计算器程序。我在大学的最后一个学期参加了一个c ++课程(现在我参加了一个让我觉得有点混乱的java课程),我发现制作这些小程序很有趣。就像往常一样,我被带走了,必须是一个完美主义者,这真的很困扰我。

现在在这个游戏中,就像在现实生活中一样,数字被逗号分隔,显然更容易阅读。因此,这主要是我兄弟将数字输入计算器的方式。现在我可以告诉他在输入数字时不要加任何逗号,并在提示中说明不应该有任何逗号,但即使这样你也无法确定。即便如此,如果代码只是在每次都没有输入数字的情况下都不会搞砸,那将是最好的。

到目前为止我所得到的非常好。如果你只是输入字母,它会再次提示用户,如果你只在字母后面输入字母(不在中间,这会使我发现它混乱),那么它将忽略这些字母并正常工作。如果你输入逗号,它总是返回相同的东西(0和5),虽然这可能是我所投入的。我不知道逗号是作为截断点还是什么。这是获取整数的代码:

#include <iostream> 
using namespace std;

int main() {

int numberofbones, amountofxp, comparableDrag, comparableBaby, comparableDragNoAltar, comparableBigNoAltar, comparableBabyNoAltar;
double comparableBig;
char Gildedaltar, BoneSelection, replay;
bool bFail;

    do{
        cout << "How many bones do you have?: ";
        cin >> numberofbones;
        bFail = cin.fail();
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    } while (bFail == true);

    do{
        cout << "How much XP do you need?: ";
        cin >> amountofxp;
        bFail = cin.fail();
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    } while (bFail == true);

    cout << "Are you using a Gilded Altar? (Y/N) ";
prompt:
    cin >> Gildedaltar;

    comparableDrag = amountofxp / 252;
    comparableBig = amountofxp / 52.5;
    comparableBaby = amountofxp / 105;
    comparableDragNoAltar = amountofxp / 72;
    comparableBigNoAltar = amountofxp / 15;
    comparableBabyNoAltar = amountofxp / 30;

    if (Gildedaltar == 'y' || Gildedaltar == 'Y') {
        system("cls");
        cout << "What bones will you be using: " << endl;
        cout << "a) Dragon Bones " << endl;
        cout << "b) Big Bones " << endl;
        cout << "c) BabyDragon Bones " << endl;
        cin >> BoneSelection;

        if (BoneSelection == 'a' || BoneSelection == 'A') {
            cout << endl << "With Dragon Bones you need " << comparableDrag << " Bones" << endl;

            if (comparableDrag < numberofbones) {
                cout << "You have enough bones with " << numberofbones - comparableDrag << " left over, get to sacrafising!" << endl;
            }
            else {
                cout << "You will need " << comparableDrag - numberofbones << " more bones" << endl;
            }
        }
        if (BoneSelection == 'b' || BoneSelection == 'B') {
            cout << endl << "With Big Bones you need a total of " << comparableBig << " Bones" << endl;

            if (comparableBig < numberofbones) {
                cout << "You have enough bones with " << numberofbones - comparableBig << " Left over, get to sacrafising!" << endl;
            }
            else {
                cout << "You will need " << comparableBig - numberofbones << " more bones" << endl;
            }
        }
        if (BoneSelection == 'c' || BoneSelection == 'C') {
            cout << endl << "With BabyDragon Bones you will need " << comparableBaby << " Bones" << endl;

            if (comparableBaby < numberofbones) {
                cout << "You have enough bones with " << numberofbones - comparableBaby << " left over, get to sacrafising!" << endl;
            }
            else {
                cout << "You will need " << comparableBaby - numberofbones << " more bones" << endl;
            }
        }
    }
    else if (Gildedaltar == 'n' || Gildedaltar == 'N') {
        system("cls");
        cout << "What bones will you be using: " << endl;
        cout << "a) Dragon Bones " << endl;
        cout << "b) Big Bones " << endl;
        cout << "c) BabyDragon Bones " << endl;
        cin >> BoneSelection;

        if (BoneSelection == 'a' || BoneSelection == 'A') {
            cout << endl << "With Dragon Bones, you will need " << comparableDragNoAltar << " Bones " << endl;

            if (comparableDragNoAltar < numberofbones) {
                cout << "You have enough bones with " << numberofbones - comparableDragNoAltar << " left over, get to sacrafising!" << endl;
            }
            else {
                cout << "You will neeed " << comparableDragNoAltar - numberofbones << " More bones" << endl;
            }
        }
        if (BoneSelection == 'b' || BoneSelection == 'B') {
            cout << endl << "With Big Bones, you will need " << comparableBigNoAltar << " Bones" << endl;

            if (comparableBigNoAltar < numberofbones) {
                cout << "You have enough bones with " << numberofbones - comparableBigNoAltar << " Left over, get to sacrafising!" << endl;
            }
            else {
                cout << "You will need " << comparableBigNoAltar - numberofbones << " More bones" << endl;
            }
        }
        if (BoneSelection == 'c' || BoneSelection == 'c') {
            cout << endl << "With BabyDragon Bones, you will need " << comparableBabyNoAltar << " Bones" << endl;

            if (comparableBabyNoAltar < numberofbones) {
                cout << "You have enough bones with " << numberofbones - comparableBabyNoAltar << " Left over, get to sacrafising!" << endl;
            }
            else {
                cout << "You will need " << comparableBigNoAltar - numberofbones << " More Bones" << endl;

            }
        }
    }
    else {
        goto prompt;
    }

}

你可以忽略大部分代码,我知道它可能看起来很草率,并且可能更好地处理这里的大部分事情。我刚刚决定用无聊的方式做到这一点,并认为这对我来说是一个很好的教训。如果你知道一种帮助我的方法,我会非常乐意听到这个解决方案,如果有办法让程序完全忽略逗号,那会更好但是我不相信有办法做到这一点。

P.S。请不要对我过于技术化,我只修了一门课程:)提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

在尝试从中获取int之前,您只需要从输入字符串中删除逗号。更好的方法是从输入中删除任何非数字字符,您可以使用isdigit()。然后拨打atoi()就可以了。

string& remove_nondigit(string& s) {
    s.erase(remove_if(s.begin(), s.end(), [](const char& c) {
        return !isdigit(c);
    }), s.end());
    return s;
}

yourF() {
   string sBones;
   cin >> sBones;
   cout >> remove_nondigit(sBones);
   // you'll want to use atoi() on sBones
}