Program repeats each time a character is scanned .. How to stop it ?
Posted
by ZaZu
on Stack Overflow
See other posts from Stack Overflow
or by ZaZu
Published on 2010-05-09T03:01:43Z
Indexed on
2010/05/09
3:08 UTC
Read the original article
Hit count: 265
Hello there,
I have a program that has this code :
#include<stdio.h>
main(){
int input;
char g;
do{
printf("Choose a numeric value");
printf(">");
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 !
© Stack Overflow or respective owner