Functions by Value and Reference

Hi,

I'm working on my assignment.
Here is the requirements:
1. Create a first function that gets an input from the user for 3 different pizza sizes (in inches), and their prices accordingly. Validate user input, therefore only the proper price and pizza size should be accepted. The valid ranges for pizza sizes: for small 5 to 7, for medium 8 to 10, and for large 11 to 13. The price ranges on pizza are between 12.50 to 32.00.
2. Create a second function that calculates the area of a pizza and price per inch.
3. Create the last function that displays results as follows: Welcome to the Frugal Pizza Calculator. Enter diameter of a small pizza (in inches): 6 Enter the price of a small pizza: $12.90 Enter diameter of a medium pizza (in inches): 9 Enter the price of a medium pizza: $13.40 Enter diameter of a large pizza (in inches): 12 Enter the price of a large pizza: $27.90 Small pizza Diameter = 6 inches Price = $ 12.90 Per square inch = $0.46 Medium pizza Diameter = 9 inches Price = $13.40 Per square inch = $0.21 Large pizza Diameter = 12 inches Price = $27.90 Per square inch = $0.25 The medium one is the better buy.

This is my code so far. I haven't started part 3 yet, could someone please review my code and let me know if I'm on the right track?

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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
using namespace std;

const double PI = 3.14;

void userInputSize(double&, double&, double&);
void userInputPrice(double&, double&, double&);
double calculateRadious(double&, double&, double&);
double calculateArea(double&, double&, double&);
double calculatePrice(double&, double&, double&);

int main(){
	
	cout << "Enter the size and the price of the pizza" << endl;
	cout << " Small Pizza varies between 5 to 7 inches " << endl;
	cout << " Medium pizza varies between 8 to 10 inches " << endl;
	cout << " Large pizza varies between 11 to 13 inches " << endl;

	double sSize,
		mSize,
		lSize,
		sPrice,
		mPrice,
		lPrice;
	userInputSize(sSize, mSize, lSize);
	cout << " Small pizza costs $12.90" << endl;
	cout << " Medium pizza costs $13.40" << endl;
	cout << " Large pizza costs $27.90" << endl;
	userInputPrice(sPrice, mPrice, lPrice);

	//calculateRadious- Will calculate the Radious of the Pizza
	double sRadious=0.00,
		mRadious=0.00,
		lRadious=0.00;
	calculateRadious(sRadious, mRadious, lRadious);

	sRadious = (sSize / 2);
	mRadious = (mSize / 2);
	lRadious = (lSize / 2);
	
	//calculateArea- Will calculate the area of the Pizza
	double sArea,
		mArea,
		lArea;

	calculateArea(sArea, mArea, lArea);
	sArea = (PI* sRadious) * 2;
	mArea = (PI * mRadious) * 2;
	lRadious = (PI * lRadious) * 2;
	cout << " Area of a small pizza = " << sArea << endl;
	cout << " Area of a medioum size pizza = " << mArea << endl;
	cout << " Area of the large pizza = " << lArea << endl;

	//calculatePrice- Will calculate the price per inch
	double sPerInch,
		mPerInch,
		lPerInch;

	calculatePrice(sPerInch, mPerInch, lPerInch);
	sPerInch = (sSize / sPrice);
	mPerInch = (mSize / mPrice);
	lPerInch = (lSize / lPrice);

	cout << " Per per inch of the small Pizza = " << sPerInch << endl;
	cout << " Per per inch of the medium Pizza = " << mPerInch << endl;
	cout << " Per per inch of the large Pizza = " << lPerInch << endl;

	
	system("pause");
	return 0;
}
void userInputSize(double&small, double& medium, double& large){

	do {
		cout << "Enter the size of the small pizza (in inches):" << endl;
		cin >> small;
		cout << "Enter the size of the medium pizza (in inches):" << endl;
		cin >> medium;
		cout << "Enter the size of the large pizza (in inches):" << endl;
		cin >> large;
	} while ((!(small >= 5) || !(small <= 7)) && (!(medium >= 8) || !(medium <= 10)) && (!(large >= 11) || !(large <= 13)));

}

void userInputPrice(double&smallp, double&mediump, double&largep){

	do{
		cout << "Enter the price of the small pizza" << endl;
		cin >> smallp;
		cout << "Enter the price of the medium pizza" << endl;
		cin >> mediump;
		cout << "Enter the price of the large pizza" << endl;
		cin >> largep;
	} while (!(smallp == 12.90) && !(mediump == 13.40) && !(largep == 27.80));
}

double calculateRadious(double& sRadious, double& mRadious, double& lRadious){

	return (sRadious, mRadious, lRadious);
}



double calculateArea(double& sArea, double& mArea, double& lArea){

	return (sArea, mArea, lArea);
}

double calculatePrice(double& sPrice, double& mPrice, double& lPrice){

	return (sPrice, mPrice, lPrice);
}
So We meet again OP.

I ran this program and only had one problem. The Area of the large pizza. You never do the calculations for it, so it prints out junk.

cout << " Area of the large pizza = " << lArea << endl;

Another thing. Whats Per Per inch?

[code]cout << " Per per inch of the small Pizza = " << sPerInch << endl;
cout << " Per per inch of the medium Pizza = " << mPerInch << endl;
cout << " Per per inch of the large Pizza = " << lPerInch << endl;[/code

Edit: Im assuming your 3 calculate functions at the end is part of part 3, because they dont do anything atm.

Last edited on
Yes:)
for Area of Large Pizza I believe I should use set precision.

For the other thing, it should be Price per inch, haha. I fixed that too.

Thank you,
I think there is a problem in my program, I need to do all the calculations inside my function definition, however, when I do that, I get 0 for all my calculation since all my variables has been initialized to 0. I'm not sure how to fix the issue.
Here is my code:

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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <iostream>
using namespace std;

const double PI = 3.14;

//Function Decleration
void userInputSize(double&, double&, double&);
//
void userInputPrice(double&, double&, double&);
double calculateRadious(double&, double&, double&);
double calculateArea(double&, double&, double&);
double calculatePrice(double&, double&, double&);
//CalculateBestDeal- Prints the size and price of the pizza
void calculateBestDeal(double, double);

int main(){
	
	cout << "Enter the size and the price of the pizza" << endl;
	cout << " Small Pizza varies between 5 to 7 inches " << endl;
	cout << " Medium pizza varies between 8 to 10 inches " << endl;
	cout << " Large pizza varies between 11 to 13 inches " << endl;

	double sSize,
		mSize,
		lSize,
		sPrice,
		mPrice,
		lPrice;
	userInputSize(sSize, mSize, lSize);
	cout << " Small pizza costs $12.90" << endl;
	cout << " Medium pizza costs $13.40" << endl;
	cout << " Large pizza costs $27.90" << endl;
	userInputPrice(sPrice, mPrice, lPrice);

	//calculateRadious- Will calculate the Radious of the Pizza
	double sRadious=0.00,
		mRadious=0.00,
		lRadious=0.00;
	calculateRadious(sRadious, mRadious, lRadious);
	
	//calculateArea- Will calculate the area of the Pizza
	double sArea,
		mArea,
		lArea;

	calculateArea(sArea, mArea, lArea);

	//calculatePrice- Will calculate the price per inch
	double sPerInch,
		mPerInch,
		lPerInch;

	calculatePrice(sPerInch, mPerInch, lPerInch);

	//Calculate the best size and best price of the pizza
	double bestPizzaSize=0.00,
		bestPizzaPrice=0.00;
	calculateBestDeal(bestPizzaSize, bestPizzaPrice);
	
	system("pause");
	return 0;
}
void userInputSize(double&small, double& medium, double& large){

	do {
		cout << "Enter the size of the small pizza (in inches):" << endl;
		cin >> small;
		cout << "Enter the size of the medium pizza (in inches):" << endl;
		cin >> medium;
		cout << "Enter the size of the large pizza (in inches):" << endl;
		cin >> large;
	} while ((!(small >= 5) || !(small <= 7)) && (!(medium >= 8) || !(medium <= 10)) && (!(large >= 11) || !(large <= 13)));

}

void userInputPrice(double&smallp, double&mediump, double&largep){

	do{
		cout << "Enter the price of the small pizza" << endl;
		cin >> smallp;
		cout << "Enter the price of the medium pizza" << endl;
		cin >> mediump;
		cout << "Enter the price of the large pizza" << endl;
		cin >> largep;
	} while (!(smallp == 12.90) && !(mediump == 13.40) && !(largep == 27.80));
}

double calculateRadious(double& newsRadious, double& newmRadious, double& newlRadious){
	double sSize=0.00,
		mSize=0.00,
		lSize=0.00;
	newsRadious = (sSize / 2);
	newmRadious = (mSize / 2);
	newlRadious = (lSize / 2);
	return (newsRadious, newmRadious, newlRadious);
}



double calculateArea(double& newsArea, double& newmArea, double& newlArea){

	double newsRadious=0.00,
		newmRadious=0.00,
		newlRadious=0.00;
	newsArea = (PI* newsRadious) * 2;
	newmArea = (PI * newmRadious) * 2;
	newlRadious = (PI * newlRadious) * 2;
	cout << " Area of a small pizza = " << newsArea << endl;
	cout << " Area of a medioum size pizza = " << newmArea << endl;
	cout << " Area of the large pizza = " << newlArea << endl;

	return (newsArea, newmArea, newlArea);
}

double calculatePrice(double& sPrice, double& mPrice, double& lPrice){

	double sPerInch=0.00,
		mPerInch=0.00,
		lPerInch=0.00,
		sSize=0.00,
		mSize=0.00,
		lSize=0.00;
	
	sPerInch = (sSize / sPrice);
	mPerInch = (mSize / mPrice);
	lPerInch = (lSize / lPrice);

	cout << " Price per inch of the small Pizza = " << sPerInch << endl;
	cout << " Price per inch of the medium Pizza = " << mPerInch << endl;
	cout << " Price per inch of the large Pizza = " << lPerInch << endl;

	return (sPrice, mPrice, lPrice);
}

void calculateBestDeal(double bestSize, double bestPrice){

	double sSize=0.00,
		mSize=0.00,
		lSize=0.00,
		sPrice=0.00,
		mPrice=0.00,
		lPrice=0.00,
		sPerInch=0.00,
		mPerInch=0.00,
		lPerInch=0.00;

	cout << " Enter diameter of a small pizza (in inches): " << sSize << endl;
	cout << " Enter the price of small pizza " << sPrice << endl;
	cout << " Enter diameter of a medium pizza (in inches): " << mSize << endl;
	cout << " Enter the price of medium pizza " << mPrice << endl;
	cout << " Enter diameter of a large pizza (in inches): " << lSize << endl;
	cout << " Enter the price of large pizza " << lPrice << endl;
	cout << " Small Pizza Diameter = " << sSize << " inches" " Price = " << sPrice << " Per square Inch = " << sPerInch << endl;
	cout << " Medium Pizza Diameter = " << mSize << " inches" " Price = " << mPrice << " Per square Inch = " << mPerInch << endl;
	cout << " Large Pizza Diameter = " << lSize << " inches" " Price = " << lPrice << " Per square Inch = " << lPerInch << endl;

}
1.- I think there is a problem in my program, I need to do all the calculations inside my function definition, however, when I do that, I get 0 for all my calculation since all my variables has been initialized to 0. I'm not sure how to fix the issue.

_____________________________________________________________________
why u dont use pointers in ur functions, example:

userInputSize(double *sSize,double * mSize,double * lSize);

_____________________________________________________________________
2.- Is that ok?

sPerInch = (sSize / sPrice);
mPerInch = (mSize / mPrice);
lPerInch = (lSize / lPrice);

isnt like this:

sPerInch = (sPrice / sSize);
______________________________________________________________________

3.- Why this?

while ((!(small >= 5) || !(small <= 7)) && (!(medium >= 8) || !(medium <= 10)) && (!(large >= 11) || !(large <= 13)));

what will happend if u input 9 in the small, 10 in the medium, and 15 in the large?

why not?

while((small!=6)||( medium != 9)||(large != 12))
_____________________________________________________________________

4.- Im not sure of the area :
newsArea = (PI* newsRadious) * 2;

isnt:

newsArea = PI* (newsRadious *newsRadious) ;

____________________________________________________________

5.- And for the end:


Im not sure about this one but isnt suppuse the function display(), do all the thing u are doing in main()?;

____________________________________________________________

Im not sure if im right but good luck, and remember u can use arrays and the for loop;






Topic archived. No new replies allowed.