How to avoid a character value in integer or floating variables

In summary, the code checks the employee's age between 18 and 65 and prints out the results. If the user inputs a non-numerical value, the program loops indefinitely. If the user inputs a numeric value, but a wrong character, the program prints an error message and returns to the same question.
  • #1
sh4rif
4
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
  • #2
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
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
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
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
 

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

1. Can I convert a character value to an integer or floating variable?

Yes, you can use the appropriate conversion function, such as int() or float(), to convert a character value to an integer or floating variable.

2. How can I prevent a character value from being accidentally assigned to an integer or floating variable?

You can use conditional statements or input validation techniques to check the data type before assigning a value to an integer or floating variable.

3. Will converting a character value to an integer or floating variable affect the original character value?

No, the original character value will remain unchanged. The conversion function will create a new variable with the converted data type.

4. Can I mix character values with integer or floating variables in mathematical operations?

No, mathematical operations can only be performed on variables of the same data type. You will need to convert the character value to an integer or floating variable before performing the operation.

5. How can I check if a variable is a character value or an integer/floating variable?

You can use the type() function to check the data type of a variable. If it returns str, then the variable is a character value. If it returns int or float, then the variable is an integer or floating variable, respectively.

Similar threads

  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
4
Views
775
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
8
Views
932
  • Programming and Computer Science
Replies
1
Views
7K
Replies
11
Views
3K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
Back
Top