![]() |
|
scanf character - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: C, C++, & Obj-C (https://sinister.li/Forum-C-C-Obj-C) +--- Thread: scanf character (/Thread-scanf-character) Pages:
1
2
|
scanf character - Xe.4 - 12-20-2012 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);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: scanf character - Xe.4 - 12-20-2012 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);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: RE: scanf character - H4R0015K - 12-20-2012 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. RE: scanf character - H4R0015K - 12-20-2012 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. RE: scanf character - Xe.4 - 12-20-2012 Thanks, that solved the problem. but I want to know the reason aswell .
RE: scanf character - H4R0015K - 12-21-2012 %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. RE: scanf character - H4R0015K - 12-21-2012 %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. RE: scanf character - Xe.4 - 12-21-2012 Thank for explaining . RE: scanf character - The Alchemist - 12-23-2012 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... RE: scanf character - H4R0015K - 12-23-2012 (12-23-2012, 04:09 PM)The Alchemist Wrote: Or, don't use scanf to input characters. Use getchar() instead... i have read somewhere that getchar() are vulnerable and is recommended not to use it. |