1

This code is designed to take an order, add that to the total_price variable, apply discounts based on the total price, then add a tip and echo all the information back to the user.

  1. For some reason when I run the code, it isn't taking input from the user. I think it's related to the while statement but it outputs that my total_price is 0 after entering integers.

  2. The tip calculation at the bottom isn't working correctly. It prompts the user to enter a tip value, but then skips to the end and says the final total is 0, without the user being able to enter any tip.

Thanks so much for the help!!

#include <iostream>
using namespace std;

int main()
{
    int total_price{0}; // This variable will contain the price for all orders
    int tip{0};
    int discount_total{0};
    int tip_total = tip + discount_total;

    cout << "Welcome!\nThis program is designed to help take your order.\n";
    cout << "Our discounts availible today are: \n10 percent off orders over $50, and 5 percent off orders between $25 and $50.\n";

    // This is where the user is prompted to enter the price of their order
    cout << "Please enter the price of your item: ";
    cin >> total_price;

    // No negative numbers will be accepted
    if (total_price <= 0)
    {
        cout << "Invalid number, please re-enter the price of your item: ";
    }
    // User can continue ordering unless typing No
    while (total_price > 0)
    {
        cout << "Is there anything else? If not type No: ";
        cin >> total_price;
    }
    // Once the user types No, it brings them to the tip section
    // Marks the end of the order
    if ("No")
    {
        cout << "Thank you. Your total price is " << total_price << endl;
    }

    // Discount modifications
    if (total_price >= 50)
    {
        discount_total = total_price * .05;
        cout << "Your new total is " << discount_total << " with the 10 percent   discount.\n";
    }

    else if (total_price >= 25 && total_price <= 50)
    {
        discount_total = total_price * .05;
        cout << "Your new total is " << total_price << " with the 5 percent discount.\n";
    }

    else
    {
        total_price = discount_total;
        cout << "Your total is the same, " << total_price << "\n";
    }

    // Tip calculation
    cout << " Feel free to add a tip! Please enter here: ";
    cin >> tip;

    if (tip > 0)
    {
        cout << "Your final total is: " << tip_total << " dollars";
    }
    else if (tip < 0)
    {
        cout << "Your tip is invalid, please enter a valid tip: ";
    }

    return 0;
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
mathwizz
  • 21
  • 2
  • 2
    This is an optimal time to learn to use your debugger. What development environment are you using so that we can point you in the right direction? – JohnFilleau Jan 20 '22 at 20:38
  • 4
    `if ("No")` -- well, that will always be true. – Fred Larson Jan 20 '22 at 20:41
  • Out of curiosity, was this code given to you as-is from your instructor? Or have you written some of this? If this was given as-is, they're setting you up for failure expecting you to accept numbers or words as input, and requiring you to differentiate between the two. – JohnFilleau Jan 20 '22 at 20:42
  • 3
    "_Is there anything else? If not type No:_" and then you read the user input into an `int` - You can't store `No` in an `int` – Ted Lyngmo Jan 20 '22 at 20:42
  • Test the IO. If `cin >> total_price;` fails the program will never recover. On failure you need to `clear` the error flag and then remove the bad input (`ignore` it or read it into a `string`) so that it doesn't get parsed, and fail, again. – user4581301 Jan 20 '22 at 20:43
  • Reformatted to properly show scope, just look at your `while(total_price > 0)` loop and explain to your [rubber duck](https://en.wikipedia.org/wiki/Rubber_duck_debugging) what that thing is supposedly trying to do. And what do you supposed happens to the value of `total_price` when, in response to `cin >> total_price;` you type `No` (e.g. clearly *not* a valid `int`)? – WhozCraig Jan 20 '22 at 20:43
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Drew Dormann Jan 20 '22 at 20:44
  • @JohnFilleau I am using VS code. I haven't tried the debugger feature yet. And no I wrote all of this – mathwizz Jan 20 '22 at 20:44
  • 1
    Try it out. The debugger is one of the best best programmer productivity tools, probably only behind the optimizing compiler, you're ever likely to find. The debugger allows you to run the program at your speed and watch what the program does as it does it. When you see the program do something you didn't expect, you just found a bug. You might not know how to fix it yet, but knowing where it is is the perfect starting place. – user4581301 Jan 20 '22 at 20:46
  • @WhozCraig Ohhhh I see what you mean. In my head I wanted to have the user type "No" when they were done with their order but that wouldn't work. – mathwizz Jan 20 '22 at 20:47
  • `run > start debugging > C++/Windows` and it should pop up with an editable json file. Fill in the boilerplate to get started. – JohnFilleau Jan 20 '22 at 20:47
  • 1
    Programming without a debugger is like electrical engineering without a multimeter. Definitely get in the habit! – JohnFilleau Jan 20 '22 at 20:50
  • 1
    @EthanHyde nobody writes completely correct code. Learning to step through your code in a debugger while observing what changes will elevate you beyond just noticing that the code is wrong "for some reason". – Drew Dormann Jan 20 '22 at 20:51
  • You as a programmer will make assumptions while writing code, and these assumptions will carry through to your reasoning about the code when trying to find errors in the code. The debugger won't respect your assumptions; it will show you exactly what the compiler made of the code in oft-humbling detail. In the difference between the behaviour you believe you told the compiler you wanted and the behaviour you told the compiler you wanted lurks the bug. – user4581301 Jan 20 '22 at 21:11
  • BTW, if you input a negative price, your program keeps going. Try it out. – Thomas Matthews Jan 20 '22 at 21:21

0 Answers0