I have a program that has this code :
#include<stdio.h>
main(){
int input;
char g;
do{
printf("Choose a numeric value");
print开发者_Go百科f(">");
scanf("\n%c",&input);
g=input-'0';
}while((g>=-16 && g<=-1)||(g>=10 && g<=42)||(g>=43 && g<=79));
}
It basically uses ASCII manipulation to allow the program to accept numbers only .. '0' is given the value 48 by default...the ASCII value - 48 gives a ranges of numbers above (in the while statement)
Anyway, whenever a user inputs numbers AND alphabets, such as :
abr39293afakvmienb23
The program ignores : a,b,r .. But takes '3' as the first input. For a b and r, the code under the do loop repeats. So for the above example, I get :
Choose a numeric value
>Choose a numeric value>
Choose a numeric value
>3
Is there a way I can stop this ??? I tried using \n%c to scan the character and account for whitespace, but that didnt work :(
Please help
thank you very much !
What I understand from your question is that when user enters abr3
, the output you get is
Choose a numeric value
Choose a numeric value>
Choose a numeric value
3
Now you do not what these "excessive" Choose a numeric value
to appear. If user has input abr3, its just take 3 from it and exit. Something like this.
$ Choose a numeric value> abr3
$
Now to do that you have to change format specifier in your scanf()
statement. Try your program with
input = 0 ;
scanf( "%[0-9]" , &input );
It tells the scanf to accept all the contents within braces i.e. 0-9. Any other character outside this range is ignored. If user inputs abr3
, a is not in the range, therefore input will remain equal to zero. Moreover, Choose a numeric value
for a,b and r will not appear.
If user input is a number in the range of 0-9, it will be accepted and put into input.
You will have to add a check for input==0.
Hope it helps.
If the problem is that scanf
ignores whitespace, try getchar
instead of scanf
.
Why not just move the printf("Choose a numeric value");
outside (just before) the do
/while
loop? If it's inside the loop it will be executed at every leg of the loop, if it's outside it will be executed only once before the loop.
Try to use example below, and don't forget to free memory.
#include <stdio.h>
int main()
{
int size = 100;
char *line;
int number;
while (1) {
puts("Enter number");
line = (char *) malloc((sizeof(char) * size) + 1);
getline(&line, &size, stdin);
if (sscanf(line, "%d", &number) != 1) {
puts ("\nInvalid input");
} else {
break;
}
}
printf ("\nInput number\n%d\n", number);
return 0;
}
Your program prints Choose a numeric value>
and then waits for a line of input. Its then scans along the input, printing Choose a numerical value>
for each printable character until it reaches a digit or control character, and then stops. If there's no digit or control character, it reads another line a repeats.
If that's not what you want the program to do, you need to change it, but to have any chance of getting real help, you need to say what you want the program to do. From the prompt string it seems like you want to read a number, but what do you want to do if the input is not a number? Only one digit, or multiple digits? What about if the number is followed by some garbage? Depending on what you want, it might make more sense to use fgets to read a line, and then parse it (either with sscanf or atoi), since you can then more easily give sensible error messages about malformed input. Using scanf is generally more appropriate for reading machine-written data rather than interactive input
精彩评论