Help with functions returning values

Pages: 123
I am fairly new to programming, but a fast learner. I have an assignment in my C++ class that is to create a menu based coffee shop program. Here is what I have so far:

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
#include<iostream>
#include<cmath> //doubt this is needed but i added it just in case

using namespace std;

int choice1();
/*void choice2(); i commented these because i have not written the code
void choice3();   for them yet. i need to get the first function done
void choice4();   before moving on.
void help();*/

int main()
{
	int choice;
	const double small = 1.75, medium = 1.90, large = 2.00; 
	double totalPriceCups = 0.00;
	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";
		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:
			int choice1();
			double priceSmaCups, priceMedCups, priceLarCups;
			//tried to declare 'int smallCups, medCups, largeCups;' but still came back with errors.
			//how do i return the values of the function and continue the program?
			//i would like to declare 'double totalPriceCups' here as well, but kept receiving errors.
			priceSmaCups = small * smallCups;
			priceMedCups = medium * medCups;
			priceLarCups = large * largeCups;
			totalPriceCups = priceSmaCups + priceMedCups + priceLarCups;

			cout << "Your total comes to: " << totalPriceCups << endl;
			break;
		case 2:		cout << "Total Number of cups sold\n";
			break;
		case 3:		cout << "Total Amount of coffee sold\n";
			break;
		case 4:		cout << "Total Amount of money made\n";
			break;
		case 5:		cout << "Help\n";
			break;
		case 6:		cout << "End of Program.\n";
			startMenu = false;
			break;
		}
	}

   return 0;
}

int choice1()
{
		bool sizeMenu = true;
		int size, smallCups, medCups, largeCups;

		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;

			while ((size < 1) || (size > 4))
			{
				cout << "Not a valid choice. Please choose again.\n";
				cin >> size;
				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;
			}

				switch (size)
				{
				case 1:
					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;
				break;

				case 2:
					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;
				break;

				case 3:
					cout << "You have selected size: LARGE\n";
					cout << "The price is $1.90 per cup. How many cups would you like to purchase?\n";
					cin >> largeCups;
				break;

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


My questions are submitted within the code. Any suggestions would be greatly appreciated. I also looked in the tutorials before posting but could not really make much sense of it. I am more of a "do it myself a few times" type of learner.
Line 51 is a declaration, not a call. Try int someVar = choice1();
Just from having a very quick look at this, you cannot return multiple integers in choice1() function.

Line 51: use choice1(). When you call a function, you don't need to specify what it returns - it's defined in its definition.

What's wrong with choice1(): it doesn't return anything. Integers size, smallCups, medCups, largeCups are not initialized.

Again, I just took a very quick look, hope this helps at least a bit.
Last edited on
Just got some time to take a closer look, that's what I see so far.

For starters, #include<cmath> indeed is not needed.


1
2
3
priceSmaCups = small * smallCups;
priceMedCups = medium * medCups;
priceLarCups = large * largeCups;


Problem with this is that your variables you want to use in your equation are declared in your function. As soon as the function is executed, these go out of scope, hence you end up with uninitialized integers in the main().

I would simply declare those constants you have globally, calculate the total in the function and return it.

Then in your main:
1
2
3
case 1:
   cout << "Your total comes to: " << choice1();
   break;


By the way, be careful how you work with integers and doubles - your cost total could be truncated.

Try this out and see how it goes. Please bear in mind that I'm very new to C++, so there might be better ways around it.
I would argue against MarciusV's suggestion to use global variables. Either use output-style variables with references, or return a data structure.
I would argue against MarciusV's suggestion to use global variables. Either use output-style variables with references, or return a data structure
+1. global variables can cause bugs very easily and are messy
Correct me if I'm wrong, but I thought it was a good practice to use global variables if they are made constant, i.e. initialized right away and had fixed value throughout the life of a program? I can swear I've seen this principle in a couple of sources. Not going against you guys here, just want to learn since I have only a month experience in C++
but I thought it was a good practice to use global variables if they are made constant, i.e. initialized right away and had fixed value throughout the life of a program?
in c maybe. ive never had a need for them. its like goto. there are better ways of achieving the same affect
Noted.
Global constants are fine. Global variables are not.
Well that's exactly what I suggested doing in my original post
In that case, your suggestion would not work - the values need to change multiple times during the execution of the program.

This is a pile of miscommunications, lol.
I agree, but the only value that changes is user input, the price is constant. I don't know, I tried to compile my version and it works fine. But I'm best dealing with my own code though, I don't always see what sort of result others want.

Anyway, no response from op, so let's maybe drop it 😊
To MarciusV - in this assignment, #1 I am not supposed to use global variables.
#2 I do not like using global variables due to, as Little Bobby Tables said, they are messy and can cause bugs easily.
#3 'smallCups, medCups, largeCups' are user defined variables so I can not make them constants in this particular program.
But thank you for your input, gave me something to look at in the future.
#4 I understand that "choice(1)' does not return anything, that is what I am trying to rectify.

To LB - what would 'someVar' represent? is this a user defined variable or a constant? where would I initiate it, the function or main?

To Little Bobby Tables - I am not familiar with data structures. Could you go into a little more detail on that? Not asking to give me the code (I quite enjoy writing it myself... more satisfaction in that) just a simple example or a link to an example.

Thank you everyone for your speedy responses. I will post more of the code as I progress to show what I have come up with.

Here is the problem word for word so you can see what I am going for:
"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 cup 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 functions: 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, and a function to show the total money made. Your program should not use any global variables and special vales such as coffee sizes and cost of a coffee cup must be declared as named constants."
Side note, as I am looking at the program, I noticed that I will need to return the values from the first function, not only to do the calculations to find the total cost of the cups, but also in my second function to show the total amount of cups in each size. I am thinking I will need to adjust my code to change a few things in my first function to make this a shorter program to make it easier to understand. Any suggestions?
Possibly a few functions within each other...? Just seems like the code would be extra long and look messy. I am kind of a perfectionist and OCD about how my code looks... lol
dub1987 wrote:
To LB - what would 'someVar' represent? is this a user defined variable or a constant? where would I initiate it, the function or main?
Did you miss the keyword int in my post? That line is a full line of code. It declares the variable right then and there, and initializes it from the return value of calling your function.
Last edited on
Ah I see. I must have missed that. Trying to read everything fast. My eyes must have been moving faster than my brain. Anyways, I have adjusted my code a bit. Having a different issue now. I have created a different function for each size. Then created a function to do the calculations. I chose to delete function 'choice2()' due to the fact that it was doing the same thing as a different function. For some reason, it will run through, choose 1 on the first menu, then select 4 to exit out of the second menu. But instead of it exiting out of the second menu, it goes through each choice of the second menu, does the math and exits to the main menu. here is the code, hope it is not too confusing:

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

using namespace std;

int choice1();
int size1();
int size2();
int size3();
double price();
/*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: $" << price() << endl;
			break;
		
		case 2:		
			cout << "Total number of SMALL cups: " << size1() << endl;
			cout << "Total number of MEDIUM cups: " << size2() << endl;
			cout << "Total number of LARGE cups: " << size3() << 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 size1()
{
	int smallCups;

	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;

	return smallCups;
}
int size2()
{
	int medCups;

	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;

	return medCups;
}
int size3()
{
	int largeCups;

	cout << "You have selected size: LARGE\n";
	cout << "The price is $1.90 per cup. How many cups would you like to purchase?\n";
	cin >> largeCups;

	return largeCups;
}
double price()
{
	const double small = 1.75, medium = 1.90, large = 2.00;
	double priceSmaCups, priceMedCups, priceLarCups, totalPriceCups = 0.00;

	setprecision(2);
	priceSmaCups = small * size1();
	priceMedCups = medium * size2();
	priceLarCups = large * size3();
	totalPriceCups = priceSmaCups + priceMedCups + priceLarCups;

	return totalPriceCups;
}
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;
}
Lines 37 and 46 probably shouldn't both exist at the same time - I recommend removing line 37.
To LB - Ah yes... of course. I will make that change. I did not see that. Probably would have caught it when I started my final debugging process. Thanks for catching that.

Does anyone know why when going into the second menu (function 'choice1') and selecting 4, the program does not exit the function and continue the program? Instead it goes though the function again, overwriting the original responses (if selected) and then exits. I'm confused with this one. My instructor was having a hard time finding the issue as well. He is supposed to be looking at the program and let me know what he comes up with, but I have not heard from him just yet.
Pages: 123