I'm writing a program that takes an integer input via 'cin'.
I want to make the program to make sure the number is not a character or string (which would cause the program to crash)
So far.. this is the code I have..
Code:int getUserTempInput() { int x = 0; while (x != 1) { cout << "\nEnter a temperature to convert: \n"; cin >> userTemp; if (cin.good() == true) { x = 1; }else{ cout << userTemp << " is not a valid integer. Please try again.\n\n" << endl; } } //end while loop return userTemp; } //end getUserInput
When i execute the program, and try to enter something like:
ssdfsdf sdfsdfs fsw
the program goes into an infinate loop. I looked on google and read something about using 'boolean cin.good()' and tried it in the code, but it doesn't seem to help.
What function could I use to insure that the correct input is taken in?
Thanks.