Help with Hmework Please :)

Here is my code so far. I am trying to write a function that will print the values of the functions lifeSmCups, med , large from the function cupsSold. I am unclear on how to call these values from the other function. Can anyone offer any guidance?
Thank you!

Tricia

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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
  /**

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 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 funtions:
    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
    a function to show the total amount of money made.
Your program should not use an global variables and special values
such as coffee cup sizes and cost of a coffee cup must be declared as named constants.


*/
#include <iostream>
#include <iomanip>

using namespace std;

//constants

double const smCoffee = 1.75;
double const mdCoffee = 1.90;
double const lgCoffee = 2.00;


//Function prototypes
void initialize (int& numberSmCups, int& numberMedCups, int& numberLgCups,double& totalSmCups,
                 double& totalMedCups, double& totalLgCups);
void showChoices();
void buyCoffee(int& numberSmCups,double& totalSmcups,int& numberMedCups,double& totalMedCups,int& numberLgCups
               ,double& totalLgCups);
void cupsSold(int& numberSmCups,int& numberMedCups,int& numberLgCups);
void coffeeSold();
void totalAmount(double& totalSmCups,double& totalMedCups, double& totalLgCups);
void printCupsSold(int& numberSmCups,int& numberMedCups,int& numberLgCups);
//Main

int main()
{
//declare variables
 int numberSmCups;
 int numberMedCups;
 int numberLgCups;
 double totalSmCups;
 double totalMedCups;
 double totalLgCups;
 int choice;

//initialize
initialize(numberSmCups,numberMedCups,numberLgCups,totalSmCups,
   totalMedCups,totalLgCups);

//showChoice switch
    do
    {
        showChoices();
        cin >> choice;
        cout << endl;

        switch (choice)
        {
        case 1:
            cout <<"You selected to buy coffee."<< endl;
            initialize(numberSmCups,numberMedCups,numberLgCups,totalSmCups,
   totalMedCups,totalLgCups);
            buyCoffee(numberSmCups,totalSmCups,numberMedCups,totalMedCups,numberLgCups,totalLgCups);

            break;
        case 2:
            cout << "The total amount of cups sold by size is:  "<<endl;
            cupsSold(numberSmCups,numberMedCups,numberLgCups);

            break;
        case 3:
            cout << "function for lifetime coffee sold";

            break;
        case 4:
            cout << "The total money made for Coffee Sales is:  ";
            totalAmount(totalSmCups,totalMedCups,totalLgCups);

            break;

        case 99:
            break;

        default:
            cout << "Invalid input." << endl;
        }
    }
    while (choice != 99);


cout<< " Thank you for using this program.  Have a nice day."<<endl;
    return 0;
}   //end Main

void showChoices()
{
    cout << "Enter--" << endl;
    cout<< endl;
    cout << "1: Buy Coffee " << endl;
    cout << "2: Shows the total cups sold by size "<< endl;
    cout << "3: Shows the total amount of coffee sold"<<endl;
    cout << "4: Shows the total amount of money made"<< endl;
    cout << "99: To quit the program." << endl;
}// end showChoices


void initialize (int& numberSmCups, int& numberMedCups, int&numberLgCups,double& totalSmCups,
                 double& totalMedCups, double& totalLgCups/*,int& lifeSmCups,int lifeMdCups,int lifeLgCups*/){
 numberSmCups=0;
 numberMedCups=0;
 numberLgCups=0;
 totalSmCups=0;
 totalMedCups=0;
 totalLgCups=0;
 } //end initialize



 void buyCoffee(int& numberSmCups,double& totalSmCups,int& numberMedCups,double& totalMedCups,
                int& numberLgCups,double& totalLgCups){

    char coffeeSize;
    char order;

               cout <<"Would you like some coffee? (y for yes, n for no)"<<endl;
               cin>>order;

    while(true){
        if (order == 'y' ||order =='Y'){


        cout<<"What size coffee would you like? (s,m,l)";
        cin>> coffeeSize;


            if (coffeeSize == 's'){
            cout<<"How many?";
            cin>>numberSmCups;
            totalSmCups = numberSmCups * smCoffee;
            cout<<"Small Coffee: "<<numberSmCups<<" "<<totalSmCups<<endl;
            cout<<"add another?"<<endl;
            cin>> order;
            }
        else
          if (coffeeSize == 'm'){
                cout<<"How many?";
                cin>>numberMedCups;
                totalMedCups = numberMedCups * mdCoffee;
                cout<<"Medium Coffee: "<<numberMedCups<<" "<<totalMedCups<<endl;
                cout<<"add another?"<<endl;
                cin>> order;
                }
        else
         if (coffeeSize == 'l'){
                cout<<"How many?";
                cin>>numberLgCups;
                totalLgCups = numberLgCups * lgCoffee;
                cout<<"Large Coffee: "<<numberLgCups<<" "<<totalLgCups<<endl;
                cout<<"add another?"<<endl;
                cin>> order;}
                }
        else
              break;

}//end While

    cout<<"you ordered: "<<endl;
    cout<< endl;
    if (numberSmCups>= 1) cout<<"Small Coffee: "<<numberSmCups<<" "<<"@"<<" "<<smCoffee<<" "<<"$"<<totalSmCups<< endl;
    if (numberMedCups>=1)cout<<"Medium Coffee: "<<numberMedCups<<"@"<<" "<<mdCoffee<<" "<<" $"<<totalMedCups<<endl;
    if (numberLgCups>=1) cout<<"Large Coffee: "<<numberLgCups<<"@"<<" "<<lgCoffee<<" "<<" $"<<totalLgCups<<endl;
    cout<< endl;
    cout<<"Amount Due: "<<" "<<"$"<< (totalSmCups+ totalMedCups+ totalLgCups)<<endl;



 }//end BuyCoffee


 void cupsSold(int& numberSmCups,int& numberMedCups,int& numberLgCups){
     static int lifeSmCups=0;
     static int lifeMdCups=0;
     static int lifeLgCups=0;

     lifeSmCups = lifeSmCups + numberSmCups;
     lifeMdCups = lifeMdCups + numberMedCups;
     lifeLgCups = lifeLgCups + numberLgCups;

     //cout<<"Total Small cups: "<<lifeSmCups<<endl;
     //cout<<"Total Medium Cups:"<<lifeMdCups<<endl;
    // cout<<"Total Large Cups:"<<lifeLgCups<<endl;

}//end cupsSold

void printCupsSold (int& numberSmCups,int& numberMedCups,int& numberLgCups){

}
void totalAmount(double& totalSmCups,double& totalMedCups, double& totalLgCups){
    static double lifeSmAmount=0;
    static double lifeMdAmount = 0;
    static double lifeLgAmount = 0;
    static double moneyMade = 0;

    lifeSmAmount = lifeSmAmount + totalSmCups;
    lifeMdAmount = lifeMdAmount + totalMedCups;
    lifeLgAmount = lifeLgAmount + totalLgCups;
    moneyMade= lifeSmAmount + lifeMdAmount+ lifeLgAmount;

    cout<<"Total: "<<"$"<<moneyMade<<endl;
}//end totalAmount

I actually just answered someones question exactly like this the other day.
Here's some code to get you started. There's no error-checking. You'll have to implement that on your own. =D

1
2
3
4
5
6
7
8
9
10
11
12
Main.cpp

#include "CoffeeShop.h"

int main()
{
	CoffeeShop Shop;

	Shop.Menu();

	return 0;
}


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
CoffeeShop.h

#pragma once

#include <iostream>
#include <iomanip>
#include <utility>
#include <sstream>
#include <string>

class CoffeeShop
{
public:
	void Menu();
	void BuyCoffee(int type, int amount);
	void ShowCoffeeSold();
	void ShowProfit();
	std::pair<int, int> ParseString(const std::string &TypeAmount);

private:
	double profit = 0;

	const double smallPrice = 1.75;
	const double mediumPrice = 1.90;
	const double largePrice = 5.00;

	int smallsSold = 0;
	int mediumsSold = 0;
	int largesSold = 0;
};


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
CoffeeShop.cpp

#include "CoffeeShop.h"

void CoffeeShop::Menu()
{
	std::string coffeeInput;

	while(coffeeInput != "exit")
	{
		std::cout << "Jason's Coffee Shop\n\n";
		std::cout << "Menu\n";
		std::cout << "#\tItem\t\tPrice\n";
		std::cout << "1\tSmall Coffee\t$1.75\n";
		std::cout << "2\tMedium Coffee\t$1.90\n";
		std::cout << "3\tLarge Coffee\t$5.00\n\n";

		std::cout << "Enter coffee # that you wish to purchase followed by a ',' and the amount or\n enter '4' to view total cups of each size sold\t'5' to view profit: ";

		std::getline(std::cin, coffeeInput);

		std::pair<int, int> parsedString = ParseString(coffeeInput);

		system("cls");

		switch(parsedString.first)
		{
			case 1:
			case 2:
			case 3:
				system("cls");
				BuyCoffee(parsedString.first, parsedString.second);
				break;
			case 4:
				system("cls");
				ShowCoffeeSold();
				break;
			case 5:
				system("cls");
				ShowProfit();
				break;
		}
	}
}

void CoffeeShop::BuyCoffee(int type, int amount)
{
	switch(type)
	{
		case 1:
			profit += smallPrice * amount;
			smallsSold += amount;
			std::cout << amount << " small coffie(s) bought for $" << smallPrice * amount;
			break;
		case 2:
			profit += mediumPrice * amount;
			mediumsSold += amount;
			std::cout << amount << " medium coffie(s) bought for $" << mediumPrice * amount;
			break;
		case 3:
			profit += largePrice * amount;
			largesSold += amount;
			std::cout << amount << " large coffie(s) bought for $" << largePrice * amount;
			break;
	}

	std::cout << "\n\nPress enter to continue.";
	std::cin.ignore();
	system("cls");
}

void CoffeeShop::ShowCoffeeSold()
{
	std::cout << "Coffee Sold\n\n";
	std::cout << "Type\t\tAmount\n";
	std::cout << "Small\t\t" << smallsSold << "\n";
	std::cout << "Medium\t\t" << mediumsSold << "\n";
	std::cout << "Large\t\t" << largesSold << "\n\n";
	std::cout << "Total\t\t" << smallsSold + mediumsSold + largesSold << "\n\n";

	std::cout << "\n\nPress enter to continue.";
	std::cin.ignore();
	system("cls");
}

void CoffeeShop::ShowProfit()
{
	std::cout << "Profit\n\n";
	std::cout << "Profit some small coffies: " << smallsSold * smallPrice << "\n";
	std::cout << "Profit some medium coffies: " << mediumsSold * mediumPrice << "\n";
	std::cout << "Profit some large coffies: " << largesSold * largePrice << "\n";
	std::cout << "Total profit: " << (smallsSold * smallPrice) + (mediumsSold * mediumPrice) + (largesSold * largePrice);

	std::cout << "\n\nPress enter to continue.";
	std::cin.ignore();
	system("cls");

}

std::pair<int, int> CoffeeShop::ParseString(const std::string &TypeAmount)
{
	std::istringstream iss(TypeAmount);

	std::string tempType;
	std::getline(iss, tempType, ',');

	std::string tempAmount;
	std::getline(iss, tempAmount);

	int type;
	std::istringstream(tempType) >> type;

	int amount;
	std::istringstream(tempAmount) >> amount;

	return std::pair<int, int>(std::move(type), std::move(amount));
}

EDIT:
I'm assuming that this is for a class? If so I'd mess with the code and change it up a bit so your Professor doesn't think you just copy/pasted it.
Last edited on
Wow..that is some code. Greatly appreciated! We are not up to classes yet though. I am looking more to actually understand as get the assignment right. I have it mostly working, here's where I am stuck: I created a function cupsSold but the way I have it written makes it increment even if there is no new purchase made when the function is called. So I thought I could write a separate function printCupsSold that would just show the values of the variables I created in the cupsSold function OR write cupsSold in a way that it will only increment if there is a new purchase. I am unclear how to do either. If I declare the variables lifeSmCups, etc in CupsSold can I call those values in printCupsSold?

thanks :)

I think what I am actually trying to ask is how to I pass the variables from cupsSold to printCupsSold
Last edited on
You would just call the function inside of it. Basically instead of
1
2
3
     //cout<<"Total Small cups: "<<lifeSmCups<<endl;
     //cout<<"Total Medium Cups:"<<lifeMdCups<<endl;
    // cout<<"Total Large Cups:"<<lifeLgCups<<endl; 
you would want printCupsSold(lifeSmCups, lifeMdCups, lifeLgCups);
Last edited on
That is how I had it but then it increments whether there is a new coffee purchase or not( i know this is because the value is still stored in (numSmCups for example). I'm just not sure how to deal with it.
This is where I am stuck. How do I pass the values to printCupsSold so that when case two(above code) is selected it gives these totals?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void cupsSold(int& numberSmCups,int& numberMedCups,int& numberLgCups){
     static int lifeSmCups=0;
     static int lifeMdCups=0;
     static int lifeLgCups=0;

     lifeSmCups = lifeSmCups + numberSmCups;
     lifeMdCups = lifeMdCups + numberMedCups;
     lifeLgCups = lifeLgCups + numberLgCups;

     //cout<<"Total Small cups: "<<lifeSmCups<<endl;
     //cout<<"Total Medium Cups:"<<lifeMdCups<<endl;
    // cout<<"Total Large Cups:"<<lifeLgCups<<endl;

}//end cupsSold

void printCupsSold (int lifeSmCups,int lifeMdCups,int lifeLgCups){
    cout<<"Total Small cups: "<<lifeSmCups<<endl;
     cout<<"Total Medium Cups:"<<lifeMdCups<<endl;
     cout<<"Total Large Cups:"<<lifeLgCups<<endl;
}


So, the answer on how to pass the value to the print function was I did not need to. I just needed to declare the variable in main and make it a reference variable in cupsSold so it could be used elsewhere. Thank you to all who tried to help me with this. I hope it helps someone else in the future.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//begin cupsSold

 void cupsSold(int& numberSmCups,int& numberMedCups,int& numberLgCups,int& lifeSmCups, int& lifeMdCups, int& lifeLgCups){

     lifeSmCups = lifeSmCups + numberSmCups;
     lifeMdCups = lifeMdCups + numberMedCups;
     lifeLgCups = lifeLgCups + numberLgCups;

    }//end cupsSold              //function to calculate the total number of cups sold by size


//begin printCupsSold
void printCupsSold (int lifeSmCups,int lifeMdCups,int lifeLgCups){
    cout<<"Total Small cups: "<<" "<<lifeSmCups<<endl;
     cout<<"Total Medium Cups: "<<" "<<lifeMdCups<<endl;
     cout<<"Total Large Cups: "<<" "<<lifeLgCups<<endl;
     cout<<endl;
     cout<<"Total Cups: "<<(lifeSmCups + lifeMdCups + lifeLgCups)<<endl;
     cout<<endl;
}//end printCupsSold        //function to print cupsSold to screen 
Topic archived. No new replies allowed.