的getch(); 不会暂停循环
我有这段代码,例如:
while(true){ printf("looping"); getch(); }
而不是在每个循环上等待用户输入,循环只在没有我的情况下继续; 印花
loop loop loop
直到我关闭程序。
有没有更好的方法来阅读单个字符? 我真正想做的就是让用户输入’y’或’n’
只需使用fgetc。 我假设您正在使用它来摆脱循环,所以要更新您的示例:
#include char iput; while(true){ printf("looping"); iput = fgetc(stdin); if(iput == 'n') break; }
#include #include #include void main() { while(1) { printf("\nlooping"); char t=getch(); if(t=='n') exit(0); } }
你也可以使用
fflush(stdin)
在调用getch之前
问候