Handling User Input Errors in C: Looping and Checking for Numeric Range

  • Thread starter Thread starter Juntao
  • Start date Start date
  • Tags Tags
    C code Code
AI Thread Summary
To handle user input errors in C, particularly when expecting numeric input, it's essential to validate the input to prevent infinite loops caused by invalid characters. The current implementation using `scanf` can lead to issues if a non-numeric character is entered, as it does not clear the input buffer. A better approach involves checking the input as a string and parsing it to ensure it is a valid number within the desired range. Utilizing functions like `getchar()` can simplify the process by allowing for more controlled input handling. Implementing these changes will ensure that the program prompts the user correctly when invalid input is detected.
Juntao
Messages
45
Reaction score
0
Hi, I am trying to write a loop that will check a users input to be between a certain numeric range in C. The thing is, I expect the person to enter a number, not a character.

When the person enters a character, the loop becomes infinite. Thats the problem.
If you look at example source code below:

#include <stdlib.h>
int main()
{
int number=0;
printf("input a number\n");
scanf("%d", &number);
while (number <1 || number >5)
{
printf("incorrect number!\nput in new number");
scanf("%d", &number);

printf("\n your number is %d",number);
}
printf("\n your number is: %d",number);
return 0;
}

it works fine for numbers, but if I enter letter 'a' for example, the loop becomes infinite.

My question is, how do I go about the code such that a character that is inputted instead of a number prints error to screen and prompts user over again?

I thought a statement like this while (number <1 || number >5) would do the trick, but it does not.
 
Physics news on Phys.org
check the ascii code of the user input. if it isn't in the range of values assigned to numbers, then print an error message.
 
The problem is with the scanf argument. You should instead allow the user to enter an alphanumeric input and add a few lines to parse it and check if it is indeed a number (and not a spurious input).
 
You should try to avoid using scanf entirely... in this case you're interested in a single digit, so getchar() would be simple to use.
 
Kindly see the attached pdf. My attempt to solve it, is in it. I'm wondering if my solution is right. My idea is this: At any point of time, the ball may be assumed to be at an incline which is at an angle of θ(kindly see both the pics in the pdf file). The value of θ will continuously change and so will the value of friction. I'm not able to figure out, why my solution is wrong, if it is wrong .
Thread 'Voltmeter readings for this circuit with switches'
TL;DR Summary: I would like to know the voltmeter readings on the two resistors separately in the picture in the following cases , When one of the keys is closed When both of them are opened (Knowing that the battery has negligible internal resistance) My thoughts for the first case , one of them must be 12 volt while the other is 0 The second case we'll I think both voltmeter readings should be 12 volt since they are both parallel to the battery and they involve the key within what the...
Back
Top