In: Computer Science
in c code
For example, a sample execution of your code would be as follows:
Cookie Corner! (name it whatever you want)
Please enter your first name.
Jane
Jane, what type of cookie would you like to order?
1
You selected a SUGAR cookie. How many cookies would you like?
4
Jane, your total cost is $1.00
Would you like to order another type of cookie?
No
Thanks for ordering!
SOURCE CODE:
*Please follow the comments to better understand the code.
**Please look at the Screenshot below and use this code to copy-paste.
***The code in the below screenshot is neatly indented for better understanding.
#include <stdio.h>
#include<string.h>
double order()
{
double price,cost;
int quantity;
// read the user choice
int choice;
// ask user what type of cookie they would like to order and how
many.
printf("1. Sugar - $0. 25\n");
printf("2. Chocolate Chip - $0.35\n");
printf("3. Peanut Butter - $0.35\n");
scanf("%d",&choice);
if(choice==1)
{
printf("You selected a SUGAR cookie. How many cookies would you
like?\n");
scanf("%d",&quantity);
price=0.25;
}
else if(choice==2)
{
printf("You selected a CHOCLATE cookie. How many cookies would you
like?\n");
scanf("%d",&quantity);
price=0.35;
}
else if(choice==3)
{
printf("You selected a PEANUT BUTTER cookie. How many cookies would
you like?\n");
scanf("%d",&quantity);
price=0.35;
}
else
{
return -1;
}
// calculate the cost for this cookie
cost=quantity*price;
// return the cost for this item
return cost;
}
int main()
{
// cost and total cost are the double value ex: 1.23, 23.45
etc.,
double cost=0, totalCost=0;
char name[100],yesOrNo[10];
printf("Welcome to Cookie Corner!\n");
// ask name
printf("Please enter your first name:\n");
scanf("%s",name);
while(1)
{
printf("%s, what type of cookie would you like to
order?\n",name);
cost=order();
if(cost==-1)
printf("That's a wrong choice. Please try again..");
else
{
printf("%s, your cost for this item is $%.2lf.",name,cost);
// add it to the totalCost
totalCost+=cost;
}
// ask for another choclate??
printf("\nWould you like to order another type of cookie? ");
scanf("%s",yesOrNo);
if(strcmp(yesOrNo,"No")==0)
{
printf("%s, your total cost is $%.2lf",name,totalCost);
break;
}
}
printf("\nThanks for ordering");
return 0;
}
=============
SCREENSHOT:
OUTPUT:
Welcome to Cookie Corner!
Please enter your first name:
Raju
Raju, what type of cookie would you like to order?
1. Sugar - $0. 25
2. Chocolate Chip - $0.35
3. Peanut Butter - $0.35
1
You selected a SUGAR cookie. How many cookies would you like?
4
Raju, your cost for this item is $1.00.
Would you like to order another type of cookie? yes
Raju, what type of cookie would you like to order?
1. Sugar - $0. 25
2. Chocolate Chip - $0.35
3. Peanut Butter - $0.35
2
You selected a CHOCLATE cookie. How many cookies would you
like?
3
Raju, your cost for this item is $1.05.
Would you like to order another type of cookie? yes
Raju, what type of cookie would you like to order?
1. Sugar - $0. 25
2. Chocolate Chip - $0.35
3. Peanut Butter - $0.35
3
You selected a PEANUT BUTTER cookie. How many cookies would you
like?
5
Raju, your cost for this item is $1.75.
Would you like to order another type of cookie? yes
Raju, what type of cookie would you like to order?
1. Sugar - $0. 25
2. Chocolate Chip - $0.35
3. Peanut Butter - $0.35
4
That's a wrong choice. Please try again..
Would you like to order another type of cookie? No
Raju, your total cost is $3.80
Thanks for ordering
================================================