Login Register






scanf character filter_list
Author
Message
scanf character #1
Guys, I have this strange problem in C, I can't figure out the problem. I want to make a small calculator and it is like this:

Code:
float a,b,total; char op; printf("a= ");scanf("%f",&a); printf("op[+,-,*,/,%]= ");scanf("%c",&op); printf("b= ");scanf("%f",&b);
The rest are switch statement for each operator.

After I compiled and run it, it just skips the scanf statement for variable 'op' and goes on to scanf for value b. like this:

a= 56
op[+,-,*,/,%]= b=
...

Thanks.

:epic:

Reply

scanf character #2
Guys, I have this strange problem in C, I can't figure out the problem. I want to make a small calculator and it is like this:

Code:
float a,b,total; char op; printf("a= ");scanf("%f",&a); printf("op[+,-,*,/,%]= ");scanf("%c",&op); printf("b= ");scanf("%f",&b);
The rest are switch statement for each operator.

After I compiled and run it, it just skips the scanf statement for variable 'op' and goes on to scanf for value b. like this:

a= 56
op[+,-,*,/,%]= b=
...

Thanks.

:epic:

Reply

RE: scanf character #3
Code:
float a,b,total; char op; printf("a= ");scanf("%f",&a); printf("op[+,-,*,/,%]= ");scanf(" %c",&op); printf("b= ");scanf("%f",&b);

after scanf(" leave a space then type %c",&op); now it will not get skipped i dont know why it is like this but i will let you know as soon as i come to know.
[Image: fow57.jpg]

Reply

RE: scanf character #4
Code:
float a,b,total; char op; printf("a= ");scanf("%f",&a); printf("op[+,-,*,/,%]= ");scanf(" %c",&op); printf("b= ");scanf("%f",&b);

after scanf(" leave a space then type %c",&op); now it will not get skipped i dont know why it is like this but i will let you know as soon as i come to know.
[Image: fow57.jpg]

Reply

RE: scanf character #5
Thanks, that solved the problem. but I want to know the reason aswell . Biggrin

Reply

RE: scanf character #6
%c format specifier requires a blank space before it because it start from a blank space or whitespace it does not store the blank space if it is not there it will skip the scanf fuction to solve this you can use %s which is string format specifier or leave a blank space before %c.
[Image: fow57.jpg]

Reply

RE: scanf character #7
%c format specifier requires a blank space before it because it start from a blank space or whitespace it does not store the blank space if it is not there it will skip the scanf fuction to solve this you can use %s which is string format specifier or leave a blank space before %c.
[Image: fow57.jpg]

Reply

RE: scanf character #8
Thank for explaining .

Reply

RE: scanf character #9
Or, don't use scanf to input characters. Use getchar() instead...
And before inputting a character, always use fflush(stdin) to flush any unnecessary characters.

I'm not going into further details... They are simple...
[Image: 2YpkRjy.png]
PM me if you need help.
My pastebin HERE. My URL Shortener HERE.

Reply

RE: scanf character #10
(12-23-2012, 04:09 PM)The Alchemist Wrote: Or, don't use scanf to input characters. Use getchar() instead...
And before inputting a character, always use fflush(stdin) to flush any unnecessary characters.

I'm not going into further details... They are simple...

i have read somewhere that getchar() are vulnerable and is recommended not to use it.
[Image: fow57.jpg]

Reply