So I have this code but I'm missing something and I can't quite seem to get it. The point of this is to prompt the user for a selection. They pick the food that they want to order, and it tells them it is added. When the user hits 0 the program ends by saying what the total is of all the items they chose. For some reason when I hit the end, it just states the total of the last item added. Any ideas on how to fix this so that the totals all add up?
#include<stdio.h>
#define SALES_TAX .06
int selection;
double total;
double amount;
int i;
double getPrice(int selection);
void printOptionName(int selection);
void printMenu();
double getPrice(int selection){
if (selection == 1){
amount = 5.99;
} else if (selection == 2){
amount = 6.99;
} else if (selection == 3){
amount = 7.99;
} else if (selection == 4){
amount = 10.50;
} else if (selection == 5){
amount = 3.50;
}
}
void printOptionName(int selection){
if (selection == 0){
printf("\nYour total is: ");
} else if (selection == 1){
printf("\nAdded a Small Pizza\n\n");
} else if (selection == 2){
printf("\nAdded a Medium Pizza\n\n");
} else if (selection == 3){
printf("\nAdded a Large Pizza\n\n");
} else if (selection == 4){
printf("\nAdded an order of Wings\n\n");
} else if (selection == 5){
printf("\nAdded a Drink\n\n");
} else
printf("Not a valid selection. Please select one of the following options: \n");
}
void printMenu(){
printf("0. (Complete Order)\n");
printf("1. Small Pizza ****** 5.99\n");
printf("2. Medium Pizza ****** 6.99\n");
printf("3. Large Pizza ****** 7.99\n");
printf("4. Wings ****** 10.50\n");
printf("5. Drink ****** 3.50\n");
printf("Enter the Number of your selection: \n");
}
int main(){
printMenu();
scanf("%d", &selection);
printOptionName(selection);
getPrice(selection);
while (selection != 0){
printMenu();
scanf("%d", &selection);
printOptionName(selection);
getPrice(selection);
}
for (i = 0; i <= selection; i++){
total = total + amount;
}
printf("%lf\n", total);
return 0;
}
Aucun commentaire:
Enregistrer un commentaire