Results from one function transferred to another

closed account (jEb91hU5)
I am having trouble figuring out how to transfer the results from "menu2()" and using that information in "Sell_coffee()." Some comments were made from previous help, but I am not well versed enough to know what arguments need to be made to call that information in the other function. I also know some things need to be renamed to as to not be so confusing (e.g. x, y, a). I'd appreciate any help. 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
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
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>


using namespace std;

void menu();
void menu2();
void Sell_coffee();						
void Show(int y);						//(int cups)
void Show(float x);						//(float ounces)
void Show(double a);					//(double money)


int main()
{
	cout << "Welcome to the coffee shop!\n";
	menu();
}

void menu()
{
	int choice, cups = 0; 
	float ounces = 0;
	double money = 0;

	cout << "\n::Please select from the following menu:\n"
		<< "\n1. Buy Coffee\n"
		<< "2. Show the total cups of coffee that has been sold\n"
		<< "3. Show how many ounces of coffee has been sold\n"
		<< "4. Show the total amount of money the coffee shop has made\n"
		<< "5. Exit\n"
		<< "\nEnter Choice: ";
	cin >> choice;

	while (choice != 5) 
	{
		switch (choice)
		{
		case 1: Sell_coffee();
			break;
		case 2: Show(cups);
			break;
		case 3: Show(ounces);
			break;
		case 4: Show(money);
			break;
		default: cout << "\nPlease try again\n";
		}
		cout << "\n::Please select from the following menu:\n"
			<< "\n1. Buy Coffee\n"													//Sell coffee in any size and any number of cups
			<< "2. Show the total cups of coffee that has been sold\n"				//Display the total number of cups sold of each size
			<< "3. Show how many ounces of coffee has been sold\n"					//Display the total amount of coffee sold (in ounces)
			<< "4. Show the total amount of money the coffee shop has made\n"		//Display the total money made
			<< "5. Exit\n"
			<< "\nEnter Choice: ";
		cin >> choice;
	}
}

void menu2()
{
	//All these variables are destroyed when the function call is over. Meaning Any information saved in them will be gone.
	//Consider making the variables in int main() and using them as arguments or making global variables
	int choice2;
	float ounces;
	string size;
	double money;

	cout << "\n::What size coffee:\n"
		<< "\n1. Small\n"
		<< "2. Medium\n"
		<< "3. Large\n"
		<< "\nEnter Choice: ";
	cin >> choice2;
	if (choice2 == 1)
	{
		size = "Small";
		ounces = 9;
		money = 1.75;
	}
	else if (choice2 == 2)
	{
		size = "Meduim";
		ounces = 12;
		money = 1.90;
	}
	else if (choice2 == 3)
	{
		size = "Large";
		ounces = 15;
		money = 2.00;
	}
	else
	{
		cout << "Invalid input\n";
	}
}

void Sell_coffee()
{
	float cups = 0;
	double money = 0;
	//Money should be dependant on the size of the drink
	//You can send the value of "Money" from menu2 to this function through an argument
	menu2();
	cout << "How many cups would you like? \n";
	cin >> cups;
	money = money * cups;
}

void Show(int y)
{
	cout << "The total number of cups sold is " << y << endl;
}

void Show(float x)
{
	cout << "The total number of ounces sold is " << x << endl;
}

void Show(double a)
{
	cout << fixed << showpoint << setprecision(2);
	cout << "The total amount of money the coffee shop has made is $" << a << endl;
}
I went overboard. I wanted to only explain, but I ended up simply writing the whole code so it would work as intended.

I commented out your code when it shouldn't be there.

Anyway, to explain. You kept making local variables. Local variables have their values disappear as soon as you're done with that function. So I got rid of your local variables for ounces, money, and cups and made them GLOBAL variables. This way, you can edit them and they'll stay the same value throughout the program unless changed. By using them this way, you can keep track of the TOTAL money/ounces/cups.

I made a few local variables (which I passed by reference) in order to do some calculations with them.

Look at the code and learn from it. Try to make your code work by doing something different to really benefit. What I did was only one way to solve it. If there's something you don't understand, let me know.

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


using namespace std;

void menu();
void menu2(double &Heremoney, float &Hereounces); //Added Arguments
void Sell_coffee();
/*void Show(int y);						//(int cups)
void Show(float x);						//(float ounces)
void Show(double a);					//(double money)*/
//You have 3 functions named "Show". Each one does a different thing so you don't want the program to choose the wrong one.
//Therefore, Simply Change the names of these functions
//EX:
void Show1(int y);
void Show2(float x);
void Show3(double a);
//^That way you're not guessing which function will be called.


double money = 0; //Declared here Instead So That You Can Keep Track Of The Money You Make Throughout The Program
float ounces = 0; //Same Reason - To keep track of all ounces sold
int cups = 0;

int main()
{
	cout << "Welcome to the coffee shop!\n";
	menu();
}

void menu()
{
	//Instead of making them here, make them in Global Scope Or Continue To Pass It to Functions Through Arguments
	//So That They Aren't Accidently Destroyed Before The End Of The Program
	//int choice, cups = 0;
	int choice = 0; //Made variable cups in global scope
	//float ounces = 0; //Not Being Used As Intended - Doesn't keep track of any ounces
	//double money = 0; //Not Being Used As Intended - DOES NOT KEEP TRACK OF ANY MONEY

	cout << "\n::Please select from the following menu:\n"
		<< "\n1. Buy Coffee\n"
		<< "2. Show the total cups of coffee that has been sold\n"
		<< "3. Show how many ounces of coffee has been sold\n"
		<< "4. Show the total amount of money the coffee shop has made\n"
		<< "5. Exit\n"
		<< "\nEnter Choice: ";
	cin >> choice;

	while (choice != 5)
	{
		switch (choice)
		{
		case 1: Sell_coffee();
			break;
		case 2: Show1(cups); //Renamed Functions
			break;
		case 3: Show2(ounces);
			break;
		case 4: Show3(money);
			break;
		default: cout << "\nPlease try again\n";
		}
		cout << "\n::Please select from the following menu:\n"
			<< "\n1. Buy Coffee\n"													//Sell coffee in any size and any number of cups
			<< "2. Show the total cups of coffee that has been sold\n"				//Display the total number of cups sold of each size
			<< "3. Show how many ounces of coffee has been sold\n"					//Display the total amount of coffee sold (in ounces)
			<< "4. Show the total amount of money the coffee shop has made\n"		//Display the total money made
			<< "5. Exit\n"
			<< "\nEnter Choice: ";
		cin >> choice;
	}
}

void menu2(double &Heremoney, float &Hereounces) //Take Arguments To Save Data To Variables For Use In "Sell_coffee"
//Pass By Reference So Values Change For The Variables Passed In
{
	//All these variables are destroyed when the function call is over. Meaning Any information saved in them will be gone.
	//Consider making the variables in int main() and using them as arguments or making global variables

	//The variable "money" will NOT write to the variable money in menu() - Same With ouncess
	//They are considered two different variables since both were declared and within different functions
	int choice2;
	//float ounces;
	string size;
	//double money;

	cout << "\n::What size coffee:\n"
		<< "\n1. Small\n"
		<< "2. Medium\n"
		<< "3. Large\n"
		<< "\nEnter Choice: ";
	cin >> choice2;
	if (choice2 == 1)
	{
		size = "Small";
		Hereounces = 9; //Now Will Save To Local Variable
		Heremoney = 1.75; //Now Will Save To Local Variable
	}
	else if (choice2 == 2)
	{
		size = "Meduim"; //Spelling Error "Medium"
		Hereounces = 12;
		Heremoney = 1.90;
	}
	else if (choice2 == 3)
	{
		size = "Large";
		Hereounces = 15;
		Heremoney = 2.00;
	}
	else
	{
		cout << "Invalid input\n";
	}
}

void Sell_coffee()
{
	//float cups = 0;
	//Make Local Variables To Work With
	int Herecups = 0;
	double Heremoney = 0;
	float Hereounces = 0;
	//double money = 0;
	//Money should be dependant on the size of the drink
	//You can send the value of "Money" from menu2 to this function through an argument
	menu2(Heremoney, Hereounces);
	cout << "How many cups would you like? \n";
	cin >> Herecups;
	money += Heremoney * Herecups; //To save Total Money Made
	ounces += Hereounces * Herecups; //I ASSUME YOU NEED TO KEEP TRACK OF TOTAL OUNCES TOO?
	cups += Herecups; //Keep Track Of Total Cups Sold
	//money = money * cups; 
}

//Renamed Functions
void Show1(int y)
{
	cout << "The total number of cups sold is " << y << endl;
}

void Show2(float x)
{
	cout << "The total number of ounces sold is " << x << endl;
}

void Show3(double a)
{
	cout << fixed << showpoint << setprecision(2);
	cout << "The total amount of money the coffee shop has made is $" << a << endl;
}
Last edited on
closed account (jEb91hU5)
Wow! That’s much more than I expected but greatly appreciated! I’ll look over it and see what all was done. Thanks so much!
I forgot to change the condition for the while loop! Again, the while loop now allows for ANY input other than 5. You want it to only accept input between 1 and 4.

Again, if you have any questions I'm here.
Topic archived. No new replies allowed.