0

Doing a loop having user enter two inputs which will calculate in a function. The program is suppose to continue to run until either a negative number is entered for the price or for the markup. A negative number for price or negative markup will never be sent to the calcRetail function.

My code works up until I enter a negative number for markup. The loop continues. What am I missing so that the loop ends not only when a negative is entered for price but also when a negative number is entered for markup?

double calcRetail(double x = 0.0, double y = 0.0)
{
    double retail = x * (1 + (y / 100));
    return retail;
}
int main()
{
    double price = 0.0, markup = 0.0;
    while(price >= 0)
    {
        cout << "Enter the wholesale price of the item:" << endl;
        cin >> price;
        if(price >= 0)
        {
            cout << "Enter the percent markup of the item:" << endl;
            cin >> markup;

            cout << "$" << calcRetail(price,markup) << endl;
        }
    }

    return 0;
}
user94559
  • 59,196
  • 6
  • 103
  • 103
Dip
  • 23
  • 2
  • 3
  • 2
    `if (price < 0) break;` right after you `cin` and `return 0` outside the `while` loop – PYA Jun 29 '17 at 16:54
  • 1
    Move `return 0;` outside of the loop. – πάντα ῥεῖ Jun 29 '17 at 16:55
  • 3
    The right tool to solve such problems is your debugger. You should step through your code line-by-line *before* asking on Stack Overflow. For more help, please read [How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). At a minimum, you should [edit] your question to include a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example that reproduces your problem, along with the observations you made in the debugger. – πάντα ῥεῖ Jun 29 '17 at 16:56
  • @πάνταῥεῖ That `return 0;` inside the loop was just bad code formatting. There was a right curly brace at the end of a line. (This was possibly my fault when I tried to fix the indentation.) – user94559 Jun 29 '17 at 16:58
  • @πάνταῥεῖ What? I'm saying the `return 0;` was *not* inside the loop. It just looked that way because of incorrect indentation. – user94559 Jun 29 '17 at 17:01

2 Answers2

1

breaks are frowned on by some developers. Something like this would be OK:

bool isLooping = true;
while (isLooping)
{
    cout << "Enter the wholesale price of the item:" << endl;
    cin >> price;

    if ( price >= 0 )
    {
        cout << "Enter the percent markup of the item:" << endl;
        cin >> markup;

        if (markup >= 0) cout << "$" << calcRetail(price,markup) << endl;
        else isLooping = false;
    }
    else isLooping = false;
}
buttonsrtoys
  • 2,359
  • 3
  • 32
  • 52
  • 1
    Your code asks for a markup even after a negative price has been entered. You've also dropped the calculation, which would have to go into an `else`. (Also, you want `||`, not `&&` in your condition to end the loop.) – user94559 Jun 29 '17 at 17:29
0

How about this?

while (true)
{
    cout << "Enter the wholesale price of the item:" << endl;
    cin >> price;

    if (price < 0) break;

    cout << "Enter the percent markup of the item:" << endl;
    cin >> markup;

    if (markup < 0) break;

    cout << "$" << calcRetail(price,markup) << endl;
}
user94559
  • 59,196
  • 6
  • 103
  • 103
  • That breaks for a neg price. He's looking to break for neg price & markup – buttonsrtoys Jun 29 '17 at 17:12
  • Yeah, but is shows the technique OP needs to use. Only thing I'd add is a warning and a link to [Why do I get an infinite loop if I enter a letter rather than a number?](https://stackoverflow.com/questions/19521320/why-do-i-get-an-infinite-loop-if-i-enter-a-letter-rather-than-a-number) – user4581301 Jun 29 '17 at 17:13
  • @buttonsrtoys This breaks on a negative price or a negative markup. I believe that was the requirement? – user94559 Jun 29 '17 at 17:26