Food Menu (While Loop)
Food Menu (While Loop)
Write a program to allow user to order from a main menu. A sub-menu for food/drink selection is
available after the user input 1 or 2 from the main menu. After selection is made from the sub-menu,
the main menu is repeated until the user choose to exit. Upon exit, the program output the user’s
selection correspondingly.
Food menu
1. Nasi Goreng
2. Nasi Lemak
Main Menu
3. Pasta
1. Food
2. Drinks Selection: ?
3. Exit
Selection: ?
Display selections
You have selected: You have selected: You have selected: You have selected:
Food: Nasi Goreng Food: Nasi Goreng Food: No selection Food: No selection
Drink: Iced Lemon Tea Drink: No selection Drink: Iced Lemon Tea Drink: No selection
You have selected: You have selected: You have selected: You have selected:
Food: Nasi Goreng Food: Nasi Goreng Food: No selection Food: No selection
$3.50 $3.50 Drink: Iced Lemon Tea Drink: No selection
Drink: Iced Lemon Tea Drink: No selection $3.00 Total price: $0.00
$3.00 Total price: $3.50 Total price: $3.00
Total price: $6.50
You have selected: You have selected: You have selected: You have selected:
Food: (2) Nasi Goreng Food: Nasi Goreng Food: No selection Food: No selection
$3.50 $3.50 Drink: (3) Iced Lemon Drink: No selection
Drink: (2) Iced Lemon Drink: No selection Tea $3.00 Total price: $0.00
Tea $3.00 Total price: $3.50 Total price: $9.00
Total price: $13.00
B.
Add the following pricings to your menu, your program will perform calculation of the total price from
food and drink and display in the output message.
Selection: ? Selection: ?
D.
Modify your program to allow the user to cancel previous order(s) from food menu or drink menu.
Food menu
1. Nasi Goreng
2. Nasi Lemak Cancel Order
3. Pasta (All orders will be
Main Menu 4. Cancel Order cancelled, are you
5. Exit sure [Y/N]?)
1. Food
2. Drinks Selection: ? 1. Yes
3. Exit 2. No
Drinks menu
Selection: ? 1. Coca Cola
2. Iced Lemon Selection: ?
Tea
3. Teh Tarik
4. Cancel Order
5. Exit
Selection: ?
Display selections
2|Page
A. (answer)
#include <stdio.h>
#include <string.h>
int main(void) {
int mm=0,fm=0,dm=0;
int ff=0,df=0;
int fc,dc;
char food[20];
char drink[20];
while(mm!=3) {
printf("\nMenu\n");
printf("1.Food\n2.Drinks\n3.Exit");
printf("\nEnter selection:");
scanf("%d",&mm);
if(mm==1)
{ printf("\n1.Nasi lemak\n2.Chicken rice\n3.Pasta");
printf("\nEnter selection:");
scanf("%d",&fm);
ff=1; fc=fm;
if (fc==1) strcpy(food,"Nasi lemak");
else if (fc==2) strcpy(food,"Chicken rice");
else strcpy(food,"Pasta");}
if(mm==2)
{ printf("\n1.Coca cola\n2.Ice lemon tea\n3.Teh tarik");
printf("\nEnter selection:");
scanf("%d",&dm);
df=1; dc=dm;
if (dc==1) strcpy(drink,"Coca cola");
else if (dc==2) strcpy(drink,"Ice lemon tea");
else strcpy(drink,"Teh tarik");}
}
3|Page
if (ff==0 && df==1)
{ printf("\nYou have made the following selection:");
printf("\nFood:Not selected");
printf("\nDrink:%s",drink);}
return 0;
}
B. (answer)
#include <stdio.h>
#include <string.h>
int main(void) {
int mm=0,fm=0,dm=0;
int ff=0,df=0;
int fc,dc;
float fp,dp,tp=0.0;
char food[20];
char drink[20];
while(mm!=3) {
printf("\nMenu\n");
printf("1.Food\n2.Drinks\n3.Exit");
printf("\nEnter selection:");
scanf("%d",&mm);
if(mm==1)
{ printf("\n1.Nasi Goreng $3.50\n2.Nasi Lemak $4.50\n3.Pasta $8.50");
printf("\nEnter selection:");
scanf("%d",&fm);
ff=1; fc=fm;
if (fc==1) { strcpy(food,"Nasi Goreng"); fp=3.5;}
else if (fc==2) {strcpy(food,"Nasi Lemak"); fp=4.5;}
else {strcpy(food,"Pasta"); fp=8.5;}
}
if(mm==2)
{ printf("\n1.Coca cola $2.00\n2.Ice lemon tea $3.00\n3.Teh tarik $1.50");
printf("\nEnter selection:");
scanf("%d",&dm);
4|Page
df=1; dc=dm;
if (dc==1) {strcpy(drink,"Coca cola"); dp=2.0;}
else if (dc==2) {strcpy(drink,"Ice lemon tea"); dp=3.0;}
else {strcpy(drink,"Teh tarik"); dp=1.5;}
} }
return 0;
}
_____________________________________________________________________________________
C. (answer)
include <stdio.h>
#include <string.h>
int main(void) {
int mm=0,fm=0,dm=0;
int ff=0,df=0;
int fc,dc;
float fp=0.0,dp=0.0,tp=0.0;
5|Page
char food[20];
char drink[20];
int fqty[3]={0,0,0};
int dqty[3]={0,0,0};
while(mm!=3) {
printf("\nMenu\n");
printf("1.Food\n2.Drinks\n3.Exit");
printf("\nEnter selection:");
scanf("%d",&mm);
if(mm==1)
{ while (fm!=4)
{printf("\n1.Nasi Goreng $3.50\n2.Nasi Lemak $4.50\n3.Pasta $8.50\n4.Exit");
printf("\nEnter selection:");
scanf("%d",&fm);
ff=1; fc=fm;
if (fc==1) { strcpy(food,"Nasi Goreng");
fp+=3.5; fqty[0]++;}
else if (fc==2) {strcpy(food,"Nasi Lemak");
fp+=4.5; fqty[1]++;}
else if (fc==3) {strcpy(food,"Pasta");
fp+=8.5; fqty[2]++;}
}
}
if(mm==2)
{ while (dm!=4)
{printf("\n1.Coca cola $2.00\n2.Ice lemon tea $3.00\n3.Teh tarik
$1.50\n4.Exit");
printf("\nEnter selection:");
scanf("%d",&dm);
df=1; dc=dm;
if (dc==1) {strcpy(drink,"Coca cola");
dp+=2.0; dqty[0]++;}
else if (dc==2) {strcpy(drink,"Ice lemon tea");
dp+=3.0; dqty[1]++;}
else if (dc==3) {strcpy(drink,"Teh tarik");
dp+=1.5; dqty[2]++;}
}
}
}
6|Page
}
printf("\nDrink:Not selected");
printf("\nTotal price $%.2f",fp+dp);}
7|Page
else printf("\n(%d) Nasi Goreng $3.50 (%d) Nasi Lemak $4.50 (%d) Pasta
$8.50",fqty[0],fqty[1],fqty[2]);
return 0;
}
_____________________________________________________________________________________
D. (answer)
#include <stdio.h>
#include <string.h>
int main(void) {
int mm=0,fm=0,dm=0;
int ff=0,df=0;
int fc,dc;
float fp=0.0,dp=0.0,tp=0.0;
char food[20];
char drink[20];
int fqty[3]={0,0,0};
int dqty[3]={0,0,0};
while(mm!=3) {
printf("\nMenu\n");
printf("1.Food\n2.Drinks\n3.Exit");
printf("\nEnter selection:");
scanf("%d",&mm);
if(mm==1)
{ while (fm!=5)
{printf("\n1.Nasi Goreng $3.50\n2.Nasi Lemak $4.50\n3.Pasta $8.50\n4.Cancel
Order\n5.Exit");
8|Page
printf("\nEnter selection:");
scanf("%d",&fm);
fc=fm;
if (fc==1) { strcpy(food,"Nasi Goreng");
fp+=3.5; fqty[0]++; ff=1;}
else if (fc==2) {strcpy(food,"Nasi Lemak");
fp+=4.5; fqty[1]++; ff=1;}
else if (fc==3) {strcpy(food,"Pasta");
fp+=8.5; fqty[2]++; ff=1;}
else if (fc==4) {fp=0.0; fqty[0]=0; fqty[1]=0; fqty[2]=0; ff=0;}
}
}
if(mm==2)
{ while (dm!=5)
{printf("\n1.Coca cola $2.00\n2.Ice lemon tea $3.00\n3.Teh tarik
$1.50\n4.Cancel Order\n5.Exit");
printf("\nEnter selection:");
scanf("%d",&dm);
dc=dm;
if (dc==1) {strcpy(drink,"Coca cola");
dp+=2.0; dqty[0]++; df=1;}
else if (dc==2) {strcpy(drink,"Ice lemon tea");
dp+=3.0; dqty[1]++; df=1;}
else if (dc==3) {strcpy(drink,"Teh tarik");
dp+=1.5; dqty[2]++; df=1;}
else if (dc==4) {dp=0.0; dqty[0]=0; dqty[1]=0; dqty[2]=0; df=0;}
}
}
}
9|Page
else if (fqty[2]==0) printf("\n(%d) Nasi Goreng $3.50 (%d) Nasi Lemak
$4.50",fqty[0],fqty[1]);
else printf("\n(%d) Nasi Goreng $3.50 (%d) Nasi Lemak $4.50 (%d) Pasta
$8.50",fqty[0],fqty[1],fqty[2]);
printf("\nDrink:Not selected");
printf("\nTotal price $%.2f",fp+dp);}
10 | P a g e
else if (dqty[2]==0) printf("\n(%d) Coca Cola $2.00 (%d) Iced Lemon Tea
$3.00",dqty[0],dqty[1]);
else printf("\n(%d) Coca Cola $2.00 (%d) Iced Lemon Tea $3.50 (%d) Teh Tarik
$1.50",dqty[0],dqty[1],dqty[2]);
printf("\nTotal price $%.2f",fp+dp);}
return 0;
}
E. (Use of functions)
1. Rewrite your program in (A) using function for the calling of submenus.
2. Rewrite your program in (E1) using function to return the user’s selection of food and drink to
the main function.
3. Using the answer from (B), use a function to return the pricing of the user’s selection of
food/drink. (The function will have parameter(s) to assign pricing of selected food/drink and
return the pricing to the main function.)
Program Type1
<Function prototypes> No return type 1
<global variables> No argument
Main function
<local variable>
Type2
Calling the functions No return type
Argument(s)
Type3
Return type
2
No argument
<Function definition>
-------------------------
------------------------- Type4
Return type 3
Argument(s)
11 | P a g e
1. (answer)
#include <stdio.h>
#include <string.h>
void food_menu();
void drink_menu();
int main(void) {
int mm=0,fm=0,dm=0;
int ff=0,df=0;
int fc,dc;
char food[20];
char drink[20];
while(mm!=3) {
printf("\nMenu\n");
printf("1.Food\n2.Drinks\n3.Exit");
printf("\nEnter selection:");
scanf("%d",&mm);
if(mm==1)
{ food_menu();
printf("\nEnter selection:");
scanf("%d",&fm);
ff=1; fc=fm;
if (fc==1) strcpy(food,"Nasi lemak");
else if (fc==2) strcpy(food,"Chicken rice");
else strcpy(food,"Pasta");}
if(mm==2)
{ drink_menu();
printf("\nEnter selection:");
scanf("%d",&dm);
df=1; dc=dm;
if (dc==1) strcpy(drink,"Coca cola");
else if (dc==2) strcpy(drink,"Ice lemon tea");
else strcpy(drink,"Teh tarik");}
}
12 | P a g e
{ printf("\nYou have made the following selection:");
printf("\nFood:%s",food);
printf("\nDrink:Not selected");}
return 0;}
void food_menu()
{printf("\n1.Nasi lemak\n2.Chicken rice\n3.Pasta");}
void drink_menu()
{printf("\n1.Coca cola\n2.Ice lemon tea\n3.Teh tarik");}
_____________________________________________________________________________________
2. (answer)
#include <stdio.h>
#include <string.h>
int food_menu();
int drink_menu();
int main(void) {
int mm=0,fm=0,dm=0;
int ff=0,df=0;
int fc,dc;
char food[20];
char drink[20];
while(mm!=3) {
printf("\nMenu\n");
printf("1.Food\n2.Drinks\n3.Exit");
printf("\nEnter selection:");
scanf("%d",&mm);
if(mm==1)
{ fm=food_menu();
13 | P a g e
ff=1; fc=fm;
if (fc==1) strcpy(food,"Nasi lemak");
else if (fc==2) strcpy(food,"Chicken rice");
else strcpy(food,"Pasta");}
if(mm==2)
{ dm=drink_menu();
df=1; dc=dm;
if (dc==1) strcpy(drink,"Coca cola");
else if (dc==2) strcpy(drink,"Ice lemon tea");
else strcpy(drink,"Teh tarik");}
}
return 0;}
int food_menu()
{ int ftemp=0;
printf("\n1.Nasi lemak\n2.Chicken rice\n3.Pasta");
printf("\nEnter selection:");
scanf("%d",&ftemp);
return ftemp;}
int drink_menu()
{ int dtemp=0;
printf("\n1.Coca cola\n2.Ice lemon tea\n3.Teh tarik");
printf("\nEnter selection:");
scanf("%d",&dtemp);
return dtemp;}
14 | P a g e
_____________________________________________________________________________________
3. (answer)
#include <stdio.h>
#include <string.h>
int main(void) {
int mm=0,fm=0,dm=0;
int ff=0,df=0;
int fc,dc;
float fp,dp,tp=0.0;
while(mm!=3) {
printf("\nMenu\n");
printf("1.Food\n2.Drinks\n3.Exit");
printf("\nEnter selection:");
scanf("%d",&mm);
if(mm==1)
{ printf("\n1.Nasi Goreng $3.50\n2.Nasi Lemak $4.50\n3.Pasta $8.50");
printf("\nEnter selection:");
scanf("%d",&fm);
ff=1; fc=fm;
fp=fpricing(fc);
}
if(mm==2)
{ printf("\n1.Coca cola $2.00\n2.Ice lemon tea $3.00\n3.Teh tarik $1.50");
printf("\nEnter selection:");
scanf("%d",&dm);
df=1; dc=dm;
dp=dpricing(dc);
} }
return 0;
}
16 | P a g e