2
\$\begingroup\$

This is for my intro level C++ class. This is the assignment:

Write a program that displays a takeout menu from which a user can place an order. The program begins by displaying the menu and prompting the user to enter a choice. If the user enters 1, 2, or 3, the program responds by asking for quantity (of pizzas, sides, etc.). If the user enters 4, the program thanks the user for ordering if s/he placed an order, or simply for visiting if s/he did not. If the user enters a number outside the range 1 – 4, the program prints an error message, then displays the menu again. The program "loops" until the user selects 4 to exit the menu. Finally, the program displays a summary of the user's order if s/he did indeed place one. Your program output should resemble the sample runs at the end of this document.

Instructions:

  • You must use a switch statement to classify the user’s menu choice (1, 2, etc.).
  • I recommend using a do-while loop to repeat (i.e., "loop") the program, but the choice is yours.

Extra Credit Options:

  • (4 points, max.) Assign prices to the pizza(s), side(s), and drink(s). Include price per item and total cost in the order summary. (Sales tax is optional.)

  • (6 points, max.) Add submenus to allow the user to order different kinds of pizzas, drinks, etc.

How can I improve this?

#include <iostream>

#include <string>

#include <iomanip>

int main(){

    std::string divider = "******";

    int pizza, side, drink, exit, choice, pizzaNum = 0, sideNum = 0, drinkNum = 0;

    double pizzaPrice = 11.99, sidePrice = 4.99, drinkPrice = 1.99, salesTax = 0.06, total;

    std::cout << divider;

    std::cout << "\n\nTakeout Menu"

    << "\n\n1) Order pizza ($11.99ea)"

    << "\n2) Order side ($4.99ea)"

    << "\n3) Order drink ($1.99ea)"

    << "\n4) Exit menu";

    std::cout << "\n\nYour choice (1-4)? ";

    std::cin >> choice;

    std::cout << "\n";

    std::cout << divider;

    do {

    switch (choice){

    case 1:

    int peperoni, cheese, supreme, meatlovers, pizzaChoice;

    std::cout << "\n\n1) Peporoni"

    << "\n2) Cheese"

    << "\n3) Supreme"

    << "\n4) Meatlovers";

    std::cout << "\n\nWhich type of pizza? ";

    std::cin >> pizzaChoice;

    switch (pizzaChoice){

    case 1:

    std::cout << "\nHow many peperoni pizzas? ";

    std::cin >> pizzaNum;

    std::cout << "\n" << divider;

    break;

    case 2:

    std::cout << "\nHow many cheese pizzas? ";

    std::cin >> pizzaNum;

    std::cout << "\n" << divider;

    break;

    case 3:

    std::cout << "\nHow many supreme pizzas? ";

    std::cin >> pizzaNum;

    std::cout << "\n" << divider;

    break;

    case 4:

    std::cout << "\nHow many meatlovers pizzas? ";

    std::cin >> pizzaNum;

    std::cout << "\n" << divider;

    break;

    default:

    std::cout << pizzaNum << " is not a valid character!";

    int peperoni, cheese, supreme, meatlovers, pizzaChoice;

    std::cout << "\n\n1) Peporoni"

    << "\n2) Cheese"

    << "\n3) Supreme"

    << "\n4) Meatlovers";

    std::cout << "\n\nWhich type of pizza? ";

    std::cin >> pizzaChoice;

    std::cout << "\n" << divider;

    break;

    }

    std::cout << "\n\nTakeout Menu"

    << "\n\n1) Order pizza ($11.99ea)"

    << "\n2) Order side ($4.99ea)"

    << "\n3) Order drink ($1.99ea)"

    << "\n4) Exit menu";

    std::cout << "\n\nYour choice (1-4)? ";

    std::cin >> choice;

    std::cout << "\n";

    std::cout << divider;

    break;

    case 2:

    int coleslaw, fries, salad, breadsticks, sideChoice;

    std::cout << "\n\n1) Coleslaw (1 cup)"

    << "\n2) Fries (medium)"

    << "\n3) Salad (medium bowl)"

    << "\n4) Breadsticks (4)";

    std::cout << "\n\nWhich type of side? ";

    std::cin >> sideChoice;

    switch (sideChoice){

    case 1:

    std::cout << "\nHow many cups of coleslaw? ";

    std::cin >> sideNum;

    std::cout << "\n" << divider;

    break;

    case 2:

    std::cout << "\nHow many fries? ";

    std::cin >> sideNum;

    std::cout << "\n" << divider;

    break;

    case 3:

    std::cout << "\nHow many bowls of salad? ";

    std::cin >> sideNum;

    std::cout << "\n" << divider;

    break;

    case 4:

    std::cout << "\nHow many breadsticks? ";

    std::cin >> sideNum;

    std::cout << "\n" << divider;

    break;

    default:

    std::cout << sideNum << " is not a valid character!";

    int peperoni, cheese, supreme, meatlovers, pizzaChoice;

    std::cout << "\n\n1) Coleslaw (1 cup)"

    << "\n2) Fries (medium)"

    << "\n3) Salad (medium bowl)"

    << "\n4) Breadsticks (4)";

    std::cout << "\n\nWhich type of side? ";

    std::cin >> sideChoice;

    std::cout << "\n" << divider;

    break;

    }

    std::cout << "\n\nTakeout Menu"

    << "\n\n1) Order pizza ($11.99ea)"

    << "\n2) Order side ($4.99ea)"

    << "\n3) Order drink ($1.99ea)"

    << "\n4) Exit menu";

    std::cout << "\n\nYour choice (1-4)? ";

    std::cin >> choice;

    std::cout << "\n";

    std::cout << divider;

    break;

    case 3:

    int coke, sprite, drpepper, lemonade, drinkChoice;

    std::cout << "\n\n1) Coke"

    << "\n2) Sprite"

    << "\n3) Dr. Pepper"

    << "\n4) Lemonade";

    std::cout << "\n\nWhich type of drink? ";

    std::cin >> drinkChoice;

    switch (drinkChoice){

    case 1:

    std::cout << "\nHow many Coke's? ";

    std::cin >> drinkNum;

    std::cout << "\n" << divider;

    break;

    case 2:

    std::cout << "\nHow many Sprite's? ";

    std::cin >> drinkNum;

    std::cout << "\n" << divider;

    break;

    case 3:

    std::cout << "\nHow many Dr. Pepper's? ";

    std::cin >> drinkNum;

    std::cout << "\n" << divider;

    break;

    case 4:

    std::cout << "\nHow many Lemonade's? ";

    std::cin >> drinkNum;

    std::cout << "\n" << divider;

    break;

    default:

    std::cout << drinkNum << " is not a valid character!";

    int coke, sprite, drpepper, lemonade, drinkChoice;

    std::cout << "\n\n1) Coke"

    << "\n2) Sprite"

    << "\n3) Dr. Pepper"

    << "\n4) Lemonade";

    std::cout << "\n\nWhich type of drink? ";

    std::cin >> drinkChoice;

    std::cout << "\n" << divider;

    break;

    }

    std::cout << "\n\nTakeout Menu"

    << "\n\n1) Order pizza ($11.99ea)"

    << "\n2) Order side ($4.99ea)"

    << "\n3) Order drink ($1.99ea)"

    << "\n4) Exit menu";

    std::cout << "\n\nYour choice (1-4)? ";

    std::cin >> choice;

    std::cout << "\n";

    std::cout << divider;

    break;

    case 4:

    std::cout << "\n\nFine, we don't want your business anyways!";

    break;

    default:

    std::cout << "\n\n" << choice << " is not a valid choice. Please enter 1-4.\n\n";

    std::cout << divider;

    std::cout << "\n\nTakeout Menu"

    << "\n\n1) Order pizza ($11.99ea)"

    << "\n2) Order side ($4.99ea)"

    << "\n3) Order drink ($1.99ea)"

    << "\n4) Exit menu";

    std::cout << "\n\nYour choice (1-4)? ";

    std::cin >> choice;

    std::cout << "\n";

    std::cout << divider;

    break;

    }

    } while (choice != 4);

    if (choice == 4){

    std::cout << "\n\nORDER SUMMARY\n";

    if (pizzaNum != 1){

    std::cout << "\n" << pizzaNum << " pizzas";

    }

    else {

    std::cout << "\n" << pizzaNum << " pizza";

    }

    if (sideNum != 1){

    std::cout << "\n" << sideNum << " sides";

    }

    else {

    std::cout << "\n" << sideNum << " side";

    }

    if (drinkNum != 1){

    std::cout << "\n" << drinkNum << " drinks\n\n";

    }

    else {

    std::cout << "\n" << drinkNum << " drink\n\n";

    }

    std::cout << "PRICE SUMMARY";

    pizzaPrice = pizzaPrice * pizzaNum;

    sidePrice = sidePrice * sideNum;

    drinkPrice = drinkPrice * drinkNum;

    total = (pizzaPrice + sidePrice + drinkPrice);

    salesTax = total * salesTax;

    std::cout << "\n\nPrice of items: $" << std::setprecision(2) << std::fixed << total;

    total = total + salesTax;

    std::cout << "\nTax of items: $" << std::setprecision(2) << std::fixed << salesTax;

    if (total > 100){

    std::cout << "\n------"

    << "\n|Total price: $" << std::setprecision(2) << std::fixed << total << "|\n"

    << "------\n\n";

    }

    else if (total > 10 && total < 100){

    std::cout << "\n------"

    << "\n|Total price: $" << std::setprecision(2) << std::fixed << total << "|\n"

    << "------\n\n";

    }

    else if (total < 10){

    std::cout << "\n------"

    << "\n|Total price: $" << std::setprecision(2) << std::fixed << total << "|\n"

    << "------\n\n";

    }

    }

    std::cout << divider << "\n\n";

}
\$\endgroup\$
  • 2
    \$\begingroup\$ I find this hard to read, as the indentation is wrong and everything is double-spaced. This is a lot of code to put into main(). It would be better to add classes or at least functions to hold some of the code. \$\endgroup\$ – Brythan Oct 17 '14 at 7:01
  • \$\begingroup\$ @Brythan this is a review and should be an answer and not a comment. It's okay to not review everything in an answer. You might want to avoid absolutes, though. Stuff like "your indentation is wrong" is frowned upon. \$\endgroup\$ – Vogel612 Oct 17 '14 at 7:16
  • \$\begingroup\$ @Brythan I haven't learned about classes yet. \$\endgroup\$ – user56284 Oct 17 '14 at 7:19
8
\$\begingroup\$

This would most likely be a nightmare to most C++ programmers. Your code contains nested switch statements, no functions, magic numbers, etc. It is very hard to read your code.

You should have at at least four functions here. One to take the customer's pizza order, another to take the side orders, and one for drinks. Then another to calculate the total. Call these functions from within your switch statement.

There are a ton of unused variables. You keep declaring these variables, in half of your switch statements needlessly:

int peperoni, cheese, supreme, meatlovers;

What for? Remove these.

Your code looks more like procedural programming than C++.

Here is what your switch statement should look like:

switch (choice){

    case 1:
        pizzaNum = PizzaOrder();
        break;
    case 2:
        sideNum = SideOrder();
        break;   
    case 3:   
        drinkNum = DrinksOrder();
        break;    
    case 4:
        calculateTotal(pizzaNum,sideNum,drinkNum);  
        done = true;  //Set true, exit while loop
        break;
    default:
             //Handle w/e exceptions come up
        break;
}

See how this is much cleaner? The self documenting function names allow a programmer to tell more or less what is happening in the switch statement at a glance.

Example for PizzaOrder():

int PizzaOrder(){

    int pizzaChoice;
    bool done = false;

    std::cout << "\n\n1) Peporoni"
              << "\n2) Cheese"
              << "\n3) Supreme"
              << "\n4) Meatlovers";

    std::cout << "\n\nWhich type of pizza? ";
    std::cin >> pizzaChoice;

    while(!done){

        switch (pizzaChoice){

        case 1:
        {

            std::cout << "\nHow many peperoni pizzas? ";
            std::cin >> pizzaNum;
            std::cout << "\n" << divider;
            done = true;
            break;
        }
        case 2:
        {
            std::cout << "\nHow many cheese pizzas? ";
            std::cin >> pizzaNum;
            std::cout << "\n" << divider;
            done = true;
            break;
        }
        case 3:
        {
            std::cout << "\nHow many supreme pizzas? ";
            std::cin >> pizzaNum;
            std::cout << "\n" << divider;
            done = true;
            break;
        }
        case 4:
        {
            std::cout << "\nHow many meatlovers pizzas? ";
            std::cin >> pizzaNum;
            std::cout << "\n" << divider;
            done = true;
            break;
        }
        default:
        {
            std::cout << pizzaNum << " is not a valid character!";
            break;
        }
        }
    }

    return pizzaNum;
}

The code may need its own review. I'm just helping you get started with writing your functions.

\$\endgroup\$
  • \$\begingroup\$ This does look a lot easier to read. Thanks for helping. :) \$\endgroup\$ – user56284 Oct 17 '14 at 7:29
  • \$\begingroup\$ Yeah, one thing that hits me right away is that you're not keeping track of which type of pizza they ordered, just how many. :) \$\endgroup\$ – GalacticCowboy Oct 17 '14 at 16:21
6
\$\begingroup\$

Indent your code, then post a follow up question. Right now, I have trouble understanding the structure of your code, which makes reviewing the code on a higher level hard.

Aside from indentation (see @EngieOP's answer for that), you should give semantic value to blank lines.

std::string divider = "******";

int pizza, side, drink, exit, choice, pizzaNum = 0, sideNum = 0, drinkNum = 0;

double pizzaPrice = 11.99, sidePrice = 4.99, drinkPrice = 1.99, salesTax = 0.06, total;

std::cout << divider;

std::cout << "\n\nTakeout Menu"

<< "\n\n1) Order pizza ($11.99ea)"

<< "\n2) Order side ($4.99ea)"

<< "\n3) Order drink ($1.99ea)"

<< "\n4) Exit menu";

std::cout << "\n\nYour choice (1-4)? ";

std::cin >> choice;

std::cout << "\n";

std::cout << divider;

First, let's strip the blank lines...

std::string divider = "******";
int pizza, side, drink, exit, choice, pizzaNum = 0, sideNum = 0, drinkNum = 0;
double pizzaPrice = 11.99, sidePrice = 4.99, drinkPrice = 1.99, salesTax = 0.06, total;
std::cout << divider;
std::cout << "\n\nTakeout Menu"
<< "\n\n1) Order pizza ($11.99ea)"
<< "\n2) Order side ($4.99ea)"
<< "\n3) Order drink ($1.99ea)"
<< "\n4) Exit menu";
std::cout << "\n\nYour choice (1-4)? ";
std::cin >> choice;
std::cout << "\n";
std::cout << divider;

And now lets add them back in where relevant.

First you have your declarations, that's one section. So put a blank line after that.

std::string divider = "******";
int pizza, side, drink, exit, choice, pizzaNum = 0, sideNum = 0, drinkNum = 0;
double pizzaPrice = 11.99, sidePrice = 4.99, drinkPrice = 1.99, salesTax = 0.06, total;

std::cout << divider;
std::cout << "\n\nTakeout Menu"
<< "\n\n1) Order pizza ($11.99ea)"
<< "\n2) Order side ($4.99ea)"
<< "\n3) Order drink ($1.99ea)"
<< "\n4) Exit menu";
std::cout << "\n\nYour choice (1-4)? ";
std::cin >> choice;
std::cout << "\n";
std::cout << divider;

Then you print something... it's a pretty large message, so that's a section too.

std::string divider = "******";
int pizza, side, drink, exit, choice, pizzaNum = 0, sideNum = 0, drinkNum = 0;
double pizzaPrice = 11.99, sidePrice = 4.99, drinkPrice = 1.99, salesTax = 0.06, total;

std::cout << divider;
std::cout << "\n\nTakeout Menu"
<< "\n\n1) Order pizza ($11.99ea)"
<< "\n2) Order side ($4.99ea)"
<< "\n3) Order drink ($1.99ea)"
<< "\n4) Exit menu";

std::cout << "\n\nYour choice (1-4)? ";
std::cin >> choice;
std::cout << "\n";
std::cout << divider;

After the menu, comes the choice. ... and it has a divider after that? Strange, I'd have printed the divider after the menu.

std::string divider = "******";
int pizza, side, drink, exit, choice, pizzaNum = 0, sideNum = 0, drinkNum = 0;
double pizzaPrice = 11.99, sidePrice = 4.99, drinkPrice = 1.99, salesTax = 0.06, total;

std::cout << divider;
std::cout << "\n\nTakeout Menu"
<< "\n\n1) Order pizza ($11.99ea)"
<< "\n2) Order side ($4.99ea)"
<< "\n3) Order drink ($1.99ea)"
<< "\n4) Exit menu";
std::cout << divider;

std::cout << "\n\nYour choice (1-4)? ";
std::cin >> choice;
std::cout << "\n";

And now we have three clear segments. Declarations, print menu, input choice... some of these segments might even be extracted into separate functions later.

By using blank lines properly, you split your code into logical segments that make it easier to understand. Use too much blank lines and it's. Like. Putting. A. Period. After. Every. Word. And then other programmers don't understand you anymore.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy