To be honest I am not sure this is a fgets issue. It may be an issue with running a fgets within a function or something I am just not aware of. I did attempt to run the contents of the function nameRequest() within the main() and commented everything out. Doing this did work, and gave the desired output. I appreciate any and all help. I will explain the details below:
Desired Outcome:
The program I have written is suppose to simply ask your name, then return via printf which presents the input given using the previous fgets.
Current Outcome:
What I am running into, is instead of asking your name then, allowing you to enter it, the program skips the fgets to the final printf and ends.
Here is what I get:
Compiler:
Welcome to the Game!
+=============+
|1. Create New |
|2. Exit Game |
+=============+
Your Selection: 1
Please enter a character name:
Is , correct?
Code:
#include <stdio.h>#include <string.h>
#define MAX_AMOUNT 15
void nameRequest(void);
void mainMenu(void);
int main(int argc, const char * argv[])
{
mainMenu();
return 0;
}
void mainMenu(void){
int choice = 0;
for (choice = 0; choice < 2 || choice > 2;) {
printf("Welcome to the Game!\n");
printf("+=============+\n");
printf("|1. Create New|\n");
printf("|2. Exit Game |\n");
printf("+=============+\n");
printf("Your Selection: ");
scanf("%d", &choice);
if (choice == 1) {
nameRequest();
choice = 2;
}
else if (choice == 2) {
choice = 2;
}
else {
printf("\nError! You must enter in one of the above numbers!\n");
}
}
}
void nameRequest(void){
char toonName[MAX_AMOUNT];
int length = 0;
printf("Please enter a character name: ");
fgets(toonName, MAX_AMOUNT, stdin);
length = strlen(toonName);
toonName[length - 1] = 0;
printf("\nIs %s, correct?", toonName);
}