Project: Need help with calling by reference

Hi, I am referencing someone's code given I have a similar project coming up. How would I go about deriving the total ounces at any given time and dealing with those reference variables? (I gave an example of what I thought I would do below) Mostly I'm having trouble with calling things by reference and getting the information to spit out properly. Also I can't get the total amount of cups to spit out correctly, it shows up blank, what am I missing? Thanks

This is what I can gather I would enter in for the missing function:
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
void amountCoffee(int &lifeSmCups, int &lifeMdCups, int &lifeLgCups) //what would I use to call this?
{ozSmall=lifeSmCups*9;
ozMed=lifeMdCups*12;
ozLarge=lifeLgCups*15;

totalOz=ozSmall+ozMed+ozLarge;

cout<<"Total ounces sold is "<<totalOz<<endl;
}
[code]


[code]
  /**

Jason opened a coffee shop at the beach and sells coffee in
three sizes: small(9oz), medium(12oz), and large(15oz).
The cost of one small cup is 1.75, one medium is 1.90, and
one large cup is 2.00.  Write a menu driven program that will
make the coffee shop operational.  Your program should allow the user to
do the following:
A. Buy coffee in any size and in any number of cups.
B. At any time show the total number of cups of each size sold.
c.  At any time show the total amount of coffee sold.
D. At any time show the total money made
your program should consist of at least the following funtions:
a function to show the user how to use the program
a function to sell coffee
a function to show the number of cups of each size sold
a function to show the total amount of coffee sold
a function to show the total amount of money made.
Your program should not use an global variables and special values
such as coffee cup sizes and cost of a coffee cup must be declared as named constants.


*/
#include <iostream>
#include <iomanip>

using namespace std;

//constants

double const smCoffee = 1.75;
double const mdCoffee = 1.90;
double const lgCoffee = 2.00;


//Function prototypes
void initialize(int& numberSmCups, int& numberMedCups, int& numberLgCups, double& totalSmCups,
	double& totalMedCups, double& totalLgCups);
void showChoices();
void buyCoffee(int& numberSmCups, double& totalSmcups, int& numberMedCups, double& totalMedCups, int& numberLgCups
	, double& totalLgCups);
void cupsSold(int& numberSmCups, int& numberMedCups, int& numberLgCups, int& lifeSmCups, int& lifeMdCups, int& lifeLgCups);
void coffeeSold();
void totalAmount(double& totalSmCups, double& totalMedCups, double& totalLgCups);
void printCupsSold(int& lifeSmCups, int& lifeMdCups, int& lifeLgCups);
//Main

int main()
{
	//declare variables
	int numberSmCups;
	int numberMedCups;
	int numberLgCups;
	int lifeSmCups;
	int lifeMdCups;
	int lifeLgCups;
	double totalSmCups;
	double totalMedCups;
	double totalLgCups;
	int choice;

	//initialize
	initialize(numberSmCups, numberMedCups, numberLgCups, totalSmCups,
		totalMedCups, totalLgCups);

	//showChoice switch
	do
	{
		showChoices();
		cin >> choice;
		cout << endl;

		switch (choice)
		{
		case 1:
			cout << "You selected to buy coffee." << endl;
			initialize(numberSmCups, numberMedCups, numberLgCups, totalSmCups,
				totalMedCups, totalLgCups);
			buyCoffee(numberSmCups, totalSmCups, numberMedCups, totalMedCups, numberLgCups, totalLgCups);

			break;
		case 2:
			cout << "The total amount of cups sold by size is:  " << endl;
			cupsSold(numberSmCups, numberMedCups, numberLgCups, lifeSmCups, lifeMdCups, lifeLgCups);

			break;
		case 3:
			cout << "function for lifetime coffee sold";

			break;
		case 4:
			cout << "The total money made for Coffee Sales is:  ";
			totalAmount(totalSmCups, totalMedCups, totalLgCups);

			break;

		case 99:
			break;

		default:
			cout << "Invalid input." << endl;
		}
	} while (choice != 99);


	cout << " Thank you for using this program.  Have a nice day." << endl;
	return 0;
}   //end Main

void showChoices()
{
	cout << "Enter--" << endl;
	cout << endl;
	cout << "1: Buy Coffee " << endl;
	cout << "2: Shows the total cups sold by size " << endl;
	cout << "3: Shows the total amount of coffee sold" << endl;
	cout << "4: Shows the total amount of money made" << endl;
	cout << "99: To quit the program." << endl;
}// end showChoices


void initialize(int& numberSmCups, int& numberMedCups, int&numberLgCups, double& totalSmCups,
	double& totalMedCups, double& totalLgCups/*,int& lifeSmCups,int lifeMdCups,int lifeLgCups*/){
	numberSmCups = 0;
	numberMedCups = 0;
	numberLgCups = 0;
	totalSmCups = 0;
	totalMedCups = 0;
	totalLgCups = 0;
} //end initialize



void buyCoffee(int& numberSmCups, double& totalSmCups, int& numberMedCups, double& totalMedCups,
	int& numberLgCups, double& totalLgCups){

	char coffeeSize;
	char order;

	cout << "Would you like some coffee? (y for yes, n for no)" << endl;
	cin >> order;

	while (true){
		if (order == 'y' || order == 'Y'){


			cout << "What size coffee would you like? (s,m,l)";
			cin >> coffeeSize;


			if (coffeeSize == 's'){
				cout << "How many?";
				cin >> numberSmCups;
				totalSmCups = numberSmCups * smCoffee;
				cout << "Small Coffee: " << numberSmCups << " " << totalSmCups << endl;
				cout << "add another?" << endl;
				cin >> order;
			}
			else
				if (coffeeSize == 'm'){
					cout << "How many?";
					cin >> numberMedCups;
					totalMedCups = numberMedCups * mdCoffee;
					cout << "Medium Coffee: " << numberMedCups << " " << totalMedCups << endl;
					cout << "add another?" << endl;
					cin >> order;
				}
				else
					if (coffeeSize == 'l'){
						cout << "How many?";
						cin >> numberLgCups;
						totalLgCups = numberLgCups * lgCoffee;
						cout << "Large Coffee: " << numberLgCups << " " << totalLgCups << endl;
						cout << "add another?" << endl;
						cin >> order;
					}
		}
		else
			break;

	}//end While

	cout << "you ordered: " << endl;
	cout << endl;
	if (numberSmCups >= 1) cout << "Small Coffee: " << numberSmCups << " " << "@" << " " << smCoffee << " " << "$" << totalSmCups << endl;
	if (numberMedCups >= 1)cout << "Medium Coffee: " << numberMedCups << "@" << " " << mdCoffee << " " << " $" << totalMedCups << endl;
	if (numberLgCups >= 1) cout << "Large Coffee: " << numberLgCups << "@" << " " << lgCoffee << " " << " $" << totalLgCups << endl;
	cout << endl;
	cout << "Amount Due: " << " " << "$" << (totalSmCups + totalMedCups + totalLgCups) << endl;



}//end BuyCoffee

//begin cupsSold

void cupsSold(int& numberSmCups, int& numberMedCups, int& numberLgCups, int& lifeSmCups, int& lifeMdCups, int& lifeLgCups){

	lifeSmCups = lifeSmCups + numberSmCups;
	lifeMdCups = lifeMdCups + numberMedCups;
	lifeLgCups = lifeLgCups + numberLgCups;

}//end cupsSold              //function to calculate the total number of cups sold by size


//begin printCupsSold
void printCupsSold(int lifeSmCups, int lifeMdCups, int lifeLgCups){
	cout << "Total Small cups: " << " " << lifeSmCups << endl;
	cout << "Total Medium Cups: " << " " << lifeMdCups << endl;
	cout << "Total Large Cups: " << " " << lifeLgCups << endl;
	cout << endl;
	cout << "Total Cups: " << (lifeSmCups + lifeMdCups + lifeLgCups) << endl;
	cout << endl;
}//end printCupsSold        //function to print cupsSold to screen 



void totalAmount(double& totalSmCups, double& totalMedCups, double& totalLgCups){
	static double lifeSmAmount = 0;
	static double lifeMdAmount = 0;
	static double lifeLgAmount = 0;
	static double moneyMade = 0;

	lifeSmAmount = lifeSmAmount + totalSmCups;
	lifeMdAmount = lifeMdAmount + totalMedCups;
	lifeLgAmount = lifeLgAmount + totalLgCups;
	moneyMade = lifeSmAmount + lifeMdAmount + lifeLgAmount;

	cout << "Total: " << "$" << moneyMade << endl;
}//end totalAmount 
Last edited on
The options are to use by value and by reference. There is also return value. For example;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int byval( int x ) {
  x = 7;
  return x;
}

void byref( int & y ) {
  y = 42;
}

int main() {
  int z = 0;
  int w = byval( z );
  // z==0, w==7

  byref( z );
  // z==42

  return 0;
}

If we pseudo-inline the function calls, we get the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main() {
  int z = 0;
  int w;
  // byval equivalent
  {
    int x = z;
    x = 7;
    // z==0
    w = x;
  } // x does not exist any more
  // z==0, w==7

  // byref equivalent
  {
    int & y = z;
    y = 42;
  }
  // z==42

  return 0;
}

A reference is not a variable. It is an alias. Whatever you do to a reference, does happen to the referenced object.


Your functions take all arguments by reference and return no values.
There is no point in passing data by reference, if it is not modified at all within function.
There is no point in passing data by non-const reference, if caller's object should not be modified.

Your main() needs only three variables: counts of cups sold. Only one called function needs to change these variables.

The cup-sizes are constants and needed only for computing total volume.
The cup-prices are constants and needed only for computing total money.
Topic archived. No new replies allowed.