Help with functions returning values

Pages: 123
Alright... I am working on it still. I made the suggested changes. Here is what happens now. I declared variables to hold the values of the functions like this
1
2
int cupSmall = size1(), cupMed = size2(), cupLar = size3();
double totalCost = price();

Since I have them declared them at the top of main, the compiler reads that I called those functions and then stores the values in main now. When the console pops up, the first thing it does is goes into size1(); and so on and then returns to main. Which it should go to the main menu, then let you make a selection before it goes into the second menu. How would I link int cupSmall; and the others in main from the function? I hope I am making sense. I know there is a way to link the same variable between a function and main without declaring it globally.
You could declare the variables at the top of main, perhaps initializing them to a default value. Then assign them the value from the called function later when appropriate. Or you could just hold off on declaring them until you need them in the program.
I tried declaring them when I needed them in main, but where I need them, it calls the functions before the main menu still. I changed them to global variables and changed the functions to void size1(); and made cupSmall = smallCups. That works, but I don't like the look of it and really don't like using global variables if I can avoid it.
you can declare

int main()
{
//variable declarations including
int cupSmall = 0; // declare variable and initialize to default value - don't call the function here

//code here - menu choices etc.
//code here
//code here
//then when needed
cupSmall = size(1); assign result of function to cupSmall variable

}//end main
Hmm... interesting. I will give that a try and see what happens.
that didn't work. I changed them back to global variables and voids instead of ints. It works that way, looks a little messy, but it works. Only thing is, it is not keeping a running total of how many cups are sold. Example, if I go through the program and select quantity 1 for each size, then exit to main menu, then select 2 to see how many cups were sold, it shows 1 of each. Then I run through the program again (without exiting the console), and select quantity 3 for each size, and then it shows 3 for each size instead of 4. I assume a loop of some kind would be in order, but I'm not sure which loop and where.
Can you post your most recent code?
I apologize for the lateness of my response but here is the most recent changes to my code with global variables. I will change them back the way you suggested and repost the code a little later today.

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
#include<iostream>
#include<iomanip>

using namespace std;

int choice1();
void size1();
void size2();
void size3();
double price();

int smallCups, medCups, largeCups;
double priceSmaCups, priceMedCups, priceLarCups, totalPriceCups;
/*void choice3();
void choice4();
void help();*/

int main()
{
	int choice;
	bool startMenu = true;

	while (startMenu != false)
	{
		cout << "*************************************\n";
		cout << " 1 - Select Coffee Size.\n";
		cout << " 2 - Show Total Number of Cups Sold of Each Size.\n";
		cout << " 3 - Show Total Amount of Coffee Sold.\n";
		cout << " 4 - Show Total Amount of Money Made.\n";
		cout << " 5 - Help With This Program.\n";
		cout << " 6 - End This Program.\n";
		cout << "Enter your choice and press return: \n";
		cout << "*************************************\n";
		cin >> choice;
		system("cls");

		while ((choice < 1) || (choice > 6))
		{
			cout << "Not a valid choice. Please choose again.\n";
			cin >> choice;
			cout << "*************************************\n";
			cout << " 1 - Select Coffee Size.\n";
			cout << " 2 - Show Total Number of Cups Sold of Each Size.\n";
			cout << " 3 - Show Total Amount of Coffee Sold.\n";
			cout << " 4 - Show Total Amount of Money Made.\n";
			cout << " 5 - Help With This Program.\n";
			cout << " 6 - End This Program.\n";
			cout << "Enter your choice and press return: \n";
			cin >> choice;
			system("cls");
		}
		
		switch (choice)
		{
		case 1:
			choice1();
			cout << "Your total comes to: $" << totalPriceCups << endl;
			break;
		
		case 2:
			cout << "Total number of SMALL cups: " << smallCups << endl;
			cout << "Total number of MEDIUM cups: " << medCups << endl;
			cout << "Total number of LARGE cups: " << largeCups << endl;
			break;
		
		/*case 3:		cout << "Total Amount of coffee sold\n";
			break;
		
		case 4:		cout << "Total Amount of money made\n";
			break;
		
		case 5:
			help();
			cout << "Help\n";
			break;
		
		case 6:		cout << "End of Program.\n";
			startMenu = false;
			break;*/
		}
	}

   return 0;
}

int choice1()
{
	bool sizeMenu = true;
	int size;

	while (sizeMenu != false)
	{
		cout << "Please select which size coffee you would like to purchase.\n";
		cout << " 1 - Small\n";
		cout << " 2 - Medium\n";
		cout << " 3 - Large\n";
		cout << " 4 - Finished Selecting Coffee Size(s)\n";
		cout << "Enter your choice and press return: \n";
		cin >> size;
		system("cls");

		while ((size <= 0) || (size >= 5))
		{
			cout << "Not a valid choice. Please choose again.\n";
			cout << "Please select which size coffee you would like to purchase.\n";
			cout << " 1 - Small\n";
			cout << " 2 - Medium\n";
			cout << " 3 - Large\n";
			cout << " 4 - Finished Selecting Coffee Size(s)\n";
			cout << "Enter your choice and press return: \n";
			cin >> size;
			system("cls");
		}

		switch (size)
		{
		case 1:
			size1();
			break;

		case 2:
			size2();
			break;

		case 3:
			size3();
			break;

		case 4:
			cout << "End of size selection.\n";
			sizeMenu = false;
			break;
		}
	}
	return 0;
}

void size1()
{
	cout << "You have selected size: SMALL\n";
	cout << "The price is $1.75 per cup. How many cups would you like to purchase?\n";
	cin >> smallCups;
}
void size2()
{
	cout << "You have selected size: MEDIUM\n";
	cout << "The price is $1.90 per cup. How many cups would you like to purchase?\n";
	cin >> medCups;
}
void size3()
{
	cout << "You have selected size: LARGE\n";
	cout << "The price is $2.00 per cup. How many cups would you like to purchase?\n";
	cin >> largeCups;
}
double price()
{
	const double small = 1.75, medium = 1.90, large = 2.00;

	setprecision(2);
	priceSmaCups = small * smallCups;
	priceMedCups = medium * medCups;
	priceLarCups = large * largeCups;
	totalPriceCups = priceSmaCups + priceMedCups + priceLarCups;
	
	return totalPriceCups;
}
Generally I'd avoid global variables, but I think having global constants for the coffee prices and ounces per cup is OK. This way, if the store changes prices or ounces per cup size, the updates only have to be made to the global constants. (As long as all the functions are using the constant names instead of hardcoding specific prices or other numbers.)

I haven't tested this completely so it might not be bugfree, but this is what I was thinking about - having one set of variables that track the number of cups in the current sale, then having another set that increment at the end of each sale to keep track of the total cups of each type so far.

The total variables are sent by reference to the coffee sale function, so the sale function has access to update the original variables. The &s in the function prototype and definition means those variables are being sent by reference instead of by value (in which case the function just gets a copy of the value in the variable).



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
#include <iostream>
#include <iomanip>
using std::cout; using std::cin; using std::endl; 

//global constants for coffee prices and ounces
const double priceSmallCoffee = 1.75;
const double priceMediumCoffee = 1.90;
const double priceLargeCoffee = 2.00;
const int ouncesSmallCoffee = 9;
const int ouncesMediumCoffee = 12;
const int ouncesLargeCoffee = 15;

//function prototypes

int storeMenu();
void coffeeSales(int&, int&, int&);//function takes parameters by reference so it can update daily totals
int coffeeMenu();
int getCups();
double totalSale(int,int,int);
void coffeeByCupSize(int,int,int);
void coffeeByOunces(int,int,int);
void coffeeByDollars(int,int,int);

int main()
{

	bool storeOpen = true;
	int mainChoice = 0;
	int todaySales_SmallCups = 0;
	int todaySales_MediumCups = 0;
	int todaySales_LargeCups = 0;

	do
	{
		mainChoice = storeMenu();
		switch(mainChoice)
		{
			case 1: 	coffeeSales(todaySales_SmallCups,todaySales_MediumCups,todaySales_LargeCups);
					break;
			case 2:		coffeeByCupSize(todaySales_SmallCups,todaySales_MediumCups,todaySales_LargeCups);
					break;
			case 3:		coffeeByOunces(todaySales_SmallCups,todaySales_MediumCups,todaySales_LargeCups);
					break;
			case 4:		coffeeByDollars(todaySales_SmallCups,todaySales_MediumCups,todaySales_LargeCups);
					break;
			case 5:		cout << "help function here" << endl;
					break;
			case 6:		storeOpen = false;
					break;
			default:	cout << "Shouldn't ever see this";						break;
		}
		
	}while(storeOpen != false);

	cout << "Coffee Shop is now closed. Goodbye!" << endl;

	return 0;	
}

int storeMenu()
{
	int choice;
	cout << "Welcome to the Coffee Shop" << endl;
	cout << "**************************" << endl;
	cout << "1. Sell Coffee" << endl;
	cout << "2. Coffee Sold Today - By Cup Size" << endl;
	cout << "3. Coffee Sold Today - By Ounces" << endl;
	cout << "4. Coffee Sold Today - By Dollars" << endl;
	cout << "5. Help" << endl;
	cout << "6. Close Store" << endl;
	cout << "\nEnter selection (1-6): " << endl;
	cin >> choice;
	while (!(choice >= 1 && choice <= 6))
	{
		cout << "Invalid Selection - Please re-enter: ";
		cin >> choice;
	}
	return choice;
}

void coffeeSales(int& todaySmall, int& todayMedium, int& todayLarge)
{
	int cups = 0;
	int thisSale_SmallCups = 0;
	int thisSale_MediumCups = 0;
	int thisSale_LargeCups = 0;
	double thisSale_total = 0.0;
	int coffeeChoice = 0;
	
	do
	{
		coffeeChoice = coffeeMenu();
	
		switch(coffeeChoice)
		{
			case 1:	cups = getCups();
				thisSale_SmallCups += cups;
				break;
			case 2: cups = getCups();
				thisSale_MediumCups += cups;
				break;
			case 3: cups = getCups();
				thisSale_LargeCups += cups;
				break;
			case 4: thisSale_total = totalSale(thisSale_SmallCups,thisSale_MediumCups,thisSale_LargeCups);
				cout << "\nTotal price is $" << thisSale_total << endl;
				//increment daily totals with the current sale
				todaySmall += thisSale_SmallCups;
				todayMedium += thisSale_MediumCups;
				todayLarge += thisSale_LargeCups;
				break;
		}
	
	}while (coffeeChoice != 4);
	
}

int coffeeMenu()
{
	int choice = 0;
	cout << "Coffee Menu" << endl;
	cout << "***********" << endl;
	cout << "1. Small" << endl;
	cout << "2. Medium" << endl;
	cout << "3. Large" << endl;
	cout << "4. End Sale" << endl;
	cout << "Enter Selection (1-4):";
	cin >> choice;
	while (!(choice >= 1 && choice <=4))
	{
		cout << "Invalid Selection - Please re-enter: ";
		cin >> choice;
	}
	return choice;
}

int getCups()
{
	int cups = 0;
	cout << "\nHow many cups?: ";
	cin >> cups;
	cout << endl;
	return cups;
}

double totalSale(int small,int medium,int large)
{
	if (small>0)
	{
		cout << "\nSmall Cups: " << small << " at $" << priceSmallCoffee << " each: " << small*priceSmallCoffee << endl;
	}
	if (medium>0)
	{
		cout << "Medium Cups: " << medium << " at $ " << priceMediumCoffee << " each: " << medium*priceMediumCoffee << endl;
	}
	if (large>0)
	{
		cout << "Large Cups: " << large << " at $" << priceLargeCoffee << " each: " << large*priceLargeCoffee << endl;
	}
	return (small*priceSmallCoffee) + (medium*priceMediumCoffee) + (large*priceLargeCoffee);
}

void coffeeByCupSize(int small,int medium,int large)
{
	cout << "Coffee Sold Today - Small Cups: " << small << endl;
	cout << "Coffee Sold Today - Medium Cups: " << medium << endl;
	cout << "Coffee Sold Today - Large Cups: " << large << endl;
}

void coffeeByOunces(int small,int medium,int large)
{
	cout << "Coffee Sold Today - Total Ounces: " << (small*ouncesSmallCoffee) + (medium*ouncesMediumCoffee) + (large*ouncesLargeCoffee) << endl;	
}

void coffeeByDollars(int small,int medium,int large)
{
	cout << "Coffee Sold Today - Total Dollars: $" << (small*priceSmallCoffee) + (medium*priceMediumCoffee) + (large*priceLargeCoffee) << endl;
}
Well... I was not expecting you to write the code (and I will not copy, but modify my own to make it work based on what you have here). I will give it a try and see how it works so I can find the errors in my own. I really appreciate your help. I'll be in touch to see what happens.
Your program works just fine. I am about to go line by line and see where my mistakes are. Again, thank you.
I have a question. You mentioned that the int& was to get a copy of the value. The one that has 3 of those, void coffeeSales(int&, int&, int&);, is that for each size (small, medium, and large)? Then the other functions that only have (int, int, int); are passing the copy to the function with the int& statements?
int& is passing by reference, so in this case...

1
2
coffeeSales(todaySales_SmallCups,todaySales_MediumCups,todaySales_LargeCups);
void coffeeSales(int& todaySmall, int& todayMedium, int& todayLarge)


...todaySmall is an alias, or just another name for, the todaySales_SmallCups variable. If I make a change to todaySmall, it's actually making a change to the todaySales_SmallCups variable.


In this other case, the variables are being passed by value - so copies of the values in the thisSale_ variables are sent to the totalSale function, but small, medium and large are just local variables in the totalSale function holding those values. A change to small etc. will not change the original variable sent to the function.

1
2
totalSale(thisSale_SmallCups,thisSale_MediumCups,thisSale_LargeCups);
double totalSale(int small,int medium,int large)
Ok. I think I got it. So, the compiler assumes the variables "small, medium, and large" in

1
2
totalSale(thisSale_SmallCups,thisSale_MediumCups,thisSale_LargeCups);
double totalSale(int small,int medium,int large)


and

1
2
coffeeSales(todaySales_SmallCups,todaySales_MediumCups,todaySales_LargeCups);
void coffeeSales(int& todaySmall, int& todayMedium, int& todayLarge)


based on what the functions are doing and stores their values in the todaySmall ect. therefore there is no need to actually hardcode it in the function at the top?
therefore there is no need to actually hardcode it in the function at the top?


I think you're talking about the function prototypes before the main function starts? You don't need to include the parameter names in function prototypes - the compiler will ignore them. In the function prototype, the compiler just needs to know the return type, function name, and the type and number of parameters the function will receive.

In the first case with totalSale - small, medium and large are local variables in the totalSale function. They get a copy of the value of the this_Sale_SmallCups etc. variables that were sent with the function call. Changes to small, medium, and large remain local to the totalSale function, they do not change the this_Sale variable back in the calling function.

In the second case with coffeeSales, todaySmall etc. are references or aliases to the variables todaySales_SmallCups etc. that were sent with the function call. Changes to todaySmall etc. are actually changing the todaySales variables back in the calling function.
That makes sense. I'm still working on mine, almost done just have a few kinks I am working out and I should have a fully functional program.
Alright! It works! My small kink that took me most of the night and a good portion of this morning was that I named a function that was already a function in the library, thus giving me a linker error. I finally decided to change the name of the function and it started working. Thanks again for all the help!
Congrats - glad you got it working :)
Thanks. Could not have done it without your help, along with that other program you helped me with! I truly appreciate all your help. I will probably be in touch as my projects are only going to get more difficult.
Topic archived. No new replies allowed.
Pages: 123