【发布时间】:2017-04-26 02:22:01
【问题描述】:
我正在编写一个简单的代码,该代码每秒保持递增一个计数器,并且只有在按下“q”时才会停止递增。
我尝试了 getchar() 和 getline() ,但它们要求用户为每个循环输入一个输入。
为了更简单,这段代码一直依赖控制台,直到我通过 (Ctrl+c) 终止它,我想使用 'q' 来执行相同的功能。
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int j=0;
int main()
{
printf("Enter Q to quit !\n");
while(1)
{
j++;
printf("%d\n",j);
Sleep(1000);
system("cls");
}
return 0;
}
【问题讨论】:
-
我看到你使用的是 Windows,所以 MSVC 有
kbhit()或_kbhit()来检测按键,之后你调用getch()或_getch()来获取按键。 -
真的,“
system("cls");”? 2016 年?
标签: c switch-statement