|
-
May 27th, 2008, 01:24 AM
#1
Beginner C++ question
My program prompts the user for a number between 1 & 5.
How can I check if the user entered a number between the given range, and not a random character?
I am using fgets(...) to read the user's answer which returns a string if I am correct.
Thanks in advance!
-
May 27th, 2008, 01:29 AM
#2
Re: Beginner C++ question
Translate the string to the equivalent number. Using atoi is probably the easiest way.
-
May 27th, 2008, 07:43 AM
#3
Re: Beginner C++ question
use something like
Code:
(0 < 1) && (1 < 5);
good with if statement.
-
May 27th, 2008, 08:31 AM
#4
Re: Beginner C++ question
Since it's a single digit, you could just read in a char and compare it to '1' and '5' (the ASCII characters, not the numbers). However, to protect against multiple characters being entered, you might want to read an entire string and then just compare the first character.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|