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.
 
I multiplied the values first without the error limit. Got 19.38. rounded it off to 2 significant figures since the given data has 2 significant figures. So = 19. For error I used the above formula. It comes out about 1.48. Now my question is. Should I write the answer as 19±1.5 (rounding 1.48 to 2 significant figures) OR should I write it as 19±1. So in short, should the error have same number of significant figures as the mean value or should it have the same number of decimal places as...
Thread 'Calculation of Tensile Forces in Piston-Type Water-Lifting Devices at Elevated Locations'
Figure 1 Overall Structure Diagram Figure 2: Top view of the piston when it is cylindrical A circular opening is created at a height of 5 meters above the water surface. Inside this opening is a sleeve-type piston with a cross-sectional area of 1 square meter. The piston is pulled to the right at a constant speed. The pulling force is(Figure 2): F = ρshg = 1000 × 1 × 5 × 10 = 50,000 N. Figure 3: Modifying the structure to incorporate a fixed internal piston When I modify the piston...
Thread 'A cylinder connected to a hanging mass'
Let's declare that for the cylinder, mass = M = 10 kg Radius = R = 4 m For the wall and the floor, Friction coeff = ##\mu## = 0.5 For the hanging mass, mass = m = 11 kg First, we divide the force according to their respective plane (x and y thing, correct me if I'm wrong) and according to which, cylinder or the hanging mass, they're working on. Force on the hanging mass $$mg - T = ma$$ Force(Cylinder) on y $$N_f + f_w - Mg = 0$$ Force(Cylinder) on x $$T + f_f - N_w = Ma$$ There's also...
Back
Top