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?
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.
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?
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;
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