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
Click For Summary

Homework Help Overview

The discussion revolves around handling user input errors in C programming, specifically focusing on ensuring that user input falls within a specified numeric range. The original poster describes an issue where entering a non-numeric character leads to an infinite loop in their code.

Discussion Character

  • Exploratory, Assumption checking, Problem interpretation

Approaches and Questions Raised

  • Participants suggest various methods to handle non-numeric input, including checking ASCII values, parsing input, and alternative input functions like getchar(). There is a focus on understanding how to validate user input effectively.

Discussion Status

Participants are exploring different strategies to address the infinite loop issue caused by invalid input. Suggestions have been made to modify the input handling approach, but no consensus has been reached on a specific solution yet.

Contextual Notes

The original poster's code is constrained by the use of scanf, which does not handle non-numeric input gracefully. There is an underlying assumption that the input should strictly be numeric, which is being questioned in the discussion.

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.
 

Similar threads

  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
11K
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
14
Views
4K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
Replies
9
Views
2K