Wrong display Calculation

closed account (E85L3TCk)
Hi
Last edited on
The discrepancy happens whenever there is more than one item on the same day.
the weekly total gets accumulated multiple times (line 60).
 
weeksales = daysales + weeksales;

Move that line so it is executed just once at the end of each days input.
closed account (E85L3TCk)
c
Last edited on
DO NOT DOUBLE POST

http://www.cplusplus.com/forum/beginner/190042/
it wastes other people's time.
The problem is the calculation of weeksales on line 60. You get the sum of the daily sales plus the last sum of the daily sales and so on. Move line 60 to line 75.
Original post:

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.