输入继续
Enter to Continue
所以,我看了几个问同样问题的帖子,其中 none 对我有用。好吧,他们半途而废,就像当我不在这里做我正在做的事情时他们工作一样。我想要做的是让它显示几行文本,要求输入以继续,并显示更多文本,这样它不仅会给你一个巨大的文本墙来一次阅读所有内容。
这是我所有的东西,我会标记我尝试等待输入的地方。我在 windows 如果它有所作为。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<process.h>
#include<conio.h>
#include<ctype.h>
//void playerName (char* playername){
//FILE* playerNameFile = fopen("player.data","w");
//fprintf(playerNameFile, "%s", playerName);
//}
int main(){
char response;
char playername[20];
char enter = 0; //For Enter
system("cls");
printf("Would you like to embark on an adventure?\n Y/N?:\n\n\n\n");
response = toupper(getch());
if (response=='Y'){
printf("Before we start, what's your name?\n Your name is: ");
scanf("%s",playername);
printf("%s, Story Text.\n");
printf("Story Text\n");
printf("Story Text.\n");
printf("Story Text\n");
printf("Story Text\n");
printf("Press enter to continue\n");
while (enter != '\r' && enter != '\n') { enter = getchar(); } //For Enter
printf("Story Text\n");
printf("Story Text\n");
printf("Story Text");
printf("Story Text \n");
printf("Story Text\n");
printf("Story Text\n");
printf("Story Text\n");
printf("Press enter to continue\n");
while (enter != '\r' && enter != '\n') { enter = getchar(); } //For Enter
printf("Story Text\n");
printf("Story Text\n\n");
printf("Story Text\n");
printf("Story Text\n");
}
else if (response=='N'){
printf("Well, I'll see you later then!");
exit(0);
}
printf("Embark on your quest?\n Y/N?");
response = toupper(getch());
if (response=='Y'){
system("cls");
printf("'\nStory Text\n");
printf("'Story Text\n");
printf("The end for now");
}
else if (response=='N'){
printf("Come back when you are ready to embark on your quest.");
exit(0);
}
return 0;
}
我假设 while
部分是它的问题,但我想不出一种方法让它在我的条件下工作。有什么办法可以做到吗?
暂停使用getc()
。丢弃第一个 scanf()
之后和每次调用 getc()
之后的额外字符以进行暂停。正如 David Bowling 提到的那样,不要 fflush(stdin)
因为它会导致未定义的行为。
查看 this 相关 post。
...
scanf("%s",playername);
/* discard extra chars */
while((c= getchar()) != '\n' && c != EOF);
printf("Story Text\n");
printf("Story Text.\n");
printf("Story Text\n");
printf("Story Text\n");
/* pause */
printf("Press enter to continue\n");
getc(stdin);
while((c= getchar()) != '\n' && c != EOF);
printf("Story Text\n");
printf("Story Text\n");
printf("Story Text");
printf("Story Text \n");
printf("Story Text\n");
printf("Story Text\n");
...
本例使用popen()
写入less命令运行作为子进程,可能会为您省去很多麻烦,给用户带来更好的体验,视情况而定,因为less他们可以来回滚动。
取决于您的程序需要何种级别的控制。
我测试了这段代码。有效。
UPDATE: 我添加了 pclose()
所以它会关闭 fd 所以 less 知道输入流是完整的,并且 pclose() 会等待子进程 运行 更少到终端(当用户按 q 时)。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
int main(void)
{
FILE *lessfp;
if ((lessfp = popen("less -P\"[SPACE] next page, [u] prev page, [q] done:\"", "w")) == NULL) {
perror("popen");
exit(-1);
}
printf("writing data\n");
for (int i = 0; i < 1000; i++) {
fprintf(lessfp, "This is some stuff %d\n", i++);
}
pclose(lessfp);
printf("less exited!\n");
}
在这一行 scanf("%s",playername); 用户通常按 Enter 来标记输入结束,但 scanf 只读到新行符号 (输入).
接下来,在第 while (enter != '\r' && enter != '\n') { enter = getchar(); } //For Enter
行,getchar 从之前的输入中吸取 new-line-char。
由于你在新的输入之前没有改变enter变量,所以你还是卡在阅读了new-line-char。
因此,输入名称使您的代码不停地 运行。
尝试在任何输入操作后添加fflush(stdin);指令。并记得重置输入变量。
当getchar()
从键盘读取用户输入的一个字符时,输入流中可能会留下多余的字符。在尝试再次阅读之前,您需要丢弃这些额外的字符。
起初我没有注意到对 scanf()
的调用(您的程序将受益于一些空白以提高可读性),但是 scanf()
也会在输入流中留下字符。出于这个原因,组合 scanf()
和 getchar()
总是很棘手,最好的解决方案可能是对所有用户输入使用 fgets()
。
但是,为避免过多更改输入代码,您可以坚持使用 scanf()
和 getchar()
。由于您需要在 scanf()
和 getchar()
之后清除输入流,因此最好编写一个函数 clear_stream()
:
void clear_stream(void)
{
int c;
while ((c = getchar()) != '\n' && c != EOF) {
continue;
}
}
这可以合并到函数 get_single_char()
中,也可以在调用 scanf()
后单独使用,以清除输入流。调用 clear_stream()
时,输入流中必须至少有一个字符,否则函数将等待更多的输入。 C 标准 (C11 §7.21.5.2 2) 明确不允许使用 fflush(stdin)
清除输入流。 Windows 系统为此定义了行为,但它不可移植(例如,在 Linux 上不起作用)。 .
这是一个玩具示例。请注意在对 scanf()
的调用中使用宽度规范以避免缓冲区溢出。
#include <stdio.h>
int get_single_char(void);
void clear_stream(void);
int main(void)
{
int choice;
char name[100];
printf("Enter name: ");
scanf("%99s", name);
clear_stream();
printf("Play a game, %s (y/n)? ", name);
choice = get_single_char();
if (choice == 'y') {
printf("Press ENTER to continue:");
get_single_char();
} else {
puts("That was uninspiring!");
}
puts("bye");
return 0;
}
int get_single_char(void)
{
int input;
input = getchar();
if (input != '\n') {
clear_stream();
}
return input;
}
void clear_stream(void)
{
int c;
while ((c = getchar()) != '\n' && c != EOF) {
continue; // discard extra characters
}
}
连续使用scanf() 会产生缓冲区输入问题。对于 window 用户,应该使用 fflush(stdin) 但对于 Linux,它不起作用。因此 Linux 用户必须在第二个 scanf() 之前重复第一个 scanf() 而不是 fflush(stdin)。这是我的程序代码:
/* Driver program to test above functions*/
int main()
{
/* Start with the empty list */
struct Node* head = NULL;
char decision = 'y';
int count=1, val;
while(decision=='y'){
printf("Input the data for node %d ",count);
scanf("%d", &val); <--------------------------------------first scanf()
insertAtFirstLocation(&head, val);
printf("\nEnter more <y>es/<n>o ? ");
//used instead of fflush(stdin) to refresh input buffer
scanf("%d", &val); <--------------------------------------repeated to refresh
scanf("%c", &decision); <---------------------------------second scanf() for new input
count++;
}
printList(head);
return 0;
}
所以,我看了几个问同样问题的帖子,其中 none 对我有用。好吧,他们半途而废,就像当我不在这里做我正在做的事情时他们工作一样。我想要做的是让它显示几行文本,要求输入以继续,并显示更多文本,这样它不仅会给你一个巨大的文本墙来一次阅读所有内容。
这是我所有的东西,我会标记我尝试等待输入的地方。我在 windows 如果它有所作为。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<process.h>
#include<conio.h>
#include<ctype.h>
//void playerName (char* playername){
//FILE* playerNameFile = fopen("player.data","w");
//fprintf(playerNameFile, "%s", playerName);
//}
int main(){
char response;
char playername[20];
char enter = 0; //For Enter
system("cls");
printf("Would you like to embark on an adventure?\n Y/N?:\n\n\n\n");
response = toupper(getch());
if (response=='Y'){
printf("Before we start, what's your name?\n Your name is: ");
scanf("%s",playername);
printf("%s, Story Text.\n");
printf("Story Text\n");
printf("Story Text.\n");
printf("Story Text\n");
printf("Story Text\n");
printf("Press enter to continue\n");
while (enter != '\r' && enter != '\n') { enter = getchar(); } //For Enter
printf("Story Text\n");
printf("Story Text\n");
printf("Story Text");
printf("Story Text \n");
printf("Story Text\n");
printf("Story Text\n");
printf("Story Text\n");
printf("Press enter to continue\n");
while (enter != '\r' && enter != '\n') { enter = getchar(); } //For Enter
printf("Story Text\n");
printf("Story Text\n\n");
printf("Story Text\n");
printf("Story Text\n");
}
else if (response=='N'){
printf("Well, I'll see you later then!");
exit(0);
}
printf("Embark on your quest?\n Y/N?");
response = toupper(getch());
if (response=='Y'){
system("cls");
printf("'\nStory Text\n");
printf("'Story Text\n");
printf("The end for now");
}
else if (response=='N'){
printf("Come back when you are ready to embark on your quest.");
exit(0);
}
return 0;
}
我假设 while
部分是它的问题,但我想不出一种方法让它在我的条件下工作。有什么办法可以做到吗?
暂停使用getc()
。丢弃第一个 scanf()
之后和每次调用 getc()
之后的额外字符以进行暂停。正如 David Bowling 提到的那样,不要 fflush(stdin)
因为它会导致未定义的行为。
查看 this 相关 post。
...
scanf("%s",playername);
/* discard extra chars */
while((c= getchar()) != '\n' && c != EOF);
printf("Story Text\n");
printf("Story Text.\n");
printf("Story Text\n");
printf("Story Text\n");
/* pause */
printf("Press enter to continue\n");
getc(stdin);
while((c= getchar()) != '\n' && c != EOF);
printf("Story Text\n");
printf("Story Text\n");
printf("Story Text");
printf("Story Text \n");
printf("Story Text\n");
printf("Story Text\n");
...
本例使用popen()
写入less命令运行作为子进程,可能会为您省去很多麻烦,给用户带来更好的体验,视情况而定,因为less他们可以来回滚动。
取决于您的程序需要何种级别的控制。
我测试了这段代码。有效。
UPDATE: 我添加了 pclose()
所以它会关闭 fd 所以 less 知道输入流是完整的,并且 pclose() 会等待子进程 运行 更少到终端(当用户按 q 时)。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
int main(void)
{
FILE *lessfp;
if ((lessfp = popen("less -P\"[SPACE] next page, [u] prev page, [q] done:\"", "w")) == NULL) {
perror("popen");
exit(-1);
}
printf("writing data\n");
for (int i = 0; i < 1000; i++) {
fprintf(lessfp, "This is some stuff %d\n", i++);
}
pclose(lessfp);
printf("less exited!\n");
}
在这一行 scanf("%s",playername); 用户通常按 Enter 来标记输入结束,但 scanf 只读到新行符号 (输入).
接下来,在第 while (enter != '\r' && enter != '\n') { enter = getchar(); } //For Enter
行,getchar 从之前的输入中吸取 new-line-char。
由于你在新的输入之前没有改变enter变量,所以你还是卡在阅读了new-line-char。
因此,输入名称使您的代码不停地 运行。
尝试在任何输入操作后添加fflush(stdin);指令。并记得重置输入变量。
当getchar()
从键盘读取用户输入的一个字符时,输入流中可能会留下多余的字符。在尝试再次阅读之前,您需要丢弃这些额外的字符。
起初我没有注意到对 scanf()
的调用(您的程序将受益于一些空白以提高可读性),但是 scanf()
也会在输入流中留下字符。出于这个原因,组合 scanf()
和 getchar()
总是很棘手,最好的解决方案可能是对所有用户输入使用 fgets()
。
但是,为避免过多更改输入代码,您可以坚持使用 scanf()
和 getchar()
。由于您需要在 scanf()
和 getchar()
之后清除输入流,因此最好编写一个函数 clear_stream()
:
void clear_stream(void)
{
int c;
while ((c = getchar()) != '\n' && c != EOF) {
continue;
}
}
这可以合并到函数 get_single_char()
中,也可以在调用 scanf()
后单独使用,以清除输入流。调用 clear_stream()
时,输入流中必须至少有一个字符,否则函数将等待更多的输入。 C 标准 (C11 §7.21.5.2 2) 明确不允许使用 fflush(stdin)
清除输入流。 Windows 系统为此定义了行为,但它不可移植(例如,在 Linux 上不起作用)。
这是一个玩具示例。请注意在对 scanf()
的调用中使用宽度规范以避免缓冲区溢出。
#include <stdio.h>
int get_single_char(void);
void clear_stream(void);
int main(void)
{
int choice;
char name[100];
printf("Enter name: ");
scanf("%99s", name);
clear_stream();
printf("Play a game, %s (y/n)? ", name);
choice = get_single_char();
if (choice == 'y') {
printf("Press ENTER to continue:");
get_single_char();
} else {
puts("That was uninspiring!");
}
puts("bye");
return 0;
}
int get_single_char(void)
{
int input;
input = getchar();
if (input != '\n') {
clear_stream();
}
return input;
}
void clear_stream(void)
{
int c;
while ((c = getchar()) != '\n' && c != EOF) {
continue; // discard extra characters
}
}
连续使用scanf() 会产生缓冲区输入问题。对于 window 用户,应该使用 fflush(stdin) 但对于 Linux,它不起作用。因此 Linux 用户必须在第二个 scanf() 之前重复第一个 scanf() 而不是 fflush(stdin)。这是我的程序代码:
/* Driver program to test above functions*/
int main()
{
/* Start with the empty list */
struct Node* head = NULL;
char decision = 'y';
int count=1, val;
while(decision=='y'){
printf("Input the data for node %d ",count);
scanf("%d", &val); <--------------------------------------first scanf()
insertAtFirstLocation(&head, val);
printf("\nEnter more <y>es/<n>o ? ");
//used instead of fflush(stdin) to refresh input buffer
scanf("%d", &val); <--------------------------------------repeated to refresh
scanf("%c", &decision); <---------------------------------second scanf() for new input
count++;
}
printList(head);
return 0;
}