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,360
4,066
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,283
134
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;
}
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |