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.
#include <iostream>
#include <string>
usingnamespace 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();
}
}
}
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.
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?
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.