C getchar()不等待输入/条件循环不int

我正在尝试向我的C控制台应用程序计算器添加一个function,提示用户决定是否要使用: yn执行另一个计算,但在测试中, getchar()拒绝等待输入并且程序继续进行,就像它收到了有效的输入。 以下是该function的最小示例:

 main() { char newCalculation; do{ lengthFormula(); /* main calculation formula */ printf("Would you like to do another calculation? (Y/N)"); newCalculation = getchar(); }while(tolower( newCalculation ) == 'y'); if(tolower(newCalculation) == 'n'){ exitProgram(); /* exit the program */ } while(tolower(newCalculation) != 'n' && tolower(newCalculation) != 'y'){ printf("This is not a valid response.\n Please enter \"Y\" if you want to do another calculation, or enter \"N\" to exit.\n"); newCalculation = getchar(); } return 0; } 

当我运行它时,程序不会等待输入后:

Would you like to do another calculation? (Y/N)

,而是继续进行,好像收到了无效的输入。 结果是它一个接一个地发出提示和无效的输入通知而没有空格:

Would you like to do another calculation? (Y/N)

This is not a valid response.

Please enter \"Y\" if you want to do another calculation, or enter \"N\" to exit.

如果我在此之后输入"y" ,则main()返回0并且程序终止。

有人能够看到我在哪里出错吗? 为什么控制台不会等待getchar()输入? 为什么有效输入在第一次无效响应后终止程序?

PS:请不要告诉我“读书”或者把我带到Dennis Ritchie或之前的SO讨论中。 我一直在研究Richie对I / O的讨论,以及来自Lynda.com和Wiley的类似文本,据我所知,之前的“它不会等待输入”的post都没有解决我的问题。

@simplicisveritatis这是我尝试过的代码修改。 仍然有相同的getchar问题。

 int main(void) { /* local variable declaration */ char newCalculation = 'y'; /* main function */ /*if(tolower( newCalculation ) == 'y') { lengthFormula(newCalculation); }*/ do { lengthFormula(); printf("Would you like to do another calculation? (Y/N)"); newCalculation = getchar(); if( tolower( newCalculation ) == 'n' ) { exitProgram(); } while( tolower( newCalculation ) != 'n' && tolower( newCalculation ) != 'y' ) { printf("This is not a valid response.\n Please enter \"Y\" if you want to do another calculation, or enter \"N\" to exit.\n"); newCalculation = getchar(); } }while( tolower( newCalculation ) == 'y' ); return 0; } 

你的代码有很多问题:主要应该是:

 int main(void){return 0;} 

你需要强制转换getchar(读取getchar),应该是:

 newCalculation = (char)getchar(); 

你做的方法做{} while; +而{}也使用错误。

请尝试以下方法:

 #include #include int main(void){ int validate; char menu_choice; validate = 0; do{ printf("Would you like another go?(y/n):\t" ); if(scanf(" %c", &menu_choice ) == 1){ if((menu_choice=='y') || (menu_choice=='Y')){ printf("You choosed Yes\n\n\n"); validate = 1; }else if((menu_choice=='n') || (menu_choice=='N')){ printf("You choosed No\n\n\n"); validate = 2; }else{ printf("Wrong Input.\n\n\n"); validate = 0; } } }while( validate == 0 || validate == 1); printf("Goodbye\n"); return 0; } 

包括你的三种情况: 退出条件错误输入和在while循环中计算

 main(){ do{ printf("Would you like to do another calculation? (Y/N)"); // get input char newCalculation; newCalculation = getchar(); // exit condition if(tolower(newCalculation) == 'n'){ exitProgram(); /* exit the program */ } // wrong input condition else if(tolower(newCalculation) != 'n' && tolower(newCalculation) != 'y'){ printf("This is not a valid response.\n Please enter \"Y\" if you want to do another calculation, or enter \"N\" to exit.\n"); // you should clear the input stream from the wrong input } else{ // calculate lengthFormula(); } }while(tolower(newCalculation) == 'y'); return 0; } 

为什么控制台不会等待getchar()的输入?

最有可能的是,函数lengthFormula()读取输入(例如,通过使用scanf()或其他),但不读取输入缓冲区中的行结束字符\n 。 然后从lengthFormula()返回后, getchar()必须从输入缓冲区中读取剩余内容,而不是请求新输入。

为什么有效输入在第一次无效响应后终止程序?

那是因为你的

  while(tolower(newCalculation) != 'n' && tolower(newCalculation) != 'y') 

y的响应之后,在响应n之后做同样的事情 – 它离开循环并进入下面

  return 0;