Question

In: Computer Science

Write a C++ program that allows a user choose item to purchase from a list; ask...

  1. Write a C++ program that allows a user choose item to purchase from a list; ask for the amount to calculate the cost of the purchase; offer to add a tip; add a delivery fee based on the subtotal; then calculate the total amount that the user needs to pay.

    ----------------------------------

    ----- GROCERY SHOPPING ITEMS -----

  2. Milk     - $5.99 / gallon
  3. Egg      - $6.99 / dozen
  4. Cheese   – $10.98 / 8oz
  5. Pasta    – $2.75 / packet
  6. ----------------------------------

    Other Values to be used in the program.

    TIP (Optional) : $5.00

    SHIP_RATE1 (for $20 or below) : $0.00

    SHIP_RATE2 (for between $20 and $35): 4.35

    SHIP_RATE3 (for above #35) : 7.65

    Follow the Steps Below

  7. The grocery list items and their prices are given above. You need to print it in the same format in your program. Use formatting output feature of the cout.
  8. Ask the user to choose one of the items. Use the switch statement to evaluate user’s selection.
  9. Ask the user to enter quantity of the chosen item. Then calculate the cost of the purchase.
  10. Ask the user if he/she wants to pay a $5 tip. If the answer Yes, then add $5 to the cost; otherwise, keep the cost as it is. Use conditional formatting to code this.
  11. Chek the subtotal. If it is less than or equal to $20.00, the shipping is free. If it is greater than $20.00 and less than or equal to $35.00, the shipping is $4.35. If it is greater than $35, then the shipping is $7.65. Based on the decision here calculate the total price that the user needs to pay.
  12. Format the total price in fixed-point notation, with two decimal places of precision, and be sure the decimal point is always displayed.
  13. Use functions to divide your program into manageable pieces. Define three functions: displayMenu, tipCalculator, and shippingCalculator.
    1. Write function prototypes at the top and define the functions after the main function.
  14. displayMenu function:
    1. Move the code that displays the menu in the program into the function.
    2. Whenever you need to display the menu, call the function.
  15. tipCalculator function:
    1. Move the code that asks users if they want to add a tip and calculate the subtotal accordingly, into the function.
    2. Call the function in an appropriate place in the main menu. Pass the value of subtotal to the function when calling. Return the value of new subtotal from the function.
    3. Write appropriate return type and parameter list for the function.
      Optional: You may ask the user how much money they would like to tip.
  16. shippingCalculator function:
    1. Move the code that calculates the total including a shipping rate based on the subtotal, into the function.
    2. Call the function in an appropriate place in the main menu. Pass the value of subtotal to the function when calling. Return the value of shipping added total from the function.
    3. Write appropriate return type and parameter list for the function.
  17. Define constants as global constants.
  18. Keep variable definitions local.
  19. At the beginning of the program display the menu, then ask the user if they want to give an order.
  20. If the answer is no, exit the program with the exit() function.
  21. Use while loop when the answer is yes in order to ask user if they want to give another order. Then, you can go through the process again and receive a new order. The subtotal will be accumulator of the while loop that keeps adding the cost of the new orders.
    The program should let user to give another order until they enter “no”.
  22. Use do-while loop to check if the user’s item choice is valid. Display menu and ask user to make a valid choice until their choice is valid. If their choice is invalid, display a warning message. Once the choice is valid, you can terminate the loop and continue with the switch statement. (do-while loop will be used inside the while loop.)

Solutions

Expert Solution

Refer to the below code for the solution:

#include<bits/stdc++.h>
#include <iostream>
using namespace std;
 
// function declaration
string displayMenu();
float tipCalculator(float cost);
float shippingCalculator(float cost);

//declare global variable
const float m = 5.99;
const float e = 6.99;
const float c = 10.98;
const float p = 2.75;
const float SHIP_RATE1 = 0;
const float SHIP_RATE2 = 4.35;
const float SHIP_RATE3 = 7.65;

//enum and map for items
enum level {milk, egg, cheese, pasta};
map<string, level> items;
void register_items()
{
    items["milk"]   = milk;
    items["egg"] = egg;
    items["cheese"]   = cheese;
        items["pasta"] = pasta;
}

//function to change the case of a string
string change_case(string sl)
{
    transform(sl.begin(), sl.end(), sl.begin(), ::tolower);
    return sl;
}


int main () {
   // local variable declaration:
   string order;
   string input;
   float cost = 0;
   int quantity = 0;
   register_items();
   
   //fixed point notation for 2 decimal place precision
   std::cout.precision(2);

   //call function to display Menu
   order = displayMenu();
   while (!change_case(order).compare("yes"))
   {
       do{
           cout << "Enter the name of the item from the above list - ";
           cin >> input;
           input = change_case(input);
           switch(items[input])
           {
               case milk:
                    cost = cost + m;
                    break;
               case egg:
                    cost = cost + e;
                    break;
               case cheese:
                    cost = cost + c;
                    break;
               case pasta:
                    cost = cost + p;
                    break;
               default:
                    cout<<"Invalid input. Try again";
            }
         }while ( !(input.compare("milk") || input.compare("egg") || 
                  input.compare("cheese") || input.compare("pasta")));
         
        cout<<"Enter the quantity - ";
        cin>>quantity;
        cost = quantity * cost;
        order = displayMenu();
   }
   
    // calling a function to get cost
    if(cost != 0){
    cost = tipCalculator(cost);
    cost = shippingCalculator(cost);
    std::cout << "Total cost = " << std::fixed;
    std::cout <<cost;
    }
    return 0;
}

// function displaying menu
string displayMenu() {
   // local variable declaration
   string order;
 
   cout<<"----------------------------------\n";
   cout<<"----- GROCERY SHOPPING ITEMS -----\n";
   cout<<"Milk     - $5.99 / gallon\n";
   cout<<"Egg      - $6.99 / dozen\n";
   cout<<"Cheese   – $10.98 / 8oz\n";
   cout<<"Pasta    – $2.75 / packet\n";
   cout<<"----------------------------------\n\n";
   cout<<"Do you want to give a order? ";
   cin >> order;
   return order;
}

// function adding tip
float tipCalculator(float cost){
    
    string tip;
    cout<<"Do you want to give a tip? ";
    cin>>tip;
    if(!change_case(tip).compare("yes"))
    {
        cost = cost + 5;
    }
    return cost;
}

//function to calculate shipping
float shippingCalculator(float cost){
    
    if(cost<=20){
        cost = cost + SHIP_RATE1;
    }
    else if(cost>20 && cost<=35){
        cost = cost + SHIP_RATE2 ;
    }
    else if(cost>35){
        cost = cost + SHIP_RATE3 ;
    }
    return cost;

}

Snippet of running code:

OUTPUT 1:

OUTPUT 2:

OUTPUT 3:


Related Solutions

write a complete C++ program that allows the user to choose from one of several options....
write a complete C++ program that allows the user to choose from one of several options. The program will display a menu and each option will be in a separate function. The program will continue looping until the user chooses to quit. The module descriptions are given below. a. Prompt for and take as input 3 floating point values (l, w, and h). The program will then calculate the volume (volume = l*h*w ). The program should then output to...
Write a program that will ask the user to enter the amount of a purchase. The...
Write a program that will ask the user to enter the amount of a purchase. The program should then compute the state and county sales tax. Assume the state sales tax is 5 percent and the county sales tax is 2.5 percent. The program should display the amount of the purchase, the state sales tax, the county sales tax, the total sales tax, and the total of the sale (which is the sum of the amount of purchase plus the...
Create a program that will ask the user to choose their order from a simple menu....
Create a program that will ask the user to choose their order from a simple menu. Customers first choose from three types of dishes: Sandwiches/wraps, Rice Meals, or Noodles. They can type in 1, 2, or 3 to select the type then, they choose a specific dish as shown below. Confirm their order by displaying it on the screen after they make their selection. 1. Sandwiches/wraps Hamburger Chicken shawarma 2. Rice meals Arroz con pollo Chana masala 3. Noodles Chow...
C++ Vector Write a program that allows the user to enter the last names of the...
C++ Vector Write a program that allows the user to enter the last names of the candidates in a local election and the votes received by each candidate. The program should then output each candidate's name, votes received by that candidate, and the percentage of the total votes received by the candidate. Assume a user enters a candidate's name more than once and assume that two or more candidates receive the same number of votes. Your program should output the...
Write a program in C, that uses standard input and output to ask the user to...
Write a program in C, that uses standard input and output to ask the user to enter a sentence of up to 50 characters, the ask the user for a number between 1 & 10. Count the number of characters in the sentence and multiple the number of characters by the input number and print out the answer. Code so far: char sentence[50]; int count = 0; int c; printf("\nEnter a sentence: "); fgets(sentence, 50, stdin); sscanf(sentence, %s;    for(c=0;...
Ask the user to input a series of numbers, write a C# program to output the...
Ask the user to input a series of numbers, write a C# program to output the sum, max, and min. Be sure to do error checking if the user input is not a number.
c++ Write a program that will ask the user for three pairs of integer values. The...
c++ Write a program that will ask the user for three pairs of integer values. The program will then display whether the first number of the pair is multiple of the second. The actual work of making the determination will be performed by a function called IsMultiple that takes two integer arguments (say, x and y). The function will return a Boolean result of whether x is a multiple of y.
In C: Write a complete program that performs the following task: Ask the user for the...
In C: Write a complete program that performs the following task: Ask the user for the number of sequences to display. For each sequence, Ask the user for a starting value Print out the value and double it (multiply by 2). Continue printing and doubling (all on the same line, separated by one space each) as long as the current number is less than 1000, or until 8 numbers have been printed on the line. You may assume that the...
Write a working C++ program to ask the user for a set of grades which are...
Write a working C++ program to ask the user for a set of grades which are to be stored in an array in increasing sequence. The user must first provide the number which represents the count of all grades to be provided. For example, if there will be 10 grades, the user is first prompted to provide the number 10. Subsequently, each grade is provided, one at a time. Allow for a maximum of 30 grades in defining the array....
Write a C# program to ask the user for an undetermined amount ofnumbers (until 0...
Write a C# program to ask the user for an undetermined amount of numbers (until 0 is entered) and display their sum.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT