Calculation error

closed account (E85L3TCk)
Hi,
Last edited on
I don't see an extra 9 anywhere, check out the names of your variable on line 12 compare with line 46.
Also I don't understand the RM in your cout line 76, and 84?

After correcting the variable name your program compiled and calculated correctly
You're only noticing extra 9 because only one of your days has more than one sale. If you had more sales per day the problem will be bigger.

1
2
            daysales = daysales + (price - discount);
            weeksales = daysales + weeksales;


each time through the daily loop, you add everything not just the current sale to the weekly sum.

You can solve this by moving the weeksales sum to the outer loop, or not accumulating it in the inner loop.

1
2
            daysales = daysales + (price - discount);
            weeksales = weeksales  + (price - discount);


or move line 60 to line 82.
DO NOT DOUBLE POST

http://www.cplusplus.com/forum/beginner/190069/
it wastes other people's time.
Original code for reference:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>

using namespace std;

int main()
{
    //declaration
    int daysinweek = 7;
    double daysales = 0;
    double weeksales= 0 ;
    double weight;
    double discountrate;
    double price;
    int productcode;




        for (int day = 1; day <= daysinweek; day++ )
        {
        daysales = 0;
        cout << "Day" << day << endl;
        cout<<endl;

        do
        {
            //enter product name
            cout << "Enter Product Number: ";
            cin >> productcode;

            //loop
            while (productcode != 1001 && productcode != 1002 && productcode != 1003)
            {

                cout<<"Wrong , Please enter again : ";
                cin>> productcode;
            }
            //enter weight and price
            cout << "Weight: ";
            cin >> weight;

            cout << "price: ";
            cin >> price;

            if (productcode == 1001 && weight > 10)
                discount = price * 0.10;
            else if (productcode == 1001 && weight <= 10)
                discount = price * 0.05;
            else if (productcode == 1002 && weight > 10)
                discount = price * 0.07;
            else if (productcode == 1002 && weight <= 10)
                discount = price * 0.03;
            else if (productcode == 1003 && weight > 10)
                discount = price * 0.25;
            else if (productcode == 1003 && weight <= 10)
                discount = price * 0.10;


            daysales = daysales + (price - discount);
            weeksales = daysales + weeksales;

            //add product
            cout<<endl;
            char add = ' ';
            cout << "Add another item[y/n]: ";
            cin >> add;
            cout<<endl;

            if(add == 'y')
            {
                continue;
            }
            else
            {

                cout << "Daily sales is: RM" << daysales << endl;
                cout<<endl;
                break;
            }

        } while (1);

    }
    cout << "Weekly Sales is: RM" << weeksales<< endl;

    return 0;
}
Topic archived. No new replies allowed.