How to avoid a character value in integer or floating variables

Click For Summary
SUMMARY

This discussion addresses the issue of validating user input for age and gender in C programming. The user, Sharif, encounters infinite loops when non-numeric characters are entered for age and invalid characters for gender. Solutions include using the isdigit function from ctype.h to check for numeric input and handling input errors by checking the return value of scanf. Implementing these strategies will prevent infinite loops and ensure valid input is processed correctly.

PREREQUISITES
  • Understanding of C programming syntax and structure
  • Familiarity with the scanf function for input handling
  • Knowledge of the ctype.h library and its functions
  • Basic error handling techniques in C
NEXT STEPS
  • Learn how to use the isdigit function for input validation
  • Research effective input handling techniques in C to prevent buffer overflow
  • Explore the use of return values from scanf to manage input errors
  • Investigate alternative input methods, such as reading strings and parsing them
USEFUL FOR

C programmers, software developers, and anyone interested in improving input validation techniques in their applications.

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
 

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 8 ·
Replies
8
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
11
Views
3K