getch();不暂停循环 [英] getch(); does not pause the loop

查看:171
本文介绍了getch();不暂停循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有这段代码:

while(true){
    printf("looping");
    getch();
}

无需等待每个循环的用户输入,而是在没有我的情况下循环继续进行;打印

Instead of waiting for user input on each loop, the loop just continues without me; printing

loop
loop
loop

直到我关闭程序.

有没有更好的方法来读取单个字符?我真正想要做的就是让用户输入"y"或"n"

Is there a better way to read a single character ? All i really want to do is to have the user input a 'y' or a 'n'

推荐答案

只需使用fgetc.我假设您正在使用它来打破循环,所以要更新您的示例:

Just use fgetc. I assume you're using this to break out of the loop, so to update your example:

#include <stdio.h>

char iput;

while(true){
    printf("looping");
    iput = fgetc(stdin);
    if(iput == 'n')
      break;
} 

这篇关于getch();不暂停循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆