I have this function, which is meant to get a user's input, validate, and translate it to an integer:
Code:
int menu(){
int rv = 0;
char in;
do{
//Display menu
printf("----------------------------\n");
printf("|What would you like to do?|\n");
printf("|1. View [A]ll Students |\n");
printf("|2. View [R]ecord Number |\n");
printf("|3. View [S]tudent Number |\n");
printf("|4. [Q]uit |\n");
printf("----------------------------\n\n");
printf("Enter your selection: ");
// while ((in = getchar()) != EOF);
scanf("%c", &in);
//Validate
if (in == '1' || in == 'A' || in == 'a') rv = 1;
else if (in == '2' || in == 'R' || in == 'r') rv = 2;
else if (in == '3' || in == 'S' || in == 's') rv = 3;
else if (in == '4' || in == 'Q' || in == 'q') rv = 4;
else printf ("Invalid entry, please try again\n");
} while (!rv);
return (rv);
}
The first time it's called, it works as expected:
Code:
----------------------------
|What would you like to do?|
|1. View [A]ll Students |
|2. View [R]ecord Number |
|3. View [S]tudent Number |
|4. [Q]uit |
----------------------------
But any additional times I call it, it acts as if I already typed something before going on to what I want:
Code:
Enter your selection: a
----------------------------
|What would you like to do?|
|1. View [A]ll Students |
|2. View [R]ecord Number |
|3. View [S]tudent Number |
|4. [Q]uit |
----------------------------
Enter your selection: Invalid entry, please try again
----------------------------
|What would you like to do?|
|1. View [A]ll Students |
|2. View [R]ecord Number |
|3. View [S]tudent Number |
|4. [Q]uit |
----------------------------
Enter your selection:
As you can see dummied out in the code, I tried clearing the keybuffer, but it didn't help. Anyone know what's going on?