user defined functions

I have attempted to write a code to solve a homework problem.
Problem statement:
Your program should allow the user to do the following:
Choose among coffee, cakes, or beans to purchase.
Buy coffee in any size and in any number of cups.
Buy cheese cakes in any number of slices.
Buy coffee beans in any amount.
At any time show the total number of cups of each size sold, total number of slices of cakes sold, and total amount of coffee beans sold.
At any time show the total amount of coffee sold.
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 sell cakes,
• a function to sell coffee beans,
a function to show the number of cups of each size of coffee, the number of slices of cheese cakes, and the amount of coffee beans 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. Special values such as coffee cup sizes and cost of a coffee cup must be declared as named constants.
I figured I was doing my functions wrong, so I wrote it in a switch statement so I could get it too work, but re-reading the conditions of the assignment, everything has to be a function pretty much. I looked at http://www.cplusplus.com/forum/general/215671/ to see what they did, and I tried to emulate with my code, but the variables would not update in int main()[I know, because i had cout statements to track the output. Output was fine in the user function, but then would read 0 in the int main()]. So, without further ado,:
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* M5A1 Menu Based Programming
Author: Jeffrey Hackett
Date: 3/4/18
 IT 240
*/
#include <iostream>
#include <iomanip>
#include <limits>

using namespace std;
void menuOptions ();
int coffeeBeans (int ouncesOfCoffee, char correctOunces);
void pointOfSale(int & totalSmallCups, int & totalLargeCups, int & totalSlices, int & totalOuncesOfCoffee, double coffeeSales, double cheeseCakeSales, double coffeeBeanSales);
void exit();

const double SMALL_CUP_PRICE = 1.50;
const double LARGE_CUP_PRICE = 1.90;
const double CHEESECAKE_SLICE_PRICE = 3.00;
const double COFFEE_BEAN_PRICE = 0.60;
const double SALES_TAX_RATE = 0.05;

int SMALL_CUP = 9;
int LARGE_CUP =12;



int main()
{
	int choice;
	int cupSize=0;
	int small =0;
	int large =0;
	int slices =0;
	int ouncesOfCoffee =0;
	int totalSmallCups=0;
	int totalLargeCups=0;
	int totalSlices=0;
	int totalOuncesOfCoffee =0;	
	double coffeeSales, cheeseCakeSales, coffeeBeanSales;
	char correctSize, correctSlices, correctOunces;
	
	do
	{
	
		menuOptions();
		cin >> choice;
	
		switch (choice)
		{
		
		case 1:
			do
			{
				cout << "Select a cup size small (9oz.) or large (12oz.). For a smal press '9' and for a large press '12': \n";
				cin >> cupSize;
				if (!cin)
					{
					cout << "Invalid input \n";
					cin.clear();
					std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
					cout << "Select a cup size small (9oz.) or large (12oz.). For a smal press '9' and for a large press '12': \n";
					cin >> cupSize;
					}
			}
			while (!cin || (cupSize != 9 && cupSize != 12));
			
			if (cupSize == SMALL_CUP)
			{
				cout << "How many small coffees would you like?" << endl;
				cin >> small;
				totalSmallCups += small;
			}
			else
			{
				cout << "How many large coffees would you like?" << endl;
				cin >> large;
				totalLargeCups += large;
			}	
		break;
		case 2:
				do
				{
					cout << "How many slices of cheese cake would you like?: \n";
					cin >> slices;
						if (!cin && slices < 0)
						{
							cout << "Invalid input \n";
							cin.clear();
							std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
							cout << "How many slices of cheese cake would you like?: \n";
							cin >> slices;
						}
				}
				while (!cin && slices < 0);
				totalSlices += slices;
			break;
		
		case 3:
			do
				{
					cout << "How many ounces of coffee would you like?: \n";
					cin >> ouncesOfCoffee;
						if (!cin && ouncesOfCoffee <0)
							{
								cout << "Invalid input \n";
								cin.clear();
								std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
								cout << "How many ounces of coffee would you like?: \n";
								cin >> ouncesOfCoffee;
							}
				}
			while (!cin && ouncesOfCoffee < 0);
			totalOuncesOfCoffee += ouncesOfCoffee;
			break;
				
		case 4:
			pointOfSale(totalSmallCups, totalLargeCups, totalSlices, totalOuncesOfCoffee, coffeeSales, cheeseCakeSales, coffeeBeanSales);
			
			break;
		
		case 5:
			pointOfSale (totalSmallCups, totalLargeCups, totalSlices, totalOuncesOfCoffee, coffeeSales, cheeseCakeSales, coffeeBeanSales);
			cout << "Thank you for your purchase, have a nice day! \n" << endl;
			small = 0;
			large = 0;
			slices = 0;
			ouncesOfCoffee = 0;
			totalSmallCups = 0;
			totalLargeCups = 0;
			totalSlices = 0;
			totalOuncesOfCoffee = 0;			
		
		case 6:
			;
		}
	}
	while (choice != 6);
		return 0;
			
}
 void menuOptions ()
 {
 	cout << "Make a selection from the menu below:" << endl;
	cout << "******************************" << endl;
 	cout << "MAIN MENU \n";
 	cout <<	"1. Coffee sale\n";
 	cout <<	"2. Cheese cake sale \n";
 	cout <<	"3. Coffee bean sales \n";
 	cout <<	"4. Sales totals and profits \n";
 	cout <<	"5. Cash out and clear \n";
 	cout << "6. Quit Program \n" ;
 	cout << "******************************" << endl;
 	cout << "Please enter your selection now: \n";
}

int coffeeBeans (int ouncesOfCoffee, char correctOunces)
{
	do
	{
		cout << "How many ounces of coffee would you like?: \n";
		cin >> ouncesOfCoffee;
		cout << "Are you sure you want " << ouncesOfCoffee << " ounces of coffee? Press 'y' to confirm. \n";
		cin >> correctOunces;
	}
	while (correctOunces != 'y' && correctOunces != 'Y');
	return ouncesOfCoffee;
}

void pointOfSale (int & totalSmallCups, int & totalLargeCups, int & totalSlices, int & totalOuncesOfCoffee, double coffeeSales, double cheeseCakeSales, double coffeeBeanSales)
{
	cout << setprecision (2);
	coffeeSales = (totalSmallCups * SMALL_CUP_PRICE) + (totalLargeCups * LARGE_CUP_PRICE);
	cheeseCakeSales = (totalSlices * CHEESECAKE_SLICE_PRICE);
	coffeeBeanSales = (totalOuncesOfCoffee * COFFEE_BEAN_PRICE);
	cout << "Number of items sold =" << (totalSmallCups+totalLargeCups+totalSlices+totalOuncesOfCoffee) << endl;
	cout << "Small cups of coffee =" << totalSmallCups << endl;
	cout << "Large cups of coffee =" << totalLargeCups << endl;
	cout << "Slices of cheese cake sold =" << totalSlices << endl;
	cout << "Ounces of coffee beans sold =" << totalOuncesOfCoffee << endl;
	cout << endl;
	cout << fixed << showpoint << "Total coffee sales total: $" << coffeeSales << "\n" << endl;
	cout << "small cups of coffee sold :" << totalSmallCups << "." << " For a total of: $" << (totalSmallCups * SMALL_CUP_PRICE) <<"\n" << endl; 
	cout << "Total large cups of coffee sold :" << totalLargeCups << "." << " For a total of: $" << (totalLargeCups * LARGE_CUP_PRICE) <<"\n" << endl;
	cout << fixed << showpoint << "Total slices of cheese cake sold: " << totalSlices << " Cheese cake sales total: $" << cheeseCakeSales << "\n" << endl;
	cout << fixed << showpoint << "Total ounces of coffee sold: " << totalOuncesOfCoffee << " Total coffee bag sales total: $" << coffeeBeanSales << "\n" << endl;
	double subTotal = coffeeSales + cheeseCakeSales + coffeeBeanSales;
	cout << "Your subtotal is: $" << subTotal << "\n" << endl;
	double grandTotal = subTotal + (subTotal* SALES_TAX_RATE);
	cout << "Taxes: $" << (subTotal * SALES_TAX_RATE) << "\n" << endl;
	cout << "Grand Total: $" << grandTotal << "\n" << endl;
	system ("pause");
}

Last edited on
the int coffeeBeans (...) is a legacy that I forgot to remove. Also the functions I tried, was a int coffeeSales (int cupSize), but when I called and inititalized:
1
2
3
4
5
6
7
int coffeeSales (int cupsize)
{
cout << "For a small coffee press 9 and for a large coffee press 12:" << endl;
cin >> cupSize;
cout << cupSize
return cupSize;
}


the value would not carry over to int main under switch / case 1 where i then:
1
2
3
4
5
6
7
8
case 1:
cout << cupSize;
if (cupSize == 9);
++small;
else
++large;
cout << small;
cout << large;
cupSize would read 9 lets say, and then output 0 when the function was over... so not sure what happened. I also kept it super basic so I could troubleshoot and see what was happening, but i could never figure it out.
Last edited on
Topic archived. No new replies allowed.