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

In summary, The conversation discusses a problem with creating a loop in C that checks for a user's input within a certain numeric range. The issue arises when a character is entered instead of a number, causing the loop to become infinite. The solution is to check the ASCII code of the input and only allow alphanumeric inputs, using functions like getchar() instead of scanf.
  • #1
Juntao
45
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
  • #2
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.
 
  • #3
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).
 
  • #4
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.
 

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

1. What is simplistic C code and why is it important?

Simplistic C code refers to code written in the C programming language that is easy to understand and maintain. It is important because it allows for better readability and reduces the chances of errors or bugs in the code.

2. What are some tips for writing simplistic C code?

Some tips for writing simplistic C code include using descriptive variable names, avoiding unnecessary complexity, using comments to explain the code, and following coding conventions.

3. How can I improve my existing C code to make it more simplistic?

To improve existing C code, you can review and refactor your code to remove any unnecessary complexity, use more descriptive variable names, and add comments to explain the code.

4. Are there any tools or resources available for simplifying C code?

Yes, there are several tools and resources available such as code refactoring tools, coding style checkers, and online communities where you can seek feedback and advice on how to improve your C code.

5. How can I test the effectiveness of my simplistic C code?

You can test the effectiveness of your simplistic C code by using debugging tools to identify and fix any errors or bugs, running test cases to ensure the code functions as intended, and seeking feedback from other developers.

Similar threads

  • Programming and Computer Science
Replies
4
Views
766
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Programming and Computer Science
Replies
7
Views
1K
  • Introductory Physics Homework Help
Replies
11
Views
795
  • Engineering and Comp Sci Homework Help
Replies
3
Views
707
  • Programming and Computer Science
Replies
14
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
927
  • Programming and Computer Science
Replies
9
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
Back
Top