Help with functions returning values

Pages: 123
My instructor said something about not having a default: statement. I'm not fully convinced that is the issue though. I have tried putting in the statement with the
1
2
3
4
5
6
7
8
9
10
11
12
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");
			}

conditions. Just doesn't work properly. Correct me if I'm wrong, but I don't NEED a default: statement in order for the switch to work. Please help. I am more confused now than I was when initially starting this program.
Correct me if I'm wrong, but I don't NEED a default: statement in order for the switch to work.


You're right, you don't necessarily need it. It acts similarly to else statement in IF loops: it's executed only if no other specified condition is met. And, of course, if you leave it out - compiler simply goes to the next bit of code.
But it is good practice to always have a default case or else statement, for the precise reason that it catches unexpected values.

If one puts the switch inside a while loop, then any unexpected input will cause the loop to run again, and will save having complicated conditions for the loop.

Btw, don't have using namespace std; -it defeats the purpose of namespaces. Instead, put std:: before every std thing, as in std::cout, std::cin, std::endl, std::string etc.

Also, your code could do with more functions, there is an unwritten rule that functions should not exceed 40 Lines Of Code say. There are lots of candidates for code that could be in a function (unless it's only a few lines): Body of loops, switch cases, if, else if, else compound statements. This can make your code much easier to understand.

HTH
But it is good practice to always have a default case or else statement, for the precise reason that it catches unexpected values.
it is possible to know that what is being tested will be valid and you then dont need a default.
Little Bobby Tables wrote:
it is possible to know that what is being tested will be valid and you then dont need a default.


TheIdeasMan wrote:
If one puts the switch inside a while loop, then any unexpected input will cause the loop to run again, and will save having complicated conditions for the loop.


Also, for simple menu operations like this, one can avoid the validation tests as well, because the switch takes care of it. The switch also allows char or int as input

I think it is prudent to put a catch-all in regardless, what if there is some quirk that causes your validation tests to fail? I am not against validation testing at all, it is essential, but this example shows how to combine the validation with the selection.

With the gcc compiler, one can enable a warning when there is no default in a switch.

With Lowest0ne's code with the < and > operators, it does restrict the input - look carefully at how that works.

Here is an example I gave a long time ago, also there is code by JLBorges just after my reply.

http://www.cplusplus.com/forum/beginner/104553/2/#msg564228
Well I use using namespace std; because that is the standard my instructor has gave us from the first day of class. As you say, it is better to use std:: where needed. When I am not doing assignments for him, I will begin making this a good practice, but for now, I will do it his way. Can we get back to the topic at hand though? I still haven't gotten this program to work properly and I am starting to get really frustrated with it to the point I may scrap it and start over with something new.
care to remind us all the current issue? also if you could post your source
lol. My apologies for not mentioning that.

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 $2.00 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;
}
The topic was: 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.
If you choose 1 at the first Start menu, you run this case.

1
2
3
case 1:	choice1();
cout << "Your total comes to: $" << price() << endl;
break;


In the choice1 function, if you choose 4, you run this case and exit that loop.

1
2
3
case 4:	cout << "End of size selection.\n";
sizeMenu = false;
break;


But - even though you've selected no coffee option, you still move on to the second step in case 1 above where you print the total and call the price function. In turn the price function calls all the size functions where you ask the person to enter how many cups they want.
Exactly. So should the cout << "Your total comes to: $" be inside the choice1(); function?

And as far as it going back through the sizes, I have not a clue why it is doing that. I understand what You are saying, but not sure where my error is.
It's calling the size functions because in the price function you have this - this calls each of the size functions:

1
2
3
	priceSmaCups = small * size1(); 
	priceMedCups = medium * size2();
	priceLarCups = large * size3();


You call them again here in case 2 from the first menu which seems like it's supposed to keep a running total of how many of each coffee size has been sold so far.

1
2
3
4
5
		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;


Ahhhh... I see now. I was thinking by calling those functions again, they already had a stored value, so I thought I was calling the return values of said functions. How do I just call the return values of those functions?

1
2
3
smallCups;
         medCups;
         largeCups;
?

I am surprised my instructor did not spot that.
The return value of a function is not "stored" anywhere - it is always immediately passed to you. If you don't do anything with the return value, it just disappears.
Can I pass them to a different variable where it can be stored?

example:

1
2
3
4
5
6
7
8
9
10
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;
}


then call it back to main where the value can be stored in a different variable... i.e. size1() = cupSmall?
Once a called function terminates, it's gone - its call stack in memory is destroyed, along with any local variables you declared in it. The number of cups sold aren't being stored or incremented anywhere once you're done with the size function.

You could have variables to hold running totals, but you'd want those accessible in main if that's where you going to call the option to report on total cups sold. I might consider using arrays here, but I'm not sure if you've covered those yet?

Looks like you'll also want to keep track of the $ coming in?
wildblue - No we have not covered arrays just yet, (although I am working a project in a different chapter that uses arrays) but I am not supposed to use arrays in this program. "Your supposed to master the menu switch... case: process first." says the instructor. I believe arrays would be much better and easier for this type of program. And yes it is supposed to keep a running total of how much money is being made. I will post the actual assignment from the book.
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."
I think I would have variables in main (e.g. total_small_sold, total_medium_sold, total_large_sold) that keep track of the total number of each cup type sold overall between all individual sales. These totals will be used to output the number of cups of each size sold for (b), multiplied by the # of ounces per size for (c), then multiplied by the price per cup for (d). I'd increment those when the user ends the current sale. (Those variables would be passed to the sale function by reference so the function can update them at the end.)

The sale function could have local versions of those number of cup variables to keep track of the number of each cup sold to that particular customer (e.g. this_sale_small_sold, this_sale_medium_sold, this_sale_large_sold). Those could be used to calculate the total price for that customer, and then be added to the overall cups sold totals when the user exits the sale function. When the sale function is called again, those variables will be back at 0 to start fresh with the next customer.
That makes way more sense! I'm going to take another stab at it and see what I can do. I'll post again when I either get stuck or if I get it... hopefully the second option lol.
Pages: 123