How to avoid a character value in integer or floating variables

Click For Summary

Discussion Overview

The discussion centers around handling user input validation in C programming, specifically for ensuring that inputs for age and gender are within specified constraints. Participants explore methods to prevent invalid character inputs and address issues related to infinite loops when invalid data is entered.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes a scenario where inputting a non-numeric character causes an infinite loop in their age validation code.
  • Another participant suggests using a character string with parsing to handle input, while noting potential buffer overflow issues.
  • A different participant mentions the use of the isdigit function from ctype.h to check if a character is a numerical digit.
  • One reply references the behavior of scanf, explaining how it handles invalid input and the importance of checking return values to detect input errors.
  • Participants discuss the issue of scanf not consuming invalid input characters, leading to repeated failures in input validation.

Areas of Agreement / Disagreement

Participants express various approaches to input validation, with no consensus on a single solution. Multiple methods are proposed, and issues regarding buffer management and input handling remain unresolved.

Contextual Notes

Limitations include the potential for buffer overflow when using character strings and the need for careful handling of invalid inputs to avoid infinite loops.

Who May Find This Useful

This discussion may be useful for programmers working with C who are interested in input validation techniques and handling user input errors effectively.

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