Code:
int main(){
int ans;
//Get user's input
printmenu();
scanf("%d", &ans);
while (ans !=6) {
if (ans==1)
tripfueleff();
else if (ans==2)
getitem();
else if (ans==3)
printlogo();
else if (ans==4)
multgame();
else if (ans==5)
printfuelgraph();
}
}
//Prints out menu
void printmenu() {
printf("Which of the following options would you like?\n");
printf("1) Calculate fuel efficiency for a trip.\n");
printf("2) Determine whether or not to go back home and pick up an item.\n");
printf("3) Print the car logo to the display.\n");
printf("4) Play the multiplication game.\n");
printf("5) Print a visual display of the recent fuel efficiency.\n");
printf("6) Quit\n");
}
That's what I got for a menu so far. But after I choose an option, and the specified function runs, it repeats the function. After the function is done running I need it to go back to the menu instead of repeating. Do I need to add something to the while statement to do this or something to the functions themselves?
Here's an example of one of the functions:
Code:
void getitem() {
int mins_dist, mins_before, mins_forgot, total, spare1, spare2;
//User input
printf("How long does it take you to drive to work (in minutes)?\n");
scanf("%d", &mins_dist);
printf("How many minutes before work did you start?\n");
scanf("%d", &mins_before);
printf("How many minutes did it take you to realize you forgot an item?\n");
scanf("%d", &mins_forgot);
//Determine if you should turn around
total = (mins_forgot * 2) + mins_dist;
spare1 = mins_before - total;
spare2 = mins_before - mins_dist;
if (total <= mins_before)
printf("Go back home and pick up the item.\nYou will arrive at work with %d minutes to spare.\n", spare1);
else
printf("Just go to work, you'll have to do without the item.\nYou will arrive at work with %d minutes to spare.\n", spare2);
}