Hello There!! I am having some trouble with 'if...else' statement. Supposed to show price for medium or large pizza with no coupon, medium or large pizza with coupon, invalid size pizza (must be L or M). For some reason, the program only calculates L - 2.00 correctly....
#include <iostream>
#include <iomanip>
using namespace std;
In your case you set totalAmt only when (toupper (coupon) == 'Y'). I suggest that you set totalAmt without coupon and in a second step eventually subtract the coupon value.
if ((size == 'M') && (toupper (coupon) == 'Y')) {
totalAmt = med - 2.00; // price Med, with coupon
}
else ((size == 'L')&& (toupper (coupon) == 'Y'));
{
totalAmt = large - 2.00; // price L, with coupon
}
That else line has a couple of things wrong with it:
a) You can't follow else with a condition. It must either be elseif (/*condition goes here*/) or just else to catch every situation where previous conditions aren't met.
b) Ending that line with a semicolon means that the conditional block ends on that line. It's the equivalent of writing:
3) The conditional block for medium-with-coupon is exactly the same as for large-with-coupon. Why two separate blocks here, when one would do?
4) You're trying to use the variable coupon to mean two different things - the amount of the discount, and the user Y/N input for whether they have one. You'll need two different variables for these two different things.
5) ... and then you should actually use the variable containing the discount value in the calculation. Scattering magic numbers about the code is generally not a good idea; it reduces maintainability and readability.
Hello @coder777 & @MikeyBoy. Thank you for the feedback!! Though I am still having issues. I did not quite understand "3) The conditional block for medium-with-coupon is exactly the same as for large-with-coupon. Why two separate blocks here, when one would do?"
Is it possible to show me how it's supposed to look like? I cannot find any examples from my textbook nor a similar example online...