non-stop looping

closed account (E85L3TCk)
I have some problem with my code, it will keep asking for number of product but it will not loop it to Day 2 until i enter an error product number. The below will be my question and code. i hope to get some help.

A shopkeeper wants to keep track of his daily sales and weekly sales. He gives discounts to his customer
depending on the weight purchased. Write a program for the shopkeeper to calculate his daily sales and
weekly sales (7 days). Allow the shopkeeper to enter the product number, the weight, and price. Let the
shopkeeper enter as many products as he wants in one day. Display the daily sales. Display the weekly sales
after 7 days. The following table shows the discounts given. Display an error message if invalid product
number is entered.

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
#include <iostream>
using namespace std;

int main()
{
    //declaration
    int days_in_week = 7;
    double daily_sales;
    double weekly_sales = 0;
    double weight;
    double discount_rate;
    double price;
    int product_num;


    for (int day = 1; day <= days_in_week; day++ )
    {
        cout << "Day" << day << endl;

        do
        {
        cout << "Product Number: ";
        cin >> product_num;

        if (product_num != 1001 && product_num != 1002 && product_num != 1003)
        {
        cout<<"Wrong Code, Please enter again : ";
        cin>> product_num;
        }

        cout << "Weight: ";
        cin >> weight;

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

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

        daily_sales = (price - discount_rate);
        weekly_sales = weekly_sales + daily_sales;

        cout << "Daily sales is: RM" << daily_sales << endl;
        } while (product_num != 1001 && product_num != 1003);

    }
        cout << "weekly Sales is: RM" << weekly_sales<< endl;

        return 0;
}


Thank you
Last edited on
Hi,

your code continues to loop bacause it only escapes if you enter an invalid
product number. You have to add query if another item should be added or not.
Else you don't really get around inputing an invalid number.

Here is how I would solve it.

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
#include <iostream>
using namespace std;

int main()
{
    //declaration
    int days_in_week = 7;
    double daily_sales;
    double weekly_sales = 0;
    double weight;
    double discount_rate;
    double price;
    int product_num;


    for (int day = 1; day <= days_in_week; day++ )
    {
        cout << "Day" << day << endl;

        do
        {
            cout << "Product Number: ";
            cin >> product_num;
    
            //changed to loop because invalid number 
            while (product_num != 1001 && product_num != 1002 && product_num != 1003)
            {
                cout<<"Wrong Code, Please enter again : ";
                cin>> product_num;
            }
    
            cout << "Weight: ";
            cin >> weight;
    
            cout << "price: ";
            cin >> price;
    
            if (product_num == 1001 && weight > 10)
                discount_rate = price * 0.10;
            else if (product_num == 1001 && weight <= 10)
                discount_rate = price * 0.05;
            else if (product_num == 1002 && weight > 10)
                discount_rate = price * 0.07;
            else if (product_num == 1002 && weight <= 10)
                discount_rate = price * 0.03;
            else if (product_num == 1003 && weight > 10)
                discount_rate = price * 0.25;
            else if (product_num == 1003 && weight <= 10)
                discount_rate = price * 0.10;
    
            daily_sales = (price - discount_rate);
            weekly_sales = weekly_sales + daily_sales;
    
            //query if another item should be added
            char query = ' ';
            cout << "Add another item[y/n]: ";
            cin >> query;
    
            if(query == 'y')
            {
                continue;
            }
            else
            {
                cout << "Daily sales is: RM" << daily_sales << endl;
                break;
            }
        } while (1);

    }
    cout << "weekly Sales is: RM" << weekly_sales<< endl;

    return 0;
}
Last edited on
Topic archived. No new replies allowed.