重复直到用户按下回车
Repeat until user presses enter
我想要一个循环,重复直到用户按下回车键。
我试过 while(getchar != '\n'){}
但是这个 waitet 每次都在输入。现在我不知道该怎么做。
do {
clear;//system("cls");
printf("\nPress [enter] to continue");
printf(".");
Sleep(500);
printf(".");//should give a output with press enter to continue... and wait after every point.
Sleep(500);
printf(".");
Sleep(500);
}while(getchar() != '\n');
以下代码来自kbhit for linux
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
int kbhit(void)
{
struct termios oldt, newt;
int ch;
int oldf;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
fcntl(STDIN_FILENO, F_SETFL, oldf);
if(ch != EOF)
{
ungetc(ch, stdin);
return 1;
}
return 0;
}
int main(void)
{
while(!kbhit())
puts("Press a key!");
printf("You pressed '%c'!\n", getchar());
return 0;
}
我想要一个循环,重复直到用户按下回车键。
我试过 while(getchar != '\n'){}
但是这个 waitet 每次都在输入。现在我不知道该怎么做。
do {
clear;//system("cls");
printf("\nPress [enter] to continue");
printf(".");
Sleep(500);
printf(".");//should give a output with press enter to continue... and wait after every point.
Sleep(500);
printf(".");
Sleep(500);
}while(getchar() != '\n');
以下代码来自kbhit for linux
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
int kbhit(void)
{
struct termios oldt, newt;
int ch;
int oldf;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
fcntl(STDIN_FILENO, F_SETFL, oldf);
if(ch != EOF)
{
ungetc(ch, stdin);
return 1;
}
return 0;
}
int main(void)
{
while(!kbhit())
puts("Press a key!");
printf("You pressed '%c'!\n", getchar());
return 0;
}