In: Computer Science
I need to take the code i already have and change it to have at least one function in it. it has to include one function and one loop. I already have the loop but cant figure out how to add a function. I thought i could create a funciton to call to the totalCost but not sure how to do it. Help please.
#include
#include
//main function
int main(void)
{
char userName [20];
char yesOrNo [10];
float cookieCost=0;
float totalCost=0;
float cookiePrice;
//declerations
int cookieAmount;
int cookieChoice;
printf("Welcome to Candy Land Cafe!\n Please tell me
your name.\n");
scanf("%s", &userName);
while(1)
{
printf("%s, what type of cookie
would you like?\n", userName);
printf("1 for Sugar Cookie - $0.25\n2 for Chocolate
Chip - $0.35\n3 for Peanut Butter - $0.35\n");
//input cookie choice
scanf("%d", &cookieChoice);
if(cookieChoice == 1){
//user selected sugar cookie
printf("You selected a Sugar
Cookie. How many cookies would you like?\n");
scanf("%d", &cookieAmount);
//input
cookiePrice = 0.25;
//set cookie price
}
else if (cookieChoice == 2)
{ //user picked chocolate cookie
printf("You selected a Chocolate
Chip Cookie. How many would you like?\n");
scanf("%d", &cookieAmount);
//input
cookiePrice = 0.35;
//set cookie price
}
else if (cookieChoice == 3)
{ //user picked peanut cookie
printf("You selected a Peanut
Butter Cookie. How many would you like?\n");
scanf("%d", &cookieAmount);
//input
cookiePrice = 0.35;
//set cookie price
}
else
{
//did not make valid choice
printf("Invalid Choice, try
again...\n");
continue;
}
//calculate cost of cookies
cookieCost= cookieAmount * cookiePrice;
//add to total cost
totalCost= totalCost + cookieCost;
//cost of current cookie request
printf("%s, your cost is $%.2f.\n", userName,
cookieCost);
//ask for more cookies
printf("Would you like to order another cookie
type?\n");
scanf("%s", yesOrNo);
if(strcmp(yesOrNo, "no")==0) //if no, skip
to total cost and end
{
printf("%s, your total cost is
%.2f\n", userName, totalCost);
break; //end
}
}
//dispaly to user at end "Thanks"
printf("Thanks for ordering from Candy Land Cafe!\n");
system ("pause"); //hold window open
return 0; //success
} //end main
//Included two functions:
function1 : To compute amount of cookie based of cookie weight and price
function2 : to compute Total cost after chooing item from the list
#include<stdio.h>
#include<string.h>
float total(float totalcost,float cookiecost)//function to compute
total cookiecost
{
float sum; //declare sum
sum=totalcost+cookiecost; //compute sum for finding total
cost
return sum; //return the result
}
float Amount(float c_cost,float price)
{
float amount;//declare amount varibale
amount = c_cost*price; //compute cost
return amount; //return the computed result
}
//main function
int main(void)
{
char userName [20];
char yesOrNo [10];
float cookieCost=0;
float totalCost=0;
float cookiePrice; //declerations
int cookieAmount;
int cookieChoice;
printf("Welcome to Candy Land Cafe!\n Please tell me your
name.\n");
scanf("%s", userName);
while(1)
{
printf("%s, what type of cookie would you like?\n",
userName);
printf("1 for Sugar Cookie - $0.25\n2 for Chocolate Chip - $0.35\n3
for Peanut Butter - $0.35\n");
//input cookie choice
scanf("%d", &cookieChoice);
if(cookieChoice == 1){
//user selected sugar cookie
printf("You selected a Sugar Cookie. How many cookies would you
like?\n");
scanf("%d", &cookieAmount); //input
cookiePrice = 0.25; //set cookie price
}
else if (cookieChoice == 2)
{ //user picked chocolate cookie
printf("You selected a Chocolate Chip Cookie. How many would you
like?\n");
scanf("%d", &cookieAmount); //input
cookiePrice = 0.35; //set cookie price
}
else if (cookieChoice == 3)
{ //user picked peanut cookie
printf("You selected a Peanut Butter Cookie. How many would you
like?\n");
scanf("%d", &cookieAmount); //input
cookiePrice = 0.35; //set cookie price
}
else
{
//did not make valid choice
printf("Invalid Choice, try again...\n");
continue;
}
//calculate cost of cookies
cookieCost= Amount(cookieAmount,cookiePrice); //function to compute
cookie cost
//add to total cost
totalCost= total(totalCost,cookieCost);//function to compute total
cost
//cost of current cookie request
printf("%s, your cost is $%.2f.\n", userName, cookieCost);
//ask for more cookies
printf("Would you like to order another cookie type , type : no to
stop :?\n");
scanf("%s", yesOrNo);
if(strcmp(yesOrNo, "no")==0) //if no, skip to total cost and
end
{
printf("%s, your total cost is %.2f\n", userName, totalCost);
break; //end
}
}
//dispaly to user at end "Thanks"
printf("Thanks for ordering from Candy Land Cafe!\n");
system ("pause"); //hold window open
return 0; //success
} //end main