Question

In: Computer Science

I need to take the code i already have and change it to have at least...

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

Solutions

Expert Solution

//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


Related Solutions

JAVA CODE BEGINNERS, I already have the DEMO CLASS(NEED YOU TO USE), I need you to...
JAVA CODE BEGINNERS, I already have the DEMO CLASS(NEED YOU TO USE), I need you to use all methods, also switch statements. Write a Temperature class. The class will have three conversion methods: toCelsius(), toKelvin() and toFahrenheit(). These methods will return a Temperature in those three scales equal to the this temperature. Note that the value of this is not changed in these conversions. In addition to these three conversion methods the class will have methods add(Temperature), subtract(Temperature), multiply(Temperature), and...
I NEED AN ESSAY OF 1000 WORDS AT LEAST BECAUSE I ALREADY POSTED THIS QUESTOIN I...
I NEED AN ESSAY OF 1000 WORDS AT LEAST BECAUSE I ALREADY POSTED THIS QUESTOIN I GOT SHORT ANSWER .PLEASE HELP Using the human resource department for recruitment, training, development and retaining employees can give any organization a competitive advantage. Some organizations became very successful in their markets by establishing an employee centered culture. Discuss how HRM activities enable organizations to become more competitive in their markets. Use an example of a well known organization that had been successful. In...
I wrote this code and just realized I need to put it into at least 6...
I wrote this code and just realized I need to put it into at least 6 different functions and I don't know how. No specific ones but recommended is: Read Data, Calculate Installation Price, Calculate Subtotal, Calculate Total, Print -> 1) Print Measurements & 2) Print Charges. Can somebody help? #include <stdio.h> // Function Declarations int length, width, area, discount; int main () { // Local Declarations double price, cost, charge, laborCharge, installed, amtDiscount, subtotal, amtTax, total; const double tax...
I already have the code of this program, I just want to know how to fix...
I already have the code of this program, I just want to know how to fix the code to Implement the 5th function (System.nanotime() and System.currentTimeMillis() methods) What Code does: Introduction Searching is a fundamental operation of computer applications and can be performed using either the inefficient linear search algorithm with a time complexity of O (n) or by using the more efficient binary search algorithm with a time complexity of O (log n). Task Requirements In this lab, you...
JAVA CODE BEGINNERS, I already have the demo code included Write a Bottle class. The Bottle...
JAVA CODE BEGINNERS, I already have the demo code included Write a Bottle class. The Bottle will have one private int that represents the countable value in the Bottle. Please use one of these names: cookies, marbles, M&Ms, pennies, nickels, dimes or pebbles. The class has these 14 methods: read()(please use a while loop to prompt for an acceptable value), set(int), set(Bottle), get(), (returns the value stored in Bottle), add(Bottle), subtract(Bottle), multiply(Bottle), divide(Bottle), add(int), subtract(int), multiply(int), divide(int), equals(Bottle), and toString()(toString()...
JAVA CODE BEGINNERS, I already have the demo code included Write a Bottle class. The Bottle...
JAVA CODE BEGINNERS, I already have the demo code included Write a Bottle class. The Bottle will have one private int that represents the countable value in the Bottle. Please use one of these names: cookies, marbles, M&Ms, pennies, nickels, dimes or pebbles. The class has these 14 methods: read()(please use a while loop to prompt for an acceptable value), set(int), set(Bottle), get(), (returns the value stored in Bottle), add(Bottle), subtract(Bottle), multiply(Bottle), divide(Bottle), add(int), subtract(int), multiply(int), divide(int), equals(Bottle), and toString()(toString()...
I have already created the journal entries for the following information but I need the t-accounts,...
I have already created the journal entries for the following information but I need the t-accounts, income statement, statement of retained earning and balance sheet: The following transactions occurred during 2018 (the company uses a perpetual inventory system with FIFO): 1)      Jan 4 Stockholders invested an additional $10,000 cash in the business in exchange for common stock 2)      Jan 4 Purchased 20 rabbits at $50 each on account from Jelly Bean Farms. 3)      Jan 4 Established a $200 petty change...
explain your answer step by step, i already have the answer but i need the explnations...
explain your answer step by step, i already have the answer but i need the explnations and type your answer, thanks 4 FeS2   +   11 O2    ®    2 Fe2O3    +    8 SO2 1.    How many moles of oxygen are required to react with 3.75 moles of FeS2?        2.    How many moles of Fe2O3 are formed when 2.83 moles of oxygen reacts with        excess FeS2? 3.    How many moles of SO2 are formed when 2.81 moles of Fe2O3 are...
These only need to be short essay answers, I have answered a few already. This is...
These only need to be short essay answers, I have answered a few already. This is due oct. 31st at 11:59 pm ESSAY: RECEIVABLES: Define Accounts Receivable. Accounts Receivable is an account where businesses can keep track of services they have pervaded without the payment from a client. For example, hospitals would use this when journaling the action when a patient is treated without payment. Account Receivables are journaled as increasing with a debit. All of #2 essay questions follow...
INSTRUCTIONS: I HAVE ALREADY ANSWERED QUESTION 1 AND 2. I NEED ASSISTANCE WITH QUESTIONS 3 AND...
INSTRUCTIONS: I HAVE ALREADY ANSWERED QUESTION 1 AND 2. I NEED ASSISTANCE WITH QUESTIONS 3 AND 4. I HAVE FILLED OUT THE PERCENTAGE CHANGE FOR QUESTION 3, AND NEED HELP ON CALCULATING THE OPERATING, INVESTING, AND FINANCIAL SECTIONS. AS WELL AS, THE EQUATIONS FOR QUESTION 4. IF YOU CAN ANSWER QUESTIONS 3 & 4 I WILL AWARD CREDIT. Question 1: Common size for income statement Income Statement (Common Size) :                                                                  Consolidated Income Statement 2011 % 2010 % Revenue $19,176.1...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT