Pizza Slices

Could someone insist me in why my program is not running correctly

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
55
#include <iostream>
using namespace std;

// Declarations
int getData();
int computePrice();
int displayData();


int numbToppings, pizzaSize;
char S, M, L;
float cost, price, toppingPrice;


int main()//Main function
{
    getData();
    computePrice();
    displayData();
}
  int getData() //getdata function (function 1)
        {
        cout<<"Please enter the size of pizza? (S,M or L): "<< endl;
        cin>>pizzaSize;
        cout<<"Please enter the number of toppings?: " << endl;
        cin>>numbToppings;
        }

  int computePrice()  //calculate function (function 2)

        {          
        if (pizzaSize=S)
        {
        toppingPrice = 5.50 ; price = 50;
        }
        else if (pizzaSize=M)
        {
        toppingPrice = 6.50 ; price = 70;
        }
        if (pizzaSize=L)
        {
        toppingPrice = 7.50 ; price = 90;
	    }
        price = (toppingPrice * numbToppings) + price;
        return cost;// returns from main()
        }

 int displayData()  //display function (function 3)
        {
        cout << endl;
        cout << "The cost of the " << pizzaSize<< " Pizza with "
        << numbToppings << " different toppings is R" << cost << "." << endl;
        cout << endl;
        return 0;
        }
On line 10 you've declared pizzaSize as an int, but you're prompting for a character (S, M, or L). So you should define it as char instead.

When comparing the size on lines 32, 36 and 40, you're actually doing 2 things wrong. First, you're using = instead of == assigns the express on the right to the variable on the left, so, for example, line 32 actually says "assign the value of variable S to pizzaSize. If the value is not zero then execute line 34." You want to compare pizzaSize to the character 'S':
if (pizzaSize == 'S')
On line 52 you are using the float cost which you've not assigned a value to so it'll never display the correct number.

Also your getData function being an int needs to return something.
its still not doing the calculation:

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
55
56
57
#include <iostream>
using namespace std;

// Declarations
int getData();
int computePrice();
void displayData();


int numbToppings ;
char S, M, L, pizzaSize;
float cost, price, toppingPrice;


int main()//Main function
{
    getData();
    computePrice();
    displayData();
}
  int getData() //getdata function (function 1)
        {
        cout<<"Please enter the size of pizza? (S,M or L): ";
        cin>>pizzaSize;
        cout << endl;
        cout<<"Please enter the number of toppings?: " ;
        cin>>numbToppings;
        cout << endl;
        return 0;
        }

  int computePrice()  //calculate function (function 2)

        {
        if (pizzaSize == S)
        {
        toppingPrice = 5.50 ; price = 50;
        }
        else if (pizzaSize == M)
        {
        toppingPrice = 6.50 ; price = 70;
        }
        if (pizzaSize == L)
        {
        toppingPrice = 7.50 ; price = 90;
	    }
        cost = (numbToppings * toppingPrice) + price;
        return cost;// returns from main()
        }

 void displayData()  //display function (function 3)
        {
        cout << endl;
        cout << "The cost of the " << pizzaSize<< " Pizza with "
        << numbToppings << " topping/s is R" << cost << "." << endl;
        cout << endl;
        }

Last edited on
If you want to compare a char using the letter it must be surrounded with ' '

On lines 35, 39, and 43 you're missing them. For example line 35 would be:
if (pizzaSize == 'S')

Keep in mind this only compares to see if pizzaSize is equal to S if you enter s it will not work.


Edit:
I just realized you're wanting to compare your variables you declared at the top with the input pizzaSize.
If you're wanting to use them you're going have to assign a value to each of them. When you assign a char a value you still have to use ' ' Example: char S = 'S'



If you want to use the first method you can get rid of the variables S, M, and L from the top.
If you're going to use the second method you're going have to keep them and assign a value to them.
Last edited on
So close ....My if statements only calculate for small doesn't matter what I select
Last edited on
Lines 11-13: S,M,L are all going to have a value of 1. 'S' || 's' is a boolean expression that evaluates to true (1). Ditto for M and L.

Lines 38,42,46: C++ does not support implied left hand side in conditionals. You must fully specify the conditions.
Example:
if (ans == 'Y' || 'y') evaluates as if ((ans == 'Y') || ('y'))

('y') always evaluates to 1 (true), therefore the if statement is always true, regardless of ans.

Edit: Corrected order of evaluation. Thanks dhayden.


Last edited on
if (ans == 'Y' || 'y') evaluates as if (ans == ('Y' || 'y'))
AbstractionAnon, You've had a rare brain hiccup. == has higher precedence than || so it evaluates as
if ((ans == 'Y') || 'y') and since 'y' is always true, the whole expression is true.
Hayden so what am I doing wrong if you not agreeing with AbstractionAnon?
Actually I agree with AnstractionAnon in the sense that the if expression always evaluates to true. My point was that it does so for a different reason.

The if should be if (ans == 'Y' || ans == 'y') Make similar changes to the other if statements.
Thank you ..it works now
Topic archived. No new replies allowed.