Utilizing a function with a passing value

Hi, I am a student and new to proramming. I just learn function last week and yesterday i got an assignment from my teacher that i can't solve.

Here are the tasks:

1. Make a program using "nested if" with the following scenario:
A shop give a discount to costumer that buy 3 items with condition, at least 2 items exceed $15 each.

2. Using the scenario above, make a "loop" which numbers of "loop" are inputed by user or as long as the user need to use the "loop".

3. Using scenario that mentioned in question number 1, make a program with "Discount" function with a "passing value".


i can solve number 1 and 2 but i stuck at number 3. as what i understand with function with passing value is you change the value of variable by a process inside another function [[is it right?]]. but i don't seem to understand how to use that in question number 3.

thanks beforehand for helping.
i can solve number 1 and 2 but i stuck at number 3
post your code for 1 and 2 then and we'll take it as a starting point to try and answer 3
okay, here my codes

for question number 1:

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
int main()
{
	int A;
	int B;
	int C;
	
	cout<<"Welcome to our shop! you will get a discount if you buy 3 items with at least 2 items above $15!"<<endl<<endl;
	
	cout<<"Enter first item"<<endl;
	cin>>A;
	cout<<"Enter second item"<<endl;
	cin>>B;
	cout<<"Enter third item"<<endl;
	cin>>C;
	
	if (A>0 && B>0 && C>0){
		if (A&&B>15|| A&&C>15 ||B&&C>15)
			cout<<"You got a Discount!";
		else
			cout<<"Sorry, You don't get a Discount";
	}else{
		cout<<"You  enter wrong value!";
	}
	
}


for number 2:
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
#include <iostream>
using namespace std;

int main()
{
	int A;
	int B;
	int C;
	int X=1;
	
	cout<<"Welcome to our shop! you will get a discount if you buy 3 items with at least 2 items above $15!"<<endl<<endl;
	
	while(X==1)
	{	
		cout<<"Enter first item"<<endl;
		cin>>A;
		cout<<"Enter second item"<<endl;
		cin>>B;
		cout<<"Enter third item"<<endl;
		cin>>C;
		
		{
			if (A>0 && B>0 && C>0)
			{
				if (A&&B>15|| A&&C>15 ||B&&C>15)
					cout<<"You got a Discount!"<<endl<<endl;
				else
					cout<<"Sorry, You don't get a Discount"<<endl<<endl;
			}else{
				cout<<"You  enter wrong value!"<<endl<<endl;
			}
		}
	
	cout<<"Try another session?"<<endl;
	cout<<"1 = YES"<<endl<<"2 = NO"<<endl;
	cin>>X;
	}
	
	
}


Thanks for your codes. In Q3 you takes what you've done for Q1 and try and turn it into a function that takes 4 variables - the 3 prices and the price limit above which you get a discount. This function will return a bool value that is true if the customer is eligible for a discount and false otherwise, including the case where wrong values are entered because obviously no discount on negative prices.

This discount_checker function is defined outside main() and then within main() you declare and initialize 4 variables - the 3 prices and the price limit. These variables are then passed to the discount_checker function and the return value of the function is evaluated to see if the customer gets the discount or not.

So ... putting all these together, your program would look something like this:
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
#include <iostream>

bool discount_checker(int priceA, int priceB, int priceC, int limit)
{
    if (priceA>0 && priceB>0 && priceC>0)
    {
		if (((priceA > limit) && (priceB > limit))|| ((priceA > limit) && (priceC>limit))
            ||((priceB > limit) && (priceC > limit)))
		{
		    return true;
		}
        else
        {
            return false;
        }
    }
	else
    {
		std::cout<<"You  entered wrong value!";
        return false;
    }
}
int main()
{
    int priceA, priceB, priceC;
    std::cout << "Enter the three prices: \n";
    std::cin >> priceA >> priceB >> priceC;

    int limit;
    std::cout << "Enter the discount limit: \n";
    std::cin >> limit;

    if (discount_checker (priceA, priceB, priceC, limit))
    {
        std::cout << "You got a Discount!\n";
    }
    else
    {
        std::cout << "Sorry, You don't get a Discount";
    }
}

Your code for checking if at least 2 prices are greater than 15 is slightly off, you need to evaluate the prices individually. If you write (A&&B>15) this expression will evaluate true if A > 0 and B > 15, so if you entered (14, 15, 16) into your program for Q1 you'll find that it says discount is available though only one price is greater than 15. You need to evaluate the prices as shown in the code above

PS: in C++ there is another concept called passing by value vs passing-by-reference that describes how arguments are passed to functions. You can also read up about it sometime
thanks for your help, and sorry if i make you confuse by the "Passing Value". you see, when i learn it in class my teacher keep saying there are two function that are function with "Passing Value" (which actualy is passing by reference) that do not return any value to the caller and function with "Return Value" that return a value to the caller. thats why i dont quite understand the tutorial for "passing by value" as it different with what i learn and because i need to input the value when runing the program.

anyway i understand now how it works thanks to your code and for pointing problem at my code, i realize that something is off, when i run my code, the result never correct when third variable is not fulfill the condition. i was trying to separate the condition with more ifs but it become more complicated.

thanks this part really help me!
1
2
if (((priceA > limit) && (priceB > limit))|| ((priceA > limit) && (priceC>limit))
            ||((priceB > limit) && (priceC > limit)))
different concepts, don't mix the two:
(a) non value returning (void) functions vs value returning functions:
this refers to whether or not any values are returned FROM the function
http://www.cs.fsu.edu/~cop3014p/lectures/ch7/index.html
(b) passing-by-value vs passing-by-reference:
this refers to how parameters are passed TO the function
http://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value
Topic archived. No new replies allowed.