So, I've looked at several posts asking the same question, and none of them have worked for me. Well, they halfway work, as in they work when I'm not doing what I'm doing here. What I'm trying to do is have it display a few lines of text, ask for enter to continue, and display more text so that it doesn't just give you a huge text wall to read through all at once.
Here's everything I've got, and I'll mark where I've tried the wait for enter thing. I'm on windows if it makes a difference.
#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;
}
I'm assuming the while
part is the problem with it, but I can't think of a way to make it work within the conditional I've got. Is there a way this can be done?
At this line scanf("%s",playername); user usually press Enter to mark the end of input, but scanf is reading only to the new line symbol (Enter).
Next, at the line while (enter != '\r' && enter != '\n') { enter = getchar(); } //For Enter
getchar is sucking that new-line-char from the previous input.
As you do not change the enter variable before the new input, you still stuck to read the new-line-char.
As a result, entering the name making your code to run without stops.
Try to add fflush(stdin); instruction after any input operation. And remember to reset the enter variable.