简体   繁体   中英

How do I validate integer input?

So I was just making this little calculator program for a game my brother plays just because I was bored. I took a c++ course last semester in college (now taking a java course which I find a bit more confusing) and I just found it fun to make these little programs. Well like usual I'm getting carried away and must be a perfectionist and this is really bothering me.

Now in this game, and like in real life, numbers are seperated by commas to obviously make it easier to read. Because of this, that's mostly how numbers are going to be inputted into the calculator by my brother. Now I could just tell him to not put in any commas when typing in the number and put it in the prompt that there should not be any commas but even then you can't be sure. Even so, it would be best if the code just doesn't mess up every time something that's not a number is put in.

What I've got so far is pretty good. if you put in just letters it will prompt the user again and if you put letters in AFTER the numbers only (not inbetween, that messes it up i found) then it will ignore those letters and work properly. If you put in commas though, it always returns the same thing (0 and 5) although that could be what I'm putting in. I have no idea if the comma is acting as a cut off point or what. Here's the code for getting the integers:

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

}

You can ignore most of the code, I know it probably looks really sloppy and there's probably much betters ways of handling most of the things in here. I just decided to make this out of boredom and figured it could be a good lesson to learn from for me. if you know of a way to help me I would be more than happy to hear the solution, if there's a way to get the program to ignore the commas completly that would be even better but alas I don't believe there's a way to do that.

PS please don't get too technical with me, I've only taken one course on this stuff :) Thanks in advance for the help.

You just need to remove the commas from the input string before trying to get an int from it. Better would be to remove any non-digit chars from the input, which you can do with isdigit() . Then call atoi() on it.

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM