【问题标题】:Enter to Continue输入继续
【发布时间】:2026-01-31 09:20:05
【问题描述】:

所以,我查看了几个提出相同问题的帖子,但没有一个对我有用。嗯,他们工作到一半,就像我不做我在这里做的事情时他们工作一样。我想做的是让它显示几行文本,要求输入以继续,并显示更多文本,这样它不仅会给你一个巨大的文本墙来一次阅读。

这是我所拥有的一切,我会标记我尝试过等待进入的地方。如果有影响,我会在 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 部分是它的问题,但我想不出一种方法让它在我拥有的条件下工作。有没有办法做到这一点?

【问题讨论】:

  • 我更新了我的答案以表明您可以将提示传递给“更少”以告诉用户该做什么,并且它还有其他可配置选项。他们不必知道这不是您的程序,它为他们提供了很大的灵活性。
  • @hego64-- 我在答案中添加了一些 cmets 和一个关于使用 fflush(stdin) 的链接。刷新输入流是 C 标准中未定义的行为,不可移植(例如,无法在 Linux 系统上运行),并且是错误的方法。

标签: c input return enter continue


【解决方案1】:

使用getc() 暂停。在第一个 scanf() 之后和每次调用 getc() 后丢弃多余的字符以进行暂停。正如David Bowling 提到的,不要fflush(stdin),因为它会导致未定义的行为。

查看this相关帖子。

...
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");
...

【讨论】:

  • 这和以前做的一样。它所做的只是显示完整的文本墙。就像@David Bowling 所说的那样简单地使用getchar()
  • 哥奇亚。我已经编辑了我的答案。看到这个问题:*.com/q/22226529/2281645,正如其他人所说,不要fflush(stdin)
  • 这里还是有问题....如果用户在Press enter to continue提示符下按回车,则换行符被getc()消耗,循环中下一次调用getchar()将等待更多输入。这是我编写一个单独的函数来获取单个字符的原因之一:如果该字符是\n,您不想开始循环。您可以通过删除循环之前的getc() 来解决这个问题。输入流已经被清除,因此循环将继续,直到用户按下 ENTER。
【解决方案2】:

此示例使用popen() 写入作为子进程运行的less 命令,可能会为您省去很多麻烦,并根据情况为用户提供更好的体验,因为他们可以来回滚动。

取决于您的程序需要的控制级别。

我测试了这段代码。它有效。

更新:我添加了pclose(),所以它会关闭 fd,所以更少知道输入流是否完整,并且 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");
}

【讨论】:

  • @DavidBowling 添加答案后我意识到它可能有点超出范围,但希望它会帮助其他看到问题并需要这样的解决方案的人。谢谢!
【解决方案3】:

在这一行 scanf("%s",playername); 用户通常按 Enter 标记输入结束,但 scanf 只读取换行符 (Enter)。 p>

接下来,在while (enter != '\r' &amp;&amp; enter != '\n') { enter = getchar(); } //For Enter 行,getchar 正在从前一个输入中吸取新行字符。

由于您没有在新输入之前更改 enter 变量,因此您仍然坚持阅读 new-line-char。

因此,输入名称可使您的代码不间断地运行。

尝试在任何输入操作后添加fflush(stdin);指令。并记得重置输入变量。

【讨论】:

  • 我打错了...对不起... fflush(stdin)
  • 这修复了它!谢谢!这当然比我之前尝试的要简单得多。
  • 是的,我输入后发现错字并意识到它是错误的。在我这边修复了它,它现在可以工作了。非常感谢。
  • fflush(stdin) 是根据标准未定义的行为(它在某些系统上确实有效,但不可移植)。
【解决方案4】:

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 标准中明确不允许使用 fflush(stdin) 清除输入流(C11 §7.21.5.2 2)。 Windows 系统已经为此定义了行为,但它不是可移植的(例如,在 Linux 上不起作用)。 You can read more about this issue here.

这是一个玩具示例。请注意在调用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
    }
}

【讨论】:

    【解决方案5】:

    连续使用 scanf() 会导致缓冲区输入问题。对于窗口用户,应该使用 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;
    }
    

    【讨论】: