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