I created a C Program that will take all the order of the user then generate the grandtotal of the orders.
But when I will order another food, the program is closing automatically.
I don't know if this is about my getch or the breaks in my switch method. Sometimes, it will proceed to take another error but it automatically outputs "INVALID FOOD".
Here is my code:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void menu();
void foods();
void main();
char food;
int quantity;
float price;
float total;
float grandtotal;
int choice;
void main()
{
clrscr();
menu();
foods();
getch();
}
void menu(){
food = ' ';
quantity = 0;
price = 0;
total = 0;
choice = 0;
printf("Please select food from the following:
");
printf(" B = Burger, F = French Fries, P = Pizza, S = Sandwiches
");
printf("Enter food:");
scanf("%c", &food);
}
void foods(){
switch(food)
{
case 'B':
printf("You selected Burger!
");
printf("Enter quantity:");
scanf("%d", &quantity);
price = 95.50;
printf("
Do you want to order more? [1] Yes [2] No:");
scanf("%d", &choice);
total = price*quantity;
if(choice == 1){
menu();
break;
}
else if (choice == 2){
grandtotal = grandtotal + total;
printf("
Total Price is: %0.2f", grandtotal);
break;
}
case 'F':
printf("You selected French Fries!
");
printf("Enter quantity:");
scanf("%d", &quantity);
price = 47.75;
printf("
Do you want to order more? [1] Yes [2] No:");
scanf("%d", &choice);
total = price*quantity;
if(choice == 1){
menu();
break;
}
else if (choice == 2){
grandtotal = grandtotal + total;
printf("
Total Price is: %0.2f", grandtotal);
break;
}
case 'P':
printf("You selected French Pizza!
");
printf("Enter quantity:");
scanf("%d", &quantity);
price = 105.00;
printf("
Do you want to order more? [1] Yes [2] No:");
scanf("%d", &choice);
total = price*quantity;
if(choice == 1){
menu();
break;
}
else if (choice == 2){
grandtotal = grandtotal + total;
printf("
Total Price is: %0.2f", grandtotal);
break;
}
case 'S':
printf("You selected Sandwiches
");
printf("Enter quantity:");
scanf("%d", &quantity);
price = 75.50;
printf("
Do you want to order more? [1] Yes [2] No:");
scanf("%d", &choice);
total = price*quantity;
if(choice == 1){
main();
break;
}
else if (choice == 2){
grandtotal = grandtotal + total;
printf("
Total Price is: %0.2f", grandtotal);
break;
}
default:
printf("INVALID FOOD!");
break;
}
}
I wish someone could help or guide me. Thanks in advance.
See Question&Answers more detail:os