How to avoid a character value in integer or floating variables

  • Thread starter sh4rif
  • Start date
  • #1
4
0

Main Question or Discussion Point

Hello everyone

Thanks for viewing this topic for me.... my question is when I tried to check user inputs a valid age it works only if a number is entered. If by mistake user inputs a character value (e.g. any character a to z or any symbol) this loop runs infinite

How can I check if a user inputs a number other then numerical number display error and take him/her back to same question again?

below is my code of age check
////min_age is 18 and max_age is 65
do
{
printf("\tEmployee Age (%2d to %2d): \t", min_age, max_age);
scanf("%d", &age);
}while(age < min_age || age >max_age);//employment age between 18 to 65

Same issue with below code too, but this is the opposite of numeric ....I want user only puts a character value no numeric value should be accepted.... plus one other issue is if user puts a wrong character twice (like 'kk' instead of 'k' ) still error

do
{
getchar();
printf("\tEmployee Gender (M/F): \t");
scanf("%c", &gender);
}while(gender != 'M' && gender != 'm' && gender != 'F' && gender != 'f');


I would really appreciate any comment or help
Thanks
Sharif
 

Answers and Replies

  • #2
595
0
You could scanf() into a char string and then parse it separately. Of course nothing prevents your user from typing a zillion chars and blowing the string buffer out of the water. Such is the fun of C...
 
  • #3
33,477
5,167
The isdigit function (protyped in ctype.h) can be used to check whether a character is a numerical digit. It returns a nonzero value if its argument (of type int) is a digit in the range 0 ... 9, and zero otherwise.
 
  • #4
Hurkyl
Staff Emeritus
Science Advisor
Gold Member
14,916
19
Straight from a man page for scanf:

These functions return the number of input items assigned. This can be
fewer than provided for, or even zero, in the event of a matching fail-
ure. Zero indicates that, although there was input available, no conver-
sions were assigned; typically this is due to an invalid input character,
such as an alphabetic character for a `%d' conversion. The value EOF is
returned if an input failure occurs before any conversion such as an end-
of-file occurs. If an error or end-of-file occurs after conversion has
begun, the number of conversions which were successfully completed is
returned.​

Many standard library functions return values. If you check the return value, you can detect if it operated correctly.



Now, your problem is that scanf stops when it sees the alphabetic character and doesn't consume the input. So the next time through the loop, the alphabetic character is still there waiting for the program to do something with it. So scanf looks, and sees the alphabetic character is still there and still does nothing, and so on....
 
  • #5
4
0
thanks alot to all of you.. i really aperciate your effort and time... come back if i need more help......

thanks once again
sharif
 

Related Threads on How to avoid a character value in integer or floating variables

  • Last Post
Replies
3
Views
2K
  • Last Post
Replies
16
Views
7K
  • Last Post
Replies
0
Views
3K
Replies
4
Views
2K
  • Last Post
Replies
21
Views
3K
Replies
9
Views
6K
Replies
2
Views
2K
Top