OK, stupid question time...
How do I verify that a user actually input a number rather then some other garbage in C++?
Take this for example...
int GetGuess()
{
cout << endl << "What is your choice? : ";
int guess;
cin >> guess;
// Check to make sure guess is an integer and not a letter or some such nonsense
return guess;
}
How do I make sure guess is truly an integer and not a character, space or other nonsense?
I can''t seem to figure this out..an ideas?
Thanks!
Trakkur
==================
Never try to argue with an idiot, they''ll only pull you down to their level...and beat you with experience.
Never try to argue with an idiot, they'll only pull you down to their level...and beat you with experience.
i believe there are functions for checking that sort of thing. in fact.. i''m positive there are functions for that sort of thing. isascii() or something similar. i haven''t quite memorized all the useful functions yet and my reference books are loaned out at the moment. but yea.. you can call a function to do that checking for yea.
"The human mind is limited only by the bounds which we impose upon ourselves." -iNfuSeD
if (guess !((>0) && (<10))
GUESS-AGAIN();
could this work?
It''''s nice to be important, but it''''s more important to be nice.
GUESS-AGAIN();
could this work?
It''''s nice to be important, but it''''s more important to be nice.
Don't we all still remember and miss the day we plotted our first pixel?
i''d read the input in as a string - that way you won''t wreck the input stream
string myGuess;
cin >> myGuess
you can then check the contents of the string
hope this helps
string myGuess;
cin >> myGuess
you can then check the contents of the string
hope this helps
Yah... I''m kind of a hybrid C/C++ programmer, so I''m not up on all the wonderfulness of string objects and streams and whatnot. But I''d be looping through the string and making sure every character was >''0'' and <''9'' (or the other way round, can''t remember which comes first in ascii). If you find something out of this range then you have "nonsense" and you can then: cout << "You think you can fool me?!?! ME?!?!?! HAH!!! I am better than that!!!" << endl; or something equally satisfying.
hope this guides you in the right direction
http://www.deakin.edu.au/~bradleyj/unsanity/
hope this guides you in the right direction
http://www.deakin.edu.au/~bradleyj/unsanity/
http://www.deakin.edu.au/~bradleyj/unsanity/
This is what I did for something...
bool justNumbers( char *strIn ){ bool answer = true; int len = strlen(strIn); for (int index = 0; index < len; index++) { if (int(strIn[index]) < 48 || int(strIn[index]) > 57) answer = false; } return answer;}// somewhere in yer programchar inputBuffer [1024];cin >> inputBuffer;if (!justNumbers){ cout << "That sir, is not what I asked you to enter" << endl;}
OK...looks like there is a function called isdigit, but I can''t get it to work...
I try this...
cout << endl << "What is your choice? : ";
int guess;
cin >> guess;
int result = isdigit(guess);
if(result==1)
cout << endl << "GOOD!";
else if(result==0)
cout << endl << "BAD!";
Result should equal 0 if guess is a non-digit and 1 if guess IS a digit.
But it works opposite...enter a number and you always see bad, enter something other then a number and the game crashes!
Am I doing something wrong?
Trakkur
==========
Never try to argue with an idiot, they''ll only pull you down to their level...and beat you with experience.
I try this...
cout << endl << "What is your choice? : ";
int guess;
cin >> guess;
int result = isdigit(guess);
if(result==1)
cout << endl << "GOOD!";
else if(result==0)
cout << endl << "BAD!";
Result should equal 0 if guess is a non-digit and 1 if guess IS a digit.
But it works opposite...enter a number and you always see bad, enter something other then a number and the game crashes!
Am I doing something wrong?
Trakkur
==========
Never try to argue with an idiot, they''ll only pull you down to their level...and beat you with experience.
Never try to argue with an idiot, they'll only pull you down to their level...and beat you with experience.
October 24, 2002 07:02 AM
It should be :
int result = isdigit(guess);
if(result)
{
cout << endl << "GOOD!";
}
else
{
cout << endl << "BAD!";
}
isdigit() returns ZERO for false and any NON-ZERO number for true..
Don''t assume it to be 1
int result = isdigit(guess);
if(result)
{
cout << endl << "GOOD!";
}
else
{
cout << endl << "BAD!";
}
isdigit() returns ZERO for false and any NON-ZERO number for true..
Don''t assume it to be 1
#include <iostream>#include <cctype>using namespace std;int GetGuess(){ int Guess; cout << "What is your choice? :"; cin >> Guess; if(isdigit(Guess)) cout << "Great!"; else cout << "Sorry, Wrong Invalid Selection!";}
The above anonymous post was mine. Ignore it. It wasn''t really clear.
isdigit(x) will return a NON-ZERO value if x is a number
and a ZERO value if x is NOT a number
if(x) will only run if x is a NON-ZERO value.
Hope this helps!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement