checking for 2 values of the 6 and adding discount

So I've just completed the shop part of my code. However, I would like to add a system such that if they chose options 1 and 2 for their choices then they would be entitled to a discount of 10%. How do I make the system check to see if the 2 choices were selected?

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
128
129
130
131
132
133
134
135
136
137
138
#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 Price[6] = {16, 20, 10, 1, 15, 10};
int sum = 0;

int payment2()
{
    cout << "\n\n\nThe total price of your purchase would be $" << sum <<endl;
}


int payment1(int y)
{
    if (y==1&y==2)
    switch (y)
    {
        case 1: sum += Price[0];
        break;
        case 2: sum += Price[1];
        break;
        case 3: sum += Price[2];
        break;
        case 4: sum += Price[3];
        break;
        case 5: sum += Price[4];
        break;
        case 6: sum += Price[5];
    }
}

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


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


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


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


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


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

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. Jessica($1)\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();
    }
}
}
Hello javiertzr01,

I disagree with the use of all the global variables that you have. It is bad form and makes it very easy to create problems that become very difficult to track down. On the up side most of the variables are initialized as they should be lines 6, 7 and 14 need to b initialized.

Next at line 18 the function payment2 returns an "int", but the function does not return anything. Since the function only prints to the screen there is no need to return anything. It should actually be written void payment2().

Line 24 payment1() returns an "int", but the function body does not return anything.

The if statement at line 26 is mostly useless and does not do anything. Also I believe you should have written it as: if (y==1 && y==2) to compare the left and right sides.

Also line 43 has the same problem it returns an "int" yet the function returns nothing.

I think that case 1 and 2 is where you would apply your 10% discount, but you will have a problem with "sum" being defined as an "int" which will loose the decimal portion of the calculation.

Then line 43 the payment function returns an "int" yet the body returns nothing.

In payment() and main you are calling function that return "ints", but you never use the returned value.

Hope that helps,

Andy
Hello Andy,

First of all, thank you for your reply! It was very much appreciated.
I changed everything you mentioned out but I do not understand the disagreement of global variables. Where am I supposed to put the variables then? In the int main()? If so, when do I determine when to put global variables and when to put variables in int main()?
I also changed the type of sum from int to float.

Secondly, how am I supposed to make the system check if sum contains Price[0] and Price[1]?

javiertzr01
Hello javiertzr01,

Most variables are defined in main and passed to the functions that need them. Other variables are defined in the functions that need a variable in that function. Sometimes a variable that is defined in a function is returned to where it is called so it can be used where it was called from.

When I write a program I will define most of my variables in main then pass anything that is needed to a function that I call from main. It would depend o the function if you need to define any variables i the function.

I worked the program to get it to work. I added to what you started with, but did not change the global variables until I made sure the program worked first. I have commented what I changed and added. If there is anything you do not understand let me know.

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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <iostream>
#include <string>
#include <iomanip>

int numberofitems{ 0 };
std::string retry{ "" };
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
int f = 0;
int x{ 0 };
int Price[6] = { 16, 20, 10, 1, 15, 10 };
double sum = 0.0;
size_t discount{ 0 }; // <--- Added to determine if discount. size_t same as unsigned int.

void payment2()
{
	std::cout << std::fixed << std::setprecision(2); // <--- Added fr decimal place in final total. include "iomanip" header file
	std::cout << "\n\n\nThe total price of your purchase would be $" << sum << std::endl;
}


void payment1(int y) // <--- Changed to void, nothing neds returned.
{
	//if (y == 1 && y == 2)  //  <---- Changed to &&/ But, not needed.
	//	;
	switch (y)
	{
	case 1:
		sum += Price[0];
		discount |= 2; // Used to determine if a discount applies.
		break;
	case 2:
		sum += Price[1];
		discount |= 4; // Used to determine if a discount applies.
		break;
	case 3:
		sum += Price[2];
		break;
	case 4:
		sum += Price[3];
		break;
	case 5:
		sum += Price[4];
		break;
	case 6:
		sum += Price[5];
	}
}

void payment() // <--- Changed to void, nothing neds returned.
{
	x = 1;
	if (x <= numberofitems)
	{
		std::cout << "\nPlease indicate the number of the first item you would like to purchase: ";
		std::cin >> a;
		payment1(a);
		x = x + 1;


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


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


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


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


						if (x <= numberofitems)
						{
							std::cout << "\nPlease indicate the number of the sixth item you would like to purchase: ";
							std::cin >> f;
							payment1(f);
							x = x + 1;
						}
						else { if (discount == 6) sum -= sum * 0.10; payment2(); } // <--- Added the if statement.
					}
					else { if (discount == 6) sum -= sum * 0.10; payment2(); } // <--- Added the if statement.
				}
				else { if (discount == 6) sum -= sum * 0.10; payment2(); } // <--- Added the if statement.
			}
			else { if (discount == 6) sum -= sum * 0.10; payment2(); } // <--- Added the if statement.
		}
		else { if (discount == 6) sum -= sum * 0.10; payment2(); } // <--- Added the if statement.
	}
}


int main()
{
	std::cout << "Welcome to our shop \n";
	std::cout << "Please choose what you may wish to buy \n";
	std::cout << "1. Charmander Plushie($16)\n2. Barney($20)\n3. Bulbasaur Plushie($10)\n4. Jessica($1)\n5. Figurine($15)\n6. Plastic Memories($10)\n";
	std::cout << "Please indicate how many items you would wish to buy: ";
	std::cin >> numberofitems;
	if (numberofitems >= 3)
	{
		std::cout << "\nYou are eligible for a discount of 10% if 2 of your items exceeds $15" << std::endl;
		payment();
	}
	else
	{
		std::cout << "You are not eligible for a discount\n A discount is only available if you purchase 3 or more items\n";
		std::cout << "Would you still want to continue with the purchase?\n";
		std::cin >> retry;
		while (retry != "Yes" && retry != "yes" && retry != "y" && retry != "No" && retry != "no" && retry != "n")
		{
			std::cout << "Please type Yes or No\n";
			std::cin >> retry;
		}

		if (retry == "Yes" || retry == "yes" || retry == "y")
		{
			payment();
		}

		if (retry == "No" || retry == "no" || retry == "n")
		{
			std::cout << std::string(5, '\n');
			main(); // Use a do while loop. Do not call main to start over.
		}
	}  //  End else
}  // End main 


I think this should compile and run. I took this from what I used which has extra code that is not needed here.

Hope this helps,

Andy
Hello Andy,

Once again, thank you for your reply!
This code you have provided really helped me understand what I needed to understand. Thank you. However, I do not understand the difference between |= and +=. Don't they both add the amount to the integer?

javiertzr01
Hello javiertzr01,

Yes on the surface it may seem that they are both addition, but the bitwise or '|' is not adding two numbers. It is doing an 'or' of each binary digit of the numbers to achieve the result. A good example is to do an '|' on an odd and even number and see the result is not an addition, but a different number.

Another quick example is outFile.open(oFileName, std::ios::trunc | std::ios::ate);. In the header file "trunc" and "ate" have a "#define" giving each name a value that is used in a line of code. Putting together the values of "trunc" and "ate" allow the open function the information to open the file correctly.

The concept of using the bitwise or '|' is something I have been working with lately and seemed like a good place to use it. I should also mention that it works best with even numbers i.e., 2, 4, 6, 8 and if you need more numbers just add a 0 (zero) i.e., 20, 40, 60, 80 next would be 200 etc. If you look in some of the header files you can see the concept used quite often.

I have been interrupted several times writing this, so if it does not make any sense let me know and I will try again.

Hope that helps,

Andy
Hello Andy,

Sorry but I still do not quite understand whay it means by doing an 'or' of each binary number.
I also do not quite understand the trunc and ate part. Does it mean that trunc and ate both contain a value and combining both binary numbers allow for the file to be opened correctly? If so, then why I still don't get how it is an 'or' rather than 'and'.
Thank you for trying to help me understand :D

javiertzr01
Topic archived. No new replies allowed.