if... else HW. Please help!!

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)

1
2
3
4
5
6
7
8
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:

1
2
3
4
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...

Thank you in advance!!

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
42
43
44
45
46
47
48
49
50
51
52
53
54
int main() {

#include <iostream>
#include <iomanip>
using namespace std;

int main () {

//declare variables
double coupon = ' ';
double totalAmt = 0.0;
double med = 9.99;
double large = 12.99;
double amtCoupon = 2.00;
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 - amtCoupon;			// price Med with coupon
}

else if ((size == 'L') && (toupper (coupon == 'Y')) {
	
	totalAmt = large - amtCoupon;   // price Large with coupon
}
	
	
cout << fixed << setprecision (2);
cout << "Price: $ " << totalAmt << endl;


    return 0;
}
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
#include <iostream>
#include <cctype>
#include <iomanip>

int main()
{
    const double price_medium = 9.99 ;
    const double price_large = 12.99 ;
    const double coupon_value = 2.00 ;

    char size = 'M' ; // 'M' or 'L'
    std::cout << "Medium (M) or Large (L) pizza? ";
    std::cin >> size;
    size = std::toupper(size);

    double price = 0.0 ;
    if( size == 'L' ) price = price_large ;
    else if( size == 'M' ) price = price_medium ;
    else
    {
        std::cout << "invalid size, size must be either M or L\n" ;
        return 1 ; // quit with non-zero (not normal program termination) result
    }

    char has_coupon = 'N' ; // 'Y' or 'N'
    std::cout << '$' << coupon_value << " coupon? (Y/N)? ";
    std::cin >> has_coupon;

    if( std::toupper(has_coupon) == 'Y' ) price -= coupon_value ;

    std::cout << std::fixed << std::setprecision (2)
              << "Price: $ " << price << '\n' ;
}
Yep, exactly as JLBorges has shown you. In both cases, all you do is subtract 2.0 from the price you've already calculated.

In your revised code, you still have coupon declared as a double, but you're trying to use it as if it were a char.

Thanks for using code tags!
Thanks for everyone's help!! I need more work on my variables...
Topic archived. No new replies allowed.