【发布时间】: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