Function overloading

closed account (jEb91hU5)
Hello,

I would really like a fresh set of eyes on my code and see what I am doing wrong. I feel like I am messing something up with my strings, as well as some other errors I probably have missed. I'd appreciate the help. I tried to put in some notes to explain what I was thinking. I'd also like to do "int cups" but changed it to "float" for ease of use.

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

using namespace std;

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


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

void menu()
{
	int choice, ounces = 0;
	float cups = 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(cups, ounces);
			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()
{
	int choice2, 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";
	}
}


float Sell_coffee(float &x, int &y, double &a)
{
	float cups;
	double money;
	menu2();
	cout << "How many cups would you like? \n";
	cin >> cups;
	money = money * cups;
	return money;
	return cups;
	return ounces;

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

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

void Show(double a)
{
	cout << "The total amount of money the coffee shop has made is " << a << endl;
}
Last edited on
Here's your code with a few correction and comments. I only corrected it enough so that it would compile, follow my comments to make the program work correctly. Good Luck!

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


using namespace std;

void menu();
void menu2();
void Sell_coffee();		//(float cups, int ounces, double money) REMOVED ARGUMENTS SINCE YOU DIDN'T PROVIDE THEM IN FUNCTION CALL
void Show(int y);						//(int ounces)
void Show(float x);						//(float cups)
void Show(double a);					//(double money)
//Don't Name 3 Functions The Same If They Don't Do The Same Thing
int main()
{
	cout << "Welcome to the coffee shop!\n";
	menu();
}

void menu()
{
	int choice, ounces = 0; //Would make more sense for cups to be int and ounces to be a float??
	float cups = 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) //This means if choice is equal to 5 the loop will end. You want "while(choice > 0 && choice < 5)"
	{
		//If choice isn't "1", Show(double a) Is ALWAYS The Function Called. CHANGE THE FUNCTION NAMES TO MATCH WHAT THE FUNCTION DOES!
		switch (choice)
		{
		case 1: Sell_coffee(); //Trying to send 2 argument while Function was EXPECTING 3 arguments! -Removed arguments since unused
			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, ounces; //Not Initialized
	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"; //No initializer List, use an = sign
		ounces = 9; //Info Will Disappear by the end of this function
		money = 1.75; //Info Will Disappear by the end of this function
	}
	else if (choice2 == 2)
	{
		size = "Meduim"; //No initializer List, use an = sign
		ounces = 12; //Info Will Disappear by the end of this function
		money = 1.90; //Info Will Disappear by the end of this function
	}
	else if (choice2 == 3)
	{
		size = "Large"; //No initializer List, use an = sign
		ounces = 15; //Info Will Disappear by the end of this function
		money = 2.00; //Info Will Disappear by the end of this function
	}
	else
	{
		cout << "Invalid input\n";
	}
}

void Sell_coffee() //Why make arguments if you wont use them? Are these Needed? Removed them So Code Would Compile
{
	float cups = 0; //Was Not Initialized
	double money = 0; //Was Not Initialized
	//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;
}

//Name Them Differently As To Not Confuse Yourself and To Call The Correct Function?
void Show(float x)
{
	cout << "The total number of cups sold is " << x << endl;
}

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

void Show(double a)
{
	cout << "The total amount of money the coffee shop has made is " << a << endl;
}
Last edited on
closed account (jEb91hU5)
Thanks so much for your help! I'll work on it.
Tiny mistake. On line 37, I said to use "while(choice > 0 && choice < 5)" but it should be the opposite. "while(choice < 0 || choice > 4)"
Topic archived. No new replies allowed.