Click to See Complete Forum and Search --> : Beginner C++ question


pdeopura
May 27th, 2008, 01:24 AM
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!

S_M_A
May 27th, 2008, 01:29 AM
Translate the string to the equivalent number. Using atoi is probably the easiest way.

ch0co
May 27th, 2008, 07:43 AM
use something like

(0 < 1) && (1 < 5);


good with if statement.

Lindley
May 27th, 2008, 08:31 AM
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.