Checking for multiple values

Dec 22, 2016 at 9:09am
I'm practising my C++ by creating a virtual shop that sells items that would get you a discount if you were to pick 2 of the 6 items which are above $15. However, I'm stuck with a dilemma of how I am supposed to check the values of the six items more easily instead of nesting "ifs". Problem is with the payment and payment1 functions.

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <iostream>
#include <string>

using namespace std;

int numberofitems;
string retry;
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
int f = 0;
int x;
int fi = 16;
int se = 20;
int thi = 10;
int fo = 14;
int fif = 15;
int six = 10;


int payment1()
{
    int sum = 0;
    if (a==1 || b==1 || c==1 || d==1 || e==1 || f==1)
    {
        sum = sum + fi

        while (a==2 || b==2 || c==2 || d==2 || e==2 || f==2)
        {
            sum = fi + se
        }

    }
}

int payment()
{
    x = 1;
    if (x <= numberofitems)
    {
    cout << "Please indicate the number of the first item you would like to purchase\n";
    cin >> a;
    x = x+1;


        if (x <= numberofitems)
        {
        cout << "Please indicate the number of the second item you would like to purchase\n";
        cin >> b;
        x = x+1;


            if (x <= numberofitems)
            {
            cout << "Please indicate the number of the third item you would like to purchase\n";
            cin >> c;
            x = x+1;


                if (x <= numberofitems)
                {
                cout << "Please indicate the number of the fourth item you would like to purchase\n";
                cin >> d;
                x = x+1;


                    if (x <= numberofitems)
                    {
                    cout << "Please indicate the number of the fifth item you would like to purchase\n";
                    cin >> e;
                    x = x+1;


                        if (x <= numberofitems)
                        {
                        cout << "Please indicate the number of the sixth item you would like to purchase\n";
                        cin >> f;
                        x = x+1;
                        }
                        else {payment1();}
                    }
                    else {payment1();}
                }
                else {payment1();}
            }
            else {payment1();}
        }
        else {payment1();}
    }
}

int main()
{
cout << "Welcome to our shop \n";
cout << "Please choose what you may wish to buy \n";
cout << "1. Charmander Plushie($16)\n2. Barney($20)\n3. Bulbasaur Plushie($10)\n4. Squirtle Plushie($14)\n5. Figurine($15)\n6. Plastic Memories($10)\n";
cout << "Please indicate how many items you would wish to buy\n";
cin >> numberofitems;
if (numberofitems >= 3)
{
    cout << "You are eligible for a discount of 10% if 2 of your items exceeds $15" << endl;
    payment();
}
else
{
    cout << "You are not eligible for a discount\n A discount is only available if you purchase 3 or more items\n";
    cout << "Would you still want to continue with the purchase?\n";
    cin >> retry;
    while (retry != "Yes" && retry != "yes" && retry != "y" && retry != "No" && retry != "no" && retry != "n")
    {
        cout << "Please type Yes or No\n";
        cin >> retry;
    }
    if (retry == "Yes" || retry == "yes" || retry == "y")
    {
        payment();
    }
    if (retry == "No" || retry == "no" || retry == "n")
    {
        cout << string(5, '\n');
        main();
    }
}
}
Last edited on Dec 22, 2016 at 9:12am
Dec 22, 2016 at 1:43pm
I think what you need is a for loop.
1
2
3
4
5
6
7
for(int x=0;x<number_of_items;x++)
{
//get amount to purchase and do stuff
}

//The loop starts with x=0
//While the condition in the middle (x<number_of_items) is true, it does whatever is in the {}, then increses x by one. 


This solves the ifs, but there is another problem.
You have a variable for every item to purchase.
For this, you need to use an array:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//int items[<maximum_elements>];
//Declare an array that can store a specific amount of elements
//In your case, the number of elements is numberofitems;
int items[numberofitems];
//You can think of an array as a list
//Every element in your list has an id
//Ids start from 0
//Your first item is items[0], the second one is items[1] etc.
//Since the x in the for loop goes trough every number from 0 to numberofitems, we can use that as our id to set every single item:
for(int x=0;x<number_of_items;x++)
{
    cin>>items[x];
}
//Now items[0] has the first value cin read, items[1] has the second one and so on. 


Hope this helped.. a little.
Last edited on Dec 22, 2016 at 1:46pm
Dec 22, 2016 at 3:05pm
Thanks alot!! It helped my understanding of the loops and arrays.
However, if I wanted to add a different text for when "number_of_items" changes value, for example:
If I want my first item to have the text "First Item:" before I "cin >> items[x];" and my second item to have the text "Second Item:" before I "cin >> items[x];" and so on.. while having it in a loop like you suggested, is it still possible?
Furthermore, if I would wish for the code to check if the items in items[x] included item A and item C, but doesn't include item B, how am I supposed to go about checking an array?
Dec 22, 2016 at 7:08pm
If you want it to say specific sentences for eash item, you could make an array of responses:
1
2
3
4
5
6
7
8
9
string response[]=
{
"First item:",
"Second item",
"Third item:",
//...
"Last item"
};
cout<<response[x];

or say the actual digit:
1
2
3
cout<<"Item number "<<i+1<<":";
//I used i+1 so that it starts from 1 instead of 0
//Ex: Item number 3: 



As for checking for items, you can loop trough all the items in the array and check:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool includes_a=false;
bool includes_b=false;
bool includes_c=false;
for(int x=0;x<number_of_items;x++)
{
    if(items[x] == A) {includes_a=true;}
    if(items[x] == B) {includes_b=true;}
    if(items[x] == C) {includes_c=true;}
}
if(includes_a && includes_c)
{
    if(!includes_b)
    {
        //items includes a & c, but not b
    }
}

Also, items[x] is an element in items. It cannot include another number.
I assumed you meant the array, not the element.
Hope it helped.
Dec 23, 2016 at 9:34am
Yup, this helped my understanding in arrays more and how to check the elements in the arrays. Thank you!
Topic archived. No new replies allowed.