Class: unable to initialize values?

Hi !

So I'm trying to practice writing classes and I wrote this class for a pizza ingredient-menu.

I'm having a problem with initializing pre-set values, more specifically the sauce choice. I have it set to 10 but once it gets into the menu it's as though sauce = 1 and if you input "10" for the number of cups you want it outputs "10 cups of sauce" when it should show 100. I tried to put the 10*sauce*num in the GetIngredients menu but then it started giving negative values.

Any idea what I'm doing wrong?

PS: is it better to write separate get-statements for each of the toppings or??

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <cmath>
using namespace std;


class Pizza
{
private:
	int anchovies =1 ;
	int pepperoni =1;
	int chicken=1;
	int bellpepper=1;
	int sauce=10;
	int num;

public:
	void setAnchovies(int a, int x)
	{
		anchovies = a;
		num = x;
	
	}
	void setPepperoni(int p, int x)
	{
		pepperoni = p;
		num = x;
	}
	void setChicken(int c, int x)
	{
		chicken = c;
		num = x;
	}
	void setBellpepper(int b, int x)
	{
		bellpepper = b;
		num = x;
	}
	void setSauce(int s, int x)
	{

		sauce = s;
		num = x;
	}
	int getIngredients()
	{
		return anchovies * num;
		return chicken * num;
		return pepperoni*num;
		return bellpepper*num;
		return sauce*num; //10*sauce*num doesn't work either
	}
};
int main()
{
	int x,p=0,a=0,c=0,b=0,s=0, choice;
	Pizza choice1;
	cout << "How much sauce would you like?" << endl;
	cin >> x;
	choice1.setSauce(s, x);
	cout << "That'll be " 
        << choice1.getIngredients() 
        << " cups of sauce." << endl;
	cout << "What topping would you like?" << endl;
	cout << "\n1.Pepperoni" 
         << "\n2.Anchovies" 
         << "\n3.Chicken" 
          << "\n5.Bellpeppers" << endl;
	cin >> choice;
	if (choice == 1)
	{
		cout << "You chose Pepperoni" << endl;
		cout << "How much Pepperoni would you like?" << endl;
		cin >> x;
		choice1.setPepperoni(p, x);
		cout << 
                 "That'll be  " 
                 << choice1.getIngredients() 
                  << " pieces of pepperoni on your pizza." 
                 << endl;
		
	}
	if (choice == 2)
	{
		cout << "You chose Anchovies" << endl;
		cout << "How many anchovies would you like?" << endl;
		cin >> x;
		choice1.setAnchovies(a, x);
		cout << "That'll be  " 
                 << choice1.getIngredients() 
                 << " pieces of anchovy on your pizza." << endl;

	}


	system("pause");
	return 0;
}
Last edited on
This:
1
2
3
4
5
6
7
8
	int getIngredients()
	{
		return anchovies * num;
		return chicken * num;
		return pepperoni*num;
		return bellpepper*num;
		return sauce*num;
	}
will have the same effect as:
1
2
3
4
	int getIngredients()
	{
		return anchovies * num;
	}

because the return statement means 'exit from this function now', as well as specifying the value which is to be returned.
Ohh, so it's not even going to the next lines at all, it just immediately stops at the first line?
Last edited on
Anyways, thanks a bunch for the help!!
Topic archived. No new replies allowed.