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;

int main () {

double coupon = 2.00;
double totalAmt = 0.0;
double med = 9.99;
double large = 12.99;
char size = ' ';

//ask the user medium or large pizza
cout << "Medium (M) or Large (L) pizza?";
cin >> size;
size = toupper (size);

if (size != 'M' && size != 'L') {
cout << "Invalid Size. Please enter either M or L." << endl; //invalid size (x), must be M or L
}

if (size == 'M') //price Med, no coupon
{
totalAmt = med;
}
else { //price L, no coupon
totalAmt = large;
}

cout << "\$2 coupon? (Y/N)?"; //ask coupon
cin >> coupon;

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
}

cout << fixed << setprecision (2);
cout << "Price: \$ " << totalAmt << endl;

return 0;
}
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.
1) Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/

2)

 12345678 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 else if (/*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:

 1234 else if ((size == 'L')&& (toupper (coupon) == 'Y')) { ; // Empty statement }

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.
Last edited on
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...