What seems to happen is that you're constantly calling new methods each time you make a choice, instead of returning to the first Menu()
call, which in my opinion should be ideal.
We can follow the flow of execution to see this clearly.
Assuming that main
calls Menu
, we have:
main
Menu
ValidateChoice
GetChoice (assume you return 1)
EnterData
Menu
ValidateChoice
GetChoice
EnterData
Menu
....
This explains why you have to press 3 multiple times before finally exiting the program, or basically returning to main.
One approach to solve this issue would be to make your ValidateChoice
return 1
or 0
(for valid choice or exit), and keep a loop in your Menu
method to verify this.
For instance:
void Menu(void)
{
do
{
printf( "\n" );
printf( "1. Enter data\n" );
printf( "2. Display data\n" );
printf( "3. Quit\n" );
} while (ValidateChoice());
return;
}
And have ValidateChoice
as follows:
int ValidateChoice()
{
char choice = '\0';
printf( "\nChoice: " );
choice = GetChoice();
switch (choice)
{
case '1':
EnterData();
break;
case '2':
DisplayData();
break;
case '3':
printf( "\nQuit\n" );
return 0;
default:
break;
}
return 1;
}