我对此代码有疑问:
printf("Select your math: \n'+'addition \n'-'subtraction \n'*'multiplication \n'/' division \n");
char do_math;
scanf("%c", &do_math);
printf("Type 1 st number: ");
问题是,该程序不会等到我键入“do_math”,但它会在第一次printf之后显示“Type 1 st number:”。有什么想法吗?
答案 0 :(得分:3)
编译器错误编译的唯一方法是,如果您故意将scanf()
定义为无操作宏。不要责怪你的编译器!
程序中可能存在先前的scanf()
调用,它在输入缓冲区中留下了回车符。您可以通过打印do_math
的值来确认该假设。
尝试使用scanf(" %c", &do_math);
(%c
之前的空格)来丢弃此类空格。
(注意scanf()
的返回值也是一个好主意。)