c++ - How do I validate integer input? -


so making little calculator program game brother plays because bored. took c++ course last semester in college (now taking java course find bit more confusing) , found fun make these little programs. usual i'm getting carried away , must perfectionist , bothering me.

now in game, , in real life, numbers seperated commas make easier read. because of this, that's how numbers going inputted calculator brother. tell him not put in commas when typing in number , put in prompt there should not commas can't sure. so, best if code doesn't mess every time that's not number put in.

what i've got far pretty good. if put in letters prompt user again , if put letters in after numbers (not inbetween, messes found) ignore letters , work properly. if put in commas though, returns same thing (0 , 5) although i'm putting in. have no idea if comma acting cut off point or what. here's code getting 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 have?: ";         cin >> numberofbones;         bfail = cin.fail();         cin.clear();         cin.ignore(numeric_limits<streamsize>::max(), '\n');     } while (bfail == true);      do{         cout << "how xp need?: ";         cin >> amountofxp;         bfail = cin.fail();         cin.clear();         cin.ignore(numeric_limits<streamsize>::max(), '\n');     } while (bfail == true);      cout << "are using 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 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 need " << comparabledrag << " bones" << endl;              if (comparabledrag < numberofbones) {                 cout << "you have enough bones " << numberofbones - comparabledrag << " left over, sacrafising!" << endl;             }             else {                 cout << "you need " << comparabledrag - numberofbones << " more bones" << endl;             }         }         if (boneselection == 'b' || boneselection == 'b') {             cout << endl << "with big bones need total of " << comparablebig << " bones" << endl;              if (comparablebig < numberofbones) {                 cout << "you have enough bones " << numberofbones - comparablebig << " left over, sacrafising!" << endl;             }             else {                 cout << "you need " << comparablebig - numberofbones << " more bones" << endl;             }         }         if (boneselection == 'c' || boneselection == 'c') {             cout << endl << "with babydragon bones need " << comparablebaby << " bones" << endl;              if (comparablebaby < numberofbones) {                 cout << "you have enough bones " << numberofbones - comparablebaby << " left over, sacrafising!" << endl;             }             else {                 cout << "you need " << comparablebaby - numberofbones << " more bones" << endl;             }         }     }     else if (gildedaltar == 'n' || gildedaltar == 'n') {         system("cls");         cout << "what bones 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, need " << comparabledragnoaltar << " bones " << endl;              if (comparabledragnoaltar < numberofbones) {                 cout << "you have enough bones " << numberofbones - comparabledragnoaltar << " left over, sacrafising!" << endl;             }             else {                 cout << "you neeed " << comparabledragnoaltar - numberofbones << " more bones" << endl;             }         }         if (boneselection == 'b' || boneselection == 'b') {             cout << endl << "with big bones, need " << comparablebignoaltar << " bones" << endl;              if (comparablebignoaltar < numberofbones) {                 cout << "you have enough bones " << numberofbones - comparablebignoaltar << " left over, sacrafising!" << endl;             }             else {                 cout << "you need " << comparablebignoaltar - numberofbones << " more bones" << endl;             }         }         if (boneselection == 'c' || boneselection == 'c') {             cout << endl << "with babydragon bones, need " << comparablebabynoaltar << " bones" << endl;              if (comparablebabynoaltar < numberofbones) {                 cout << "you have enough bones " << numberofbones - comparablebabynoaltar << " left over, sacrafising!" << endl;             }             else {                 cout << "you need " << comparablebignoaltar - numberofbones << " more bones" << endl;              }         }     }     else {         goto prompt;     } 

}

you can ignore of code, know looks sloppy , there's betters ways of handling of things in here. decided make out of boredom , figured lesson learn me. if know of way me more happy hear solution, if there's way program ignore commas completly better alas don't believe there's way that.

p.s. please don't technical me, i've taken 1 course on stuff :) in advance help.

you need remove commas input string before trying int it. better remove non-digit chars input, can isdigit(). 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 use atoi() on sbones } 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -