Calling class ASAP

So I am trying to figure out how to call line 41 to function on line 105 after you make your selection it should display Selection: name, price per unit: $price

I need to know how to do this so each time you select it to display this so, for example, hit 1 for water Selection: Water, price per unit: $1.45 then it should ask for quantity.

Also my Order summary is not working properly it displays nothing instead of all the choices and totals.

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
  #include <iostream>
#include <string>
#include <iomanip>
#include <sstream> // stringstream
#include <cctype> // isalph, isdigit

class Customer {
	const std::string custName, custAddress;
public:
	void custInfo(std::ostream &) const
	{
		
	}
	void custWelcome(std::ostream &) const
	{
		std::cout << "\nHello " << custName << "\nAddress: " << custAddress << std::endl;
	}
	std::string custValue() const
	{
		return custName;
	}

	Customer(const std::string &inputCustName, const std::string &inputCustAddress)
		: custName(inputCustName), custAddress(inputCustAddress)
	{
	}
};

struct Beverage {
	const std::string name;
	double price;

	void bevInfo(std::ostream &) const
	{
		std::cout << "Name" << name << "\n"
			<< "Price" << "$" << price << "\n"
			<< "Total" << "$" << std::endl;
	}
	void bevSelect(std::ostream &) const
	{
		std::cout << "Selection: " << name << ", price per unit: $" << price << std::endl;
	}
	Beverage(const std::string &bevName, double bevPrice)
		: name(bevName), price(bevPrice)
	{
	}
};


const int AVAILABLE_PRODUCTS_COUNT = 3;
const Beverage AVAILABLE_PRODUCTS[AVAILABLE_PRODUCTS_COUNT] = {
	Beverage("Water", 1.45), // Element 0
	Beverage("Soda", 2.98), // Element 1
	Beverage("Ice Tea", 3.29) // Element 2
};

typedef int Quantities[AVAILABLE_PRODUCTS_COUNT];
Quantities ordered = { 0 };

double totalCost(Quantities);
void orderSummary(Quantities);
char getCharFromUser(const std::string &prompt);
Customer getCustomerFromUser();
void displayMenu(const Customer &);
void loadProductQuantity(const Beverage &, Quantities);
void doneShowOrder(const Customer &, Quantities);

double totalCost(Quantities beverageCount)//Cost loop
{
	double totalCost = 0;

	for (int i = 0; i < AVAILABLE_PRODUCTS_COUNT; i++) {
		totalCost += (beverageCount[i] * AVAILABLE_PRODUCTS[i].price);
	}
	return totalCost;

}

void loadProductQuantity(int beverageNumber, int quantity)
{
	ordered[beverageNumber] = quantity;
}

void doneShowOrder(const Customer &, Quantities)
{
	orderSummary(ordered);
}

void displayMenu(const Customer &prompt)
{
	std::cout << std::endl << std::endl
		<< prompt.custValue()
		<< ", Please select the beverage you would like to purchase from the menu: "
		<< std::endl;

	std::cout << "Drink Menu" << std::endl;
	std::cout << "========" << std::endl;
	std::cout << "1 - Water $1.45" << std::endl;
	std::cout << "2 - Soda $2.98" << std::endl;
	std::cout << "3 - Iced Tea $3.29" << std::endl;
	std::cout << "X - Exit " << std::endl << std::endl;
}


char getCharFromUser(const std::string &prompt)
{
	char key;

	std::cout << "Selection: ";

	std::cin >> key;

	return key;
}


Customer getCustomerFromUser()
{
	std::string custName;
	std::string custAddress;
	std::cout << "Please enter your name ==> ";
	std::getline(std::cin, custName);
	std::cout << "Please enter your address ==> ";
	std::getline(std::cin, custAddress);

	return Customer(custName, custAddress);
}

int main(void) {
	Customer patron = getCustomerFromUser();
	patron.custWelcome(std::cout);
	patron.custInfo(std::cout);

	bool done = false;
	while (!done) {
		int amountOrdered = 0;
		displayMenu(patron);
		switch (getCharFromUser("Your selection: ")) {
		case '1':
			std::cout << "Please chose a quantiy: ";
			std::cin >> amountOrdered;
			loadProductQuantity(0, amountOrdered);
			break;
		case '2':
			std::cout << "Please chose a quantiy: ";
			std::cin >> amountOrdered;
			loadProductQuantity(1, amountOrdered);
			break;
		case '3':
			std::cout << "Please chose a quantiy: ";
			std::cin >> amountOrdered;
			loadProductQuantity(2, amountOrdered);
			break;
		case 'X': case 'x':
			done = true;
			break;
		default:
			std::cout << "Invalid selection. Please try again";
			std::cin.get();
			// no break in the default case
		}
	}
	doneShowOrder(patron, ordered);
	std::cout << "Press any key to exit." << std::endl;
	char blah;
	std::cin >> blah;
	return 0;
}

void orderSummary(Quantities ordered) {
	std::cout << std::endl;
	std::cout << "======= ORDER SUMMARY ====" << std::endl;
	std::cout << "Items selected" << std::endl;
	for (int i = 0; i < AVAILABLE_PRODUCTS_COUNT; i++) {
		// only show beverages that has at least one order
		if (ordered[i] > 0) {
			std::cout << "You selcted: " << std::endl;
		}
	}
}
Last edited on
So I am trying to figure out how to call line 41 to function on line 105 after you make your selection it should display Selection: name, price per unit: $price


getCharFromUser() is a free function, so you can just call it from within Beverage::bevSelect(), if that's what you're asking.

Or do you want it the other way around - that whenever you call getCharFromUser(), you want it to call Beverage::bevSelect()?
Last edited on
Other way around so basically what I need is everytime you make a selection it will show you the selection, price per unit

The program should run like this:

Please enter your name ==> 'Joseph'
Please enter your address ==> 'Here'

Hello Joseph
Address: Here

Joseph, Please select the beverage you would like to purchase from the menu:

Drink Menu
========
1 - Water $1.45
2 - Soda $2.98
3 - Iced Tea $3.29
X - Exit

Selection: '1'

Selection: Water, price per unit: $1.45 //It is not doing this step
Please chose a quantity:


That is the only thing missing so each time you make a selection it should say and the price per unit.
You want to show something after the user has made a selection, but before you ask for quantity?

Where is that point in your code?
Once you have the user's selection, you'll need to:

1. find some way to identify which of the elements in the AVAILABLE_PRODUCTS array corresponds to the user's selection
2. call the bevSelect() method of that element.
Last edited on
Speaking of bevSelect() ...

I see that these four functions all take parameter of type std::ostream&:
1
2
3
4
void Customer::custInfo( std::ostream& ) const
void Customer::custWelcome( std::ostream& ) const
void Beverage::bevInfo( std::ostream& ) const
void Beverage::bevSelect( std::ostream&) const

There is surely some reason for that?
Topic archived. No new replies allowed.