How to avoid a character value in integer or floating variables

Click For Summary
The discussion revolves around handling user input validation in C programming, specifically for age and gender inputs. The user, Sharif, encounters issues with infinite loops when non-numeric characters are entered for age and when invalid characters are input for gender. Suggestions include using the `isdigit` function from `ctype.h` to check if the input is a digit and handling input with `scanf` more effectively. It is noted that `scanf` does not consume invalid input, leading to repeated failures in the loop. To resolve these issues, it is recommended to read input into a character string and then parse it, ensuring that the input is validated correctly before proceeding. Additionally, checking the return value of `scanf` can help identify input errors.
sh4rif
Messages
4
Reaction score
0
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
 
Technology news on Phys.org
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...
 
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.
 
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...
 
thanks a lot to all of you.. i really aperciate your effort and time... come back if i need more help...

thanks once again
sharif
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

Replies
7
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 23 ·
Replies
23
Views
2K
Replies
1
Views
8K
  • · Replies 3 ·
Replies
3
Views
1K
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
11
Views
3K