问题
How can I make sure the user inputs numerical values only instead of alphanumeric or any other character? Also what to look for to insert error message for incorrent input?
#include<stdio.h>
int main()
{
int a, b, c;
printf("Enter first number to add\n");
scanf("%d",&a);
printf("Enter second number to add\n");
scanf("%d",&b);
c = a + b;
printf("Sum of entered numbers = %d\n",c);
return 0;
}
回答1:
If you really want to deal with user input that could be hostile use a separate function for getting the number.
Allows
- leading spaces : " 123"
- trailing spaces : "123 "
- leading zeros : "0000000000000000000000000000000000123"
- Rescans nicely after error input.
Catches the following errors
- No input : ""
- Extra text after the number: "123 abc"
- Text before the number : "abc 123"
- Split number : "123 456"
- Overflow/underflow : "12345678901234567890"
- other : "--123"
Re-prompts on invalid input.
#include <errno.h>
#include <stdio.h>
#include <stddef.h>
int GetInteger(const char *prompt, int *i) {
int Invalid = 0;
int EndIndex;
char buffer[100];
do {
if (Invalid)
fputs("Invalid input, try again.\n", stdout);
Invalid = 1;
fputs(prompt, stdout);
if (NULL == fgets(buffer, sizeof(buffer), stdin))
return 1;
errno = 0;
} while ((1 != sscanf(buffer, "%d %n", i, &EndIndex)) || buffer[EndIndex] || errno);
return 0;
}
int main() {
int a, b, c;
if (GetInteger("Enter first number to add\n", &a)) {
; // End of file or I/O error (rare)
}
if (GetInteger("Enter second number to add\n", &b)) {
; // End of file or I/O error (rare)
}
c = a + b;
printf("Sum of entered numbers = %d\n",c);
return 0;
}
BTW, You should not do printf("Enter first number to add\n")
. Use fputs()
instead. Consider what would happen if the string had a %
in it.
回答2:
It's better to avoid using scanf
at all. Use fgets
to get entire line, and then use sscanf
to extract the information you need. Check the return value of sscanf
to make sure the input is as expected.
回答3:
Please read the manual page for scanf. You need to check the return value. SHould return 1 if able to match a number.
回答4:
EDIT- I needed to add the getchar()
in the while loop because the unread non-numeric item was left in the input queue causing the program to go in an infinite loop , further I have also added more compact form of while
loop for the same thing , both will have the same effect .
You can check the return value of scanf
it returns 1 on successful match with the format specifier and 0 if not . You can do it like this for your program :
#include <stdio.h>
#include <stdbool.h>
int main(void)
{
int num;
bool status;//_bool may also be used and stdbool.h won't be needed for that
status = scanf ("%d",&num);
if (status != 1 )
{
getchar();// needed to add it to eat up the unread item left in input queue.
while (status != 1)
{
printf (" sorry ! incorrect input :ERROR: try again");
status = scanf("%d", &num);
}
}
/*....do something...*/
return 0;
}
More compact form of while loop :-
while (scanf("%d",&num) != 1)
{
getchar();
printf (" sorry ! incorrect input :ERROR: try again\n");
}
Besides, you should always use meaningful names for variables like num1
and num2
for instance instead of a
and b
.
来源:https://stackoverflow.com/questions/18584302/accept-numerical-values-only-for-input-using-scanf