Program Not Computing Correctly

I have written the following program but am having two small problems with it. The first problem is that after I enter -1 for the product to end the program I have to also enter -1 quantity and would like the program to end after I end -1 for the product. Also it is not computing correctly...for instance if I type in the combos:

Product 1: 4
Product 2: 1
Product 3: 1
Product 4: 1
Product 5: 1
Product 1: 2
Product 4: 5

it is not properly computing the second entries for Products 1 and 4. I have played around with it and moved things around but cannot seem to figure out the correct combination to make the program execute correctly.

Any help would be much appreciated!!

#include <iostream>
using namespace std;

int main()
{
int product = 0;
int quantity = 0;
double total = 0.0;
double price1 = 0.0;
double price2 = 0.0;
double price3 = 0.0;
double price4 = 0.0;
double price5 = 0.0;

while( product != -1)
{
cout << "Enter product number: " << endl;
cin >> product;
cout << "Enter quantitiy: " << endl;
cin >> quantity;

if(quantity > 0)
{
quantity = quantity + 1;
}



switch (product)
{
case 1:
price1 = 2.98 * quantity;
break;
case 2:
price2 = 4.50 * quantity;
break;
case 3:
price3 = 9.98 * quantity;
break;
case 4:
price4 = 4.49 * quantity;
break;
case 5:
price5 = 6.87 * quantity;
break;

}
}



total = price1 + price2 + price3 + price4 + price5;
cout << "The total is: " << total << endl;

system("pause");
return 0;

}
Hi - please use code tags!

The first problem is that after I enter -1 for the product to end the program I have to also enter -1 quantity and would like the program to end after I end -1 for the product.


There are several ways of doing this - here's one.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool ok = true;
while ( ok )
{
    cout << "Enter product number: " << endl;
    cin >> product;
    if ( product < 0 )
    { 
        ok = false;
    }
    else
    {
        // get quantity, do switch, etc.
    } // if ... else
} // while 


In your switch statement you assign a new value to price1, price2, etc. each time, so the old values get overwritten. That's why in your example the last two products don't add up properly - it's only the last two entries that are registered. Try using += instead. For example:

1
2
3
4
5
switch ( product )
{
    case 1:
        price1 += 2.98 * quantity;
        // etc. 


Also it's a VERY VERY VERY bad idea to store prices as doubles - they should be ints :)
To further clarify why using doubles is bad for money, it's because doubles aren't precise. When you add 0.1 to 0.1, you don't get 0.2, because there is no double representation for 0.2, or even 0.1. In some cases, this is acceptable, in other cases, such as money, it makes for very frustrated accountants. Instead of counting in dollars, count in pennies.

As for leaving the loop immediatly for the sentinal value, check for -1 immediatly after getting the input, and if it is, use break.
Topic archived. No new replies allowed.