Tag: 输入缓冲区

getchar在交换机情况下不工作(c)

使用一个非常简单的计算器程序,提示用户执行操作,然后提示两个整数来执行此操作。 程序应该在这些操作之后循环,除非用户输入字符’q’,此时程序应该退出。 #include int main (void) { char c; int number[2], num1, num2, result; double num1d, num2d, resultd; int done=1; while(done) { printf(“\t What sort of operation would you like to perform? \n \t Type + – * / accordingly. \n”); c = getchar(); printf(“\tplease enter a number \n”); scanf(“%d”,&number[0]); printf(“\tplease enter another number \n”); […]

为什么在循环内部,带有%d的scanf()不会等待用户输入,以防它先前收到无效输入?

我正在使用scanf() returns when it gets what is expects or when it doesn’t. What happens is it gets stuck in the scanf() returns when it gets what is expects or when it doesn’t. What happens is it gets stuck in the while()循环中。 据我所知test = scanf(“%d”, &testNum); 如果收到数字则返回1,否则返回0。 我的代码: #include int main(void) { while (1) { int […]