Advertisement

OK, stupid question time...

Started by October 23, 2002 11:22 PM
13 comments, last by Trakkur 22 years ago
OK...this is bugging me...

Here is the code snippet...

int GetGuess()
{
cout << endl << "What is your choice? : ";
int guess;
cin >> guess;

if(isdigit(guess))
cout << endl << "Good!" << endl;
else
cout << endl << "Bad!" << endl;

return guess;
}

It doesn''t work for some reason...

If you enter a number (Example-9) it still says Bad!

And if you enter a letter the program crashes.

I don''t understand what''s wrong here... but just to cover my bases, here is the entire code... in case I''m wrong somewhere else. Keep in mind that the user can enter any number of digits, they are told if they are out of range (1-50).

// Pick-a-number.cpp
//

#include <iostream> // required for ''cout''
#include <conio.h> // required for ''getch''
#include <time.h>
#include <stdlib.h>
#include <ctype.h>

using namespace std; // activates the standard namespace

int SetNumber();
int GetGuess();
void DisplayOutput(int guess, int number);

int main()
{
int number = SetNumber();
cout << endl << "Choose a number from 1 to 50. Choose 0 to stop playing." << endl;
cout << "Keep in mind that you only have five guesses, use them wisely." << endl;
int guess;
int tries = 5;

do
{
guess = GetGuess();
--tries;
if (guess == 0)
{
cout << endl << "OK, quitting now." << endl;
}
else if (guess == number)
cout << endl << "Whoa! You got it!" << endl;
else if(tries == 0)
{
cout << endl << "SORRY! You only get five tries and that was wrong also! The number was - " << number << "." << endl;
cout << "Too bad...you lose!" << endl;
}
else if ((guess > 50) || (guess < 1))
cout << endl << "I said a number from 1 to 50, try again." << endl;
else if(guess>number)
cout << "You guessed too high, try a lower number." << endl;
else if(guess cout << "You guessed too low, try a higher number." << endl;
if(tries == 1)
cout << endl << "Choose carefully! You only have one guess left!" << endl;
} while ((guess!=number) && (guess!=0) && (tries!=0));

cout << endl << "Press any key to exit the game." << endl;
cout << "Thank you for playing!";
getch();

return 0;
}

int SetNumber()
{
srand( (unsigned)time( NULL ) );
int number = ((rand() % 50) +1);

return number;
}

int GetGuess()
{
cout << endl << "What is your choice? : ";
int guess;
cin >> guess;

if(isdigit(guess))
cout << endl << "Good!" << endl;
else
cout << endl << "Bad!" << endl;
// Check to make sure guess is an integer and not a letter or some such nonsense

return guess;
}

void DisplayOutput(int guess, int number)
{
cout << "You chose " << guess << " as your number. It was in fact, " << number << "." << endl;
}


Help?!?

Thanks! This is making me nuts

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.
Ok, I''m new to C++, so I don''t know of any neat little commands you could use, but this is how I would do it.

First, I''m going to assume that you''re going to let the user input more than one character at a time. If not, you can modify my code to make it more simple.

Ok, basically, first you''re going to want to use cin.getline to read user input into an array of chars[i.e. a string]. Here is the code for that:

const int MAXINPUT = 20;
char input[MAXINPUT]; // used to read in user input

cout << "Enter a number.\n: ";
cin.getline(input, (MAXINPUT-1));

if (isInt(input))
cout<<"Yes.\n";
else
cout<<"No.\n";

Ok, now the user can input a string of up to 20 characters, and our function isInt will tell you if it''s a valid integer or not.
Before I give you the code, I''ll tell you what the idea is.

The function reads in the input char array one character at a time, until it either reaches the string terminator or it finds a char which is not an integer. How do you tell? First, convert the char into a integer. However, if you enter the char ''0'', and then convert that into an int, what do you get? The answer is 48... why? Look at an ascii table. The CHARACTER ''0'' is actually the ascii code 48. The character ''1'' is ascii code 49... etc. So how we use this is to subtract 48 from our integer (converted from char), which will make 0 = 0, 1 = 1, 2 = 2, etc.

Next what we do, is check if our integer is less than 0 or more than 9. If it is, it means the user inputted a char which was not an integer, and therefore is out of range of our conversion.
This might be hard to grasp, or I might be overexplaining. Anyway, here is the code. Let me know if you have any problems.

bool isInt(char* const input) // takes an array of chars
{
int testChar;
for(int i=0; input != ''\0''; i++)
{
testChar = int(input); <br> testChar -= 48;<br> if (testChar < 0 || testChar > 9)<br> return false;<br> }<br> return true;<br>}<br><br> </i>
Advertisement
Trakkur,
The isdigit() function checks if a character is a numeric digit. It does not check if a number is a digit. In other words it tells you if the character ''5'' is a digit. You are reading guess into an int as a number, not a character, and then passing that to isdigit().
You could read the input into a string and then go through it, character by character, and check if each character is a digit.

Did anyone read Beer Hunter''s post?
quote: Original post by Beer Hunter
if (cin >> num) {
  // good
} else {
  // garbage
  cin.clear();
}


Using this code you can know for sure whether you got a number or not. If the expression (cin >> num) evaluates to true, you got a number. If the user enters ''12'', num will contain the number 12. If the user enters ''abcd'', num will contain garbage, or whatever you initialized it with. If the user enters ''45abcd12'', num will contain the number 45.
The solution you choose will depend on how much validation you want to do; whether you want to check if all of the characters entered are digits, or if you just want to know if the input started with some digits.

Shawn
When using the Windows calculator program, always remember to clear any values from memory before exiting to prevent burn-in.
Shawn,

I did see Beer Hunter''s reply, and was wondering (As stupid as it sounds) what is "num" in that code? Is taht the comparison to my random number being generated?

I''m still working on this, but I''ll get it soon, that last few suggestions really helped.

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.
Beowulf,
May I suggest the following small change.
Instead of subtracting 48 from testChar, why not compare it against the characters ''0'' and ''9''.
For example:
if (testChar < ''0'' || testChar > ''9'')
That way you don''t have to remember the ASCII value of ''0''.

Trakkur,
"num" in the code I quoted from Beer Hunter would be "guess" in yours.

operator>> is overloaded for many different data types. If you do this:

int guess;
cin >> guess;

the overload for int is called. This method will read input until a non-number is encountered, and store the resulting number in your variable. If the input stream begins with a non-number, your variable will remain unchaged, containing whatever it did before. If you didn''t initialize it, it may contain garbage.

If you just want to be sure that some number was read, in your GetGuess() function check if (cin >> guess) evaluates to true. If it does, you successfully read a number into guess.
If you need to ensure that the entire input contains only numbers, do something more like what Beowulf suggests.

Shawn
When using the Windows calculator program, always remember to clear any values from memory before exiting to prevent burn-in.

This topic is closed to new replies.

Advertisement