If statements

Hello I am new to programming and having trouble with a function using a class with if statements to check for a correct character value. The user inputs the size of the pizza which is just one of those characters, and the user also inputs the number of toppings. When I run the program pizzaSizePrice is always set to 17, and if I remove the else statements, pizzaSizePrice is always set to 14. Can someone explain what I need to do?

void pizza::computePrice()
{
double pizzaSizePrice;
if (pizzaSize == 'M' || 'm'&& pizzaSize != 'S'||'s'||'L'||'l')
{
pizzaSizePrice = 14;
}
else if(pizzaSize == 'S' || 's'&& pizzaSize != 'M'||'m'||'L'||'l')
{
pizzaSizePrice = 10;
}
else if (pizzaSize == 'L' || 'l'&& pizzaSize != 'S' || 's' || 'M' || 'm')
{
pizzaSizePrice = 17;
}

double toppingPrice = numberOfTopping * 2;
double totalPrice = toppingPrice + pizzaSizePrice;
cout << totalPrice;

}
This is not valid:
 
    pizzaSize == 'M' || 'm'

each condition must be expressed separately
 
    (pizzaSize == 'M' || pizzaSize == 'm')


However, that can soon become tedious. You can simplify things slightly by first converting pizzaSize to either uppercase or lowercase.

pizzaSize = toupper(pizzaSize);
and then you need only
if (pizzaSize == 'M')

Some of the if-statements are unnecessarily complex, if the value is 'M' we know it isn't 'S' or 'L', so there's no need to test those as well in the same statement.

Remember to #include <cctype>
http://www.cplusplus.com/reference/cctype/toupper/


Last edited on
Hello Chervil!

So I used to the toupper() and that definitely simplified my arguments.

However, I still have a problem with the variable pizzaSizePrice. I ran the program and tried a small pizza with two toppings. The price should have been 14 dollars. However the price was 21 dollars. That means that the pizzaSizePrice must somehow been assigned the value of 17. Do you know why?
I think you need to show your updated code, otherwise it's not possible to say.
My bad here is what I have now

void pizza::computePrice()
{
toupper(pizzaSize);
double pizzaSizePrice;
if (pizzaSize == 'M')
{
pizzaSizePrice = 14;
}
else if(pizzaSize == 'S')
{
pizzaSizePrice = 10;
}
else if (pizzaSize == 'L' )
{
pizzaSizePrice = 17;
}

double toppingPrice = numberOfTopping * 2;
double totalPrice = toppingPrice + pizzaSizePrice;
cout << totalPrice;


}
Last edited on
Run-Time Check Failure #3 - The variable 'pizzaSizePrice' is being used without being initialized.

I have this run-time error. Here is my input function. I don't think I'm skipping anything along the way.

void pizza::setPizza()
{
cout << "Please enter the size of the pizza(S for small, M for medium, and L for large): ";
cin >> pizzaSize;
cout << "Please enter the type of the pizza" << endl;
cout << "1 is a Deep Dished Pizza, 2 is a Hand Tossed Pizza, and 3 is a Pan Pizza: ";
cin >> pizzaType;
cout << "Please select the number of toppings(1 is minimal, 2 is average, and 3 is Extreme: ";
cin >> numberOfTopping;
cout << "Please select your type of topping(P for Pepperoni and C for cheese: ";
cin >> pizzaType;

}
Last edited on
The function toupper doesn't alter the parameter which is passed.
Hence this line has no effect:
 
    toupper(pizzaSize);


Instead you must make use of the value which is returned by the function:
 
pizzaSize = toupper(pizzaSize);


The rest of the code looks reasonable, though if the size doesn't match any of S,M,L then the price is still not initialised. It's a good idea to set the price to an initial value of zero
 
    double pizzaSizePrice = 0;


Run-Time Check Failure #3 - The variable 'pizzaSizePrice' is being used without being initialized.

That is to be expected. You might also add a final else
1
2
3
4
5
6
7
8
    else if (pizzaSize == 'L' )
    {
        pizzaSizePrice = 17;
    }
    else
    {
        cout << "warning - unknown pizzaSize " << pizzaSize  << '\n';
    }
Last edited on
Thanks so much! Got it!
Topic archived. No new replies allowed.