C++ validating int variables-

bGIveNs33

Golden Member
Jul 10, 2002
1,543
0
71
So I'm writing a paper rock scissors program for class and I need to validate that the input is a number and not a letter, I get a infinite loop other wise. here is my function for it-

Code:
int getHumanChoice()
{


  int choice=0;
   
  while(true)
  {
    
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << "Please select your choice: " << endl;
    cout << "Enter '1' for Rock" << endl;
    cout << "Enter '2' for Paper" <<endl;
    cout << "Enter '3' for Scissors" <<endl;
    cout << "Enter '4' to Quit" << endl;
    cout << endl;
    
   
    cout << "What is your selection?: ";
    cin >> choice;
    cin.ignore(1000,10) ;

    if (choice == 1 || choice == 2 || choice == 3 || choice == 4) break;
    
    cout << "Invalid entry, please enter a number 1-4 " << endl;
  }

thanks!
 

bGIveNs33

Golden Member
Jul 10, 2002
1,543
0
71
but I've defined choice as an int variable...?

I think I need to use the isdigit function but I can't seem to get it to work.

I guess I could use the char variable but if the users types in 14 or 24, it will read it as 1 and 2.
 

Scooby Doo

Golden Member
Sep 1, 2006
1,034
18
81
If you're leaving it as int's shouldn't it be:

Code:
if (choice == 49 || choice == 50 || choice == 51 || choice == 52) break;

49 being ASCII for "1"....
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,609
4,530
75
...which can be more clearly done as:

Code:
if (choice == '1' || choice == '2' || choice == '3' || choice == '4') break;

In the worst case, some casting (int) or (char) might be required.

P.S. Give me fscanf("&#37;c", &choice) over an ambiguous cin >> any day.
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
but I've defined choice as an int variable...?
...

That's the beauty of C++: all the misleading types and conversions of a dynamically typed language, and yet, when you least expect it, C++ surprises you with byte-stream behavior.

Ken_g6 said:
P.S. Give me fscanf("&#37;c", &choice) over an ambiguous cin >> any day.
I prefer the fgets() family myself, followed by some good old-fashioned painful parsing via strtok() and then, if needed, some checks for >= '0' and <= '9', so help me. Even despite the reliance on the newline.
 
Last edited:

bGIveNs33

Golden Member
Jul 10, 2002
1,543
0
71
thx for the input. I'm not allowed to use fscan unfortunately...

I will look into what you said scooby.

I ended up taking the input as a string, and then using this streamstring to convert it back into an int. It seems to be working.

Thx for everyone with my last two problems... a lot of great info.
 

Cogman

Lifer
Sep 19, 2000
10,284
138
106
check out cin.fail. If a letter (anything other then 0-9) is entered in, and cin tries to put that into an int. It will go into a fail state.
 

Merad

Platinum Member
May 31, 2010
2,586
19
81
If you're leaving it as int's shouldn't it be:

Code:
if (choice == 49 || choice == 50 || choice == 51 || choice == 52) break;
49 being ASCII for "1"....

No. The stream object does the conversion for you, and you check for invalid input by seeing if cin has gone into a failed state.

Code:
#include <iostream>
#include <limits>

using namespace std;

int main()
{
  int input = 0;

  do
  {
    cout << "Enter a number: ";
    cin >> input;

    if (!cin)  // or use cin.fail()
    {
      cout << "Input is not a number!" << endl;
      cin.clear();
      cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');            
    }
    else
    {
      cout << "You entered " << input << "!" << endl;
    }
  } while (input != -1);

  return 0;
}