Header file:
#include <stdio.h>
#include <stdlib.h>
int print_menu(){
printf ("MENU\n");
printf ("1. Total of All Top Scores for the Week\n");
printf ("2. Total of All High Scores for the Week\n");
printf ("3. Total Machine High Scores for the Week\n");
printf ("4. Machine High Score for the Week\n");
printf ("5. EXIT\n");
printf ("Enter Selection:");
int selection = getchar();
return selection;
}
Main C file:
#include <stdio.h>
#include <stdlib.h>
#include "lab1.h"
int main(int argc, char *argv[]){
int selection = print_menu();
while(1)
{
switch (selection)
{
case '1':
printf ("\nselected 1\n");
break;
case '2':
printf ("\nselected 2\n");
break;
case '3':
printf ("\nselected 3\n");
break;
case '4':
printf ("\nselected 4\n");
break;
case '5':
printf ("\nExit\n");
exit(0);
break;
default:
printf ("Invalid Selection");
print_menu();
break;
};
};
}
My issue is that when I run the program and enter in a wrong character the program is suppose to re-print the menu and ask for the selection again. Except it prints out the menu twice. Example:
maiah@maiah-vb:~/shared$ ./a.out
MENU
1. Total of All Top Scores for the Week
2. Total of All High Scores for the Week
3. Total Machine High Scores for the Week
4. Machine High Score for the Week
5. EXIT
Enter Selection:d
Invalid Selection
MENU
1. Total of All Top Scores for the Week
2. Total of All High Scores for the Week
3. Total Machine High Scores for the Week
4. Machine High Score for the Week
5. EXIT
Enter Selection:
Invalid Selection
MENU
1. Total of All Top Scores for the Week
2. Total of All High Scores for the Week
3. Total Machine High Scores for the Week
4. Machine High Score for the Week
5. EXIT
Enter Selection:
And then you are given the option to enter in another selection. When I went through I noticed that it seems to be taking the selection of 'd' and outputting correctly but then acting like a blank space or new line has been automatically entered and proceeds to check the selection (I'm not sure if this is the actual issue though - this is just how it seems to be acting). If anyone has any ideas on how to fix this and explain why this is happening. Any help would be great!