Need Assistance w/ Void fxs

Nov 4, 2015 at 7:32pm
Hi all, I have a problem with void functions. This program is supposed to be setup with the only two global variables (which I've added)and with void functions for referencing. I keep getting stuck in infinite loops, can't go back to main menu, etc. It's barely where it needs to be but I'm hoping someone can make the necessary additions. Please take a look and (void functions required) let me know if you can fix this menu-driven program, and include comments if you can.. I'd really appreciate knowing how to use these voids better in the future. Thanks.

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

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cctype>

using namespace std;

double smallSize = 9, mediumSize = 12, largeSize = 15; //in OZ
double smallPrice = 1.75, mediumPrice = 1.90, largePrice = 2.00; //in $0.00

void sellCoffee(int &smallCups, int &mediumCups, int &largeCups)
{
	char size = 0;
	int numOfCups = 0;

	cout << "Enter '$' at any time to return to the main menu." << endl;

	while (size != '$')
	{
		cout << "What size cup do you want? (S, M, L,)" << endl;
		cin >> size;
		cout << "How many cups do you want?" << endl;
		cin >> numOfCups;

		if (islower(size))
		{
			size = toupper(size);
		}
		switch (size)
		{
		case 'S': smallCups = smallCups + numOfCups;
			break;
		case 'M': mediumCups = mediumCups + numOfCups;
			break;
		case 'L': largeCups = largeCups + numOfCups;
			break;
		case '$': exit;
			break;
		default: cout << "Invalid option..." << endl;
			break;
		}
	}
}
void reportCups(int &smallCups, int &mediumCups, int &largeCups)
{

}
void reportOunces(int &smallCups, int &mediumCups, int &largeCups)
{

}
void reportRevenue(int &smallCups, int &mediumCups, int &largeCups)
{

}

int main()
{
	int smallCups = 0, mediumCups = 0, largeCups = 0;
	int userChoice = 0;

	cout << "Please, select a menu option (1-5). " << endl << endl;
	cout << "1. Sell Coffee" << endl << endl;
	cout << "2. Cups Report" << endl << endl;
	cout << "3. Ounces Report" << endl << endl;
	cout << "4. Revenue Report" << endl << endl;
	cout << "5. Help" << endl << endl;
	cin >> userChoice;

	while (userChoice >= 1 || userChoice <= 5 && userChoice != 6)
	{
		switch (userChoice)
		{
		case 1: sellCoffee(smallCups, mediumCups, largeCups);
			break;
		case 2: reportCups(smallCups, mediumCups, largeCups);
			break;
		case 3: reportOunces(smallCups, mediumCups, largeCups);
			break;
		case 4: reportRevenue(smallCups, mediumCups, largeCups);
			break;
		case 5: cout << "To use this program simply choose a number (1-5)." << endl;
			cout << "Entering a '$' within the sub-menus will return you to the main menu." << endl;
			cout << "Entering a '6' will end the program." << endl;
			break;
			return 0;
		default: cout << "Invalid choice..." << endl;
		}
	}
	system("pause");

	return 0;
}
Nov 4, 2015 at 8:04pm
Line 38: case '$': exit(0);

Also, you need

cin >> userChoice;

somewhere in the while loop. There are many possibilities, but the simplest change would be to add it to the end of while loop.

Nov 4, 2015 at 10:57pm
I edited my exit(0); but that exits the entire program. Is there a way to exit from that void function (sub-menu) and go back to the main menu?
Nov 4, 2015 at 11:01pm
Use the return statement (without return value) like:

return;

It exits from the function.
Nov 4, 2015 at 11:53pm
return does nothing inside a void function, so I hear
Nov 5, 2015 at 12:05am
You hear wrong.

Return exits a void function. Return exits any function.
Nov 5, 2015 at 12:13am
Okay I made some corrections but I'm still getting stuck in a while loop in the void sellCoffee function. Also, I'm trying to add all the references together into a new variable. Is that possible? Apparently, I'm getting an error for it.

edit: this line: totalSold = &smallCups + &mediumCups + &largeCups;

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

using namespace std;

double smallSize = 9, mediumSize = 12, largeSize = 15; //in OZ
double smallPrice = 1.75, mediumPrice = 1.90, largePrice = 2.00; //in $0.00

void sellCoffee(int &smallCups, int &mediumCups, int &largeCups)
{
	char size = 0;
	int numOfCups = 0;

	cout << "Enter '$' at any time to return to the main menu." << endl;

	while (size != '$')
	{
		cout << "What size cup do you want? (S, M, L,)" << endl;
		cin >> size;
		cout << "How many cups do you want?" << endl;
		cin >> numOfCups;

		if (islower(size))
		{
			size = toupper(size);
		}
		switch (size)
		{
		case 'S': smallCups = smallCups + numOfCups;
			break;
		case 'M': mediumCups = mediumCups + numOfCups;
			break;
		case 'L': largeCups = largeCups + numOfCups;
			break;
		case '$': 
			return;
		default: cout << "Invalid option..." << endl;
			break;
		}
	}
}
void reportCups(int &smallCups, int &mediumCups, int &largeCups)
{
	/*int totalSold = 0;
	totalSold = &smallCups + &mediumCups + &largeCups;
	cout << "Total Cups Sold" << endl;
	cout << "-------------------" << endl;
	cout << left << "Small Cups: " << &smallCups << endl;
	cout << left << "Medium Cups: " << &mediumCups << endl;
	cout << left << "Large Cups: " << &largeCups << endl;
	cout << left << "Total Sold: " << totalSold << endl;*/
}
void reportOunces(int &smallCups, int &mediumCups, int &largeCups)
{

}
void reportRevenue(int &smallCups, int &mediumCups, int &largeCups)
{

}

int main()
{
	int smallCups = 0, mediumCups = 0, largeCups = 0;
	int userChoice = 0;

	cout << "Please, select a menu option (1-5). " << endl << endl;
	cout << "1. Sell Coffee" << endl << endl;
	cout << "2. Cups Report" << endl << endl;
	cout << "3. Ounces Report" << endl << endl;
	cout << "4. Revenue Report" << endl << endl;
	cout << "5. Help" << endl << endl;

	while (userChoice >= 1 || userChoice <= 5 && userChoice != 6)
	{
		switch (userChoice)
		{
		case 1: sellCoffee(smallCups, mediumCups, largeCups);
			break;
		case 2: reportCups(smallCups, mediumCups, largeCups);
			break;
		case 3: reportOunces(smallCups, mediumCups, largeCups);
			break;
		case 4: reportRevenue(smallCups, mediumCups, largeCups);
			break;
		case 5: cout << "To use this program simply choose a number (1-5)." << endl;
			cout << "Entering a '$' within the sub-menus will return you to the main menu." << endl;
			cout << "Entering a '6' will end the program." << endl;
			break;
			return 0;
		default: cout << "Invalid choice..." << endl;
		}
		cin >> userChoice;
	}
	system("pause");

	return 0;
}
Last edited on Nov 5, 2015 at 12:15am
Nov 5, 2015 at 12:44am
It seems to be working OK.

Perhaps, to make it easier to use, copy the lines 69-74 just before line 95.
Nov 5, 2015 at 12:46am
totalSold = smallCups + mediumCups + largeCups;
Nov 5, 2015 at 2:05am
Got it thanks for your help :D
Topic archived. No new replies allowed.