Help with Array's

Hi, I need to implement the following into my code and need some help reviewing:

Use 2 arrays: one for the products, and one for the quantities.
Use loops to assign values to the arrays.
Use loops to read and print values from the arrays.
Reuse the functions that you developed in Week 2 and the customer class that you developed in Week 3. Changes can be done as needed.
Complete the following in your code:
*Provide a list of available products
*Ask the customer to select products and quantities
*Save the provided details in arrays
*Read from the arrays to print the order summary; that is, the products, *quantities, and the total price for each product
*Calculate and print the total price for the order


I already have an array for product line 188 of my code I need to figure out how to do one for the quantity and verify my array for the product look correct. Therefore, I need to store not only the product in an array but also the quantity. Then read it to the order summary. Please assist and help provide me with an understanding for this.


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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream> // stringstream
#include <cctype> // isalph, isdigit
using namespace std;


class Customer
{
	string custName; // Customer name
	string custAddress; //Customer Address

public:
	void info()
	{
		cout << setw(10) << left << "Name" << custName << "\n"
			<< setw(10) << left << "Address" << custAddress << "\n" << endl;
	}
	void simpleInfo()
	{
		cout << "\nHello " << custName << "\nAddress: " << custAddress << endl;
	}
	std::string value() const {
		return custName;
	}

	Customer() {}
	~Customer() {}
	Customer(string n, string a) {
		custName = n;
		custAddress = a;
	}
}; // END OF CLASS

struct Beverage
{
	string name;	// The name
	int quantity{}; // The quantity
	double price{}; //The price

	void info()
	{
		cout << setw(10) << left << "Name" << name << "\n"
			<< setw(10) << left << "Price" << "$" << price << "\n"
			<< setw(10) << left << "Quantity" << quantity << "\n"
			<< setw(10) << left << "Total" << "$"
			<< quantity * price << "\n" << endl;
	}
	void simpleInfo()
	{
		cout << "Selection: " << name << ", price per unit: $" << price << endl;
	}

	Beverage() {}
	~Beverage() {}
	Beverage(string n, double p) {
		name = n;
		price = p;
	}
}; // END OF STRUCT

double totalCost(Beverage beverages[], int numOfBeverages);
void orderSummary(Beverage beverages[], int numOfBeverages);
bool isAllDigits(string input);
int getValidNumber(string text, int min, int max);
void doShopping();

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

	return Customer(custName, custAddress);
}


void displayMenu(string userName) //Menu function
{

	cout << endl << endl
		<< userName
		<< ", Please select the beverage you would like to purchase from the menu: "
		<< endl;

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

int main(void)
{
	doShopping();

	return 0;
}


int getQuantity() //Quantity function
{
	int quantity = 0;
	cout << "Enter quantity : ";
	cin >> quantity;
	return quantity;
}


bool isAllDigits(string input)
{
	for (unsigned i = 0; i < input.length(); i++) {
		if (!isdigit(input[i])) {
			return false;
		}
	}
	return true;
}

int getValidNumber(string text, int min, int max)
{
	string input{};
	int num{};
	while (true)
	{
		cout << text << " [" << min << " - " << max << "]: ";
		getline(cin, input);

		stringstream ss(input);

		if (!isAllDigits(ss.str())) {
			cout << "\nInvalid input. Try again!" << endl;
		}
		else {
			if (isAllDigits(ss.str()) && ss >> num) {
				if (num >= min && num <= max) {
					break;
				}
				else {
					std::cout << "\nOut of range. Try again!" << endl;
				}
			}
		}
	}
	return num;
}


double totalCost(Beverage beverages[], int numOfBeverages)  //Total cost function
{
	double totalCost = 0;

	for (int i = 0; i < numOfBeverages; i++) {
		totalCost += (beverages[i].price*beverages[i].quantity);
	}
	return totalCost;

}


void orderSummary(Beverage beverages[], int numOfBeverages) //Summary function
{
	cout << "\n======= ORDER SUMMARY ====" << endl;
	cout << "Items selected" << endl << endl;
	for (int i = 0; i < numOfBeverages; i++) {
		// only show beverages that has at least one order
		if (beverages[i].quantity > 0) {
			beverages[i].info();
		}
	}
}


void doShopping() //Shopping function
{
	// declare our beverages 
	Beverage
		water("Water", 1.45),
		soda("Soda", 2.98),
		iceTea("Ice Tea", 3.29);

	// Beverage array to hold the beverages
	Beverage beverages[] = { water, soda, iceTea };

	char selection = ' ';

	Customer patron = snafu(); // get info, create patron
	patron.simpleInfo(); // greet patron 

						 /*string name = "";
						 string address = "";

						 //Ask user for her/his name
						 cout << "Please enter your name ==> ";
						 getline(cin, name);

						 cout << "Please enter your address ==> ";
						 getline(cin, address);

						 //display user name
						 cout << "Hello " << name << endl;
						 cout << "Address: " << address << endl;
						 */

	do
	{
		// display menu
		displayMenu(patron.value());

		// read user selection
		cout << "Your selection: ";
		cin >> selection;
		cin.get(); // takes last ENTER character in the stream

		switch (selection)
		{
		case '1':
			beverages[0].simpleInfo();
			// assuming the customer can shop at least 1 and max 100
			beverages[0].quantity += getValidNumber("Enter quantity", 1, 100);
			break;
		case '2':
			beverages[1].simpleInfo();
			beverages[1].quantity += getValidNumber("Enter quantity", 1, 100);
			break;
		case '3':
			beverages[2].simpleInfo();
			beverages[2].quantity += getValidNumber("Enter quantity", 1, 100);
			break;
		case 'X':
		case 'x':
			orderSummary(beverages, 3);
			if (totalCost(beverages, 3) > 0) {
				cout << "\nGrand total = $" << totalCost(beverages, 3) << endl;
			}
			cout << "\nThank you for your purchase " << patron.value() << ", Come back soon!!!" << endl;
			break;
			// other than 1, 2, 3 and X...
		default: cout << "Invalid selection. Please try again";
			cin.get();
			// no break in the default case
		}
		cout << endl << endl;
	} while (selection != 'X' && selection != 'x'); // 'X' or 'x' displays the summary
	system("pause"); //Pauses the system until a key is hit, The the program will end.
}
you can have tightly coupled arrays where the index is the key and each array at a given index is a piece of data that is correlated. This is kind of poor for most designs; its something you would see in very low level code in lower level languages.

looks like
Beverage beverages[] = { water, soda, iceTea };
double values[] = {1.45,2.98,3.29}; //it works, but the order of entries must match which is bug prone, there must be 1 entry for each entry in both arrays, also bug prone, and tying them together requires a key, which is annoying).

so you can see that beverages[0] is water which costs values[0] $1.45
you can further tie that together with an enum so that water = 0 as a named constant for easy reading.

Is this what you are asking?

you already know about structs; a better design would be a struct that has the name, values, and other bits all together, and have a container of those (you can make an array or vector of structs). Then you could see that array[0] is water price of 1.45 with other attributes and whatnot.



Last edited on
Hi joinnin,

Thank you for the response. I am having a hard time following. I apologize for my ignorance but I am new to this so I am having a hard time understanding this.

Thanks,
Joseph
you can have 2 distinct arrays, and you can logically "connect" them by the index. So array1[0] and array2[0] could have some sort of connected meaning defined by you, the programmer because when you wrote the code you decided that these arrays are tied together. Its an awkward way to do things, but there are a few specific places where it can make sense.

If that is what you are asking to do, my first post shows it with your data.

better is this:
struct bevs
{
double value;
string type;
};
bevs mybevs[3];
mybevs[0].value = 1.45;
mybevs[0].type = "Water";

or whatever. It gets even better with more advanced tools, but that is the start of a better way because the data is tied together, you only need 1 array and can't mess it up by accessing array[0] and array2[1] accidentally tying water and soda together or something.



I am beginning to understand so for mine it would be like:

1
2
3
4
5
6
7
Beverage beverages[3];
beverages[0].price = 1.45;
beverages[0].name = "Water";
beverages[1].price = 2.98;
beverages[1].name = "Soda";
beverages[2].price = 3.29;
beverages[2].name = "Ice Tea";


Am I assuming it is just calling the struct? For example:
beverages[0].price
is just calling from the struct. Am I following or is this incorrect? So Along with this, I need an Array of my quantity and I am unsure how to do this as well? For these Arrays would they just be replacing the location of my current array? I need help understanding thank you.
the struct is a user defined type, just like int.
you made an array of it, just like an array of int.
then you access the array with [] and once there you can get to the parts of your user defined type.

so yes, it is just "calling" the struct, of sorts. Or maybe "using" it is a better word.

as it is written quantity can just be another field in the struct. That is probably a design mistake, though. It does not belong there.
if you plan on having multiple customers at once or a growing list of them over time etc, you need something else.

if you do it with an array, the index represents the customer number and the values in the array represent how much they bought of each type. Quantity would be either another struct or a typedef for an array[3] or the like.
Last edited on
Can you help break this down or explain to me the best way of doing this. I can really use the help I don't want to have a bad program that will encounter errors or bugs. I need to know best practices of getting this done with what I have and having an array for the quantity as well I want to keep this clean and efficient.
I am in the middle of relearning the language; c++ 11 and higher are all still new to me so my code examples tend to be a little unpolished still. Ill post something tonight.

what you had is very good, really.
I changed the array to be pointers; a vector would be better but I didn't know if you were allowed those yet. With that small change, its all tied together. Because you destroy and re-create everything in doshopping, what I said earlier isn't useful; you can wrap do-shopping if you want to support multiple shoppers fairly cleanly (I didn't do this). So putting the quantity where you had it works given the volatile nature of the objects which I had missed before.

With these changes it works, but did that answer your question? I apologize for any confusion, still not 100% sure what you want to see.
what you had was ok, the pointers get rid of some excess copying.

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


double totalCost(Beverage *beverages[], int numOfBeverages)  //Total cost function
{
	double totalCost = 0;

	for (int i = 0; i < numOfBeverages; i++) {
		totalCost += (beverages[i]->price*beverages[i]->quantity);
	}
	return totalCost;

}


void orderSummary(Beverage *beverages[], int numOfBeverages) //Summary function
{
	cout << "\n======= ORDER SUMMARY ====" << endl;
	cout << "Items selected" << endl << endl;
	for (int i = 0; i < numOfBeverages; i++) {
		// only show beverages that has at least one order
		if (beverages[i]->quantity > 0) {
			beverages[i]->info();
		}
	}
}


void doShopping() //Shopping function
{
	// declare our beverages 		
		Beverage water("Water", 1.45);
		Beverage soda("Soda", 2.98);
	    Beverage iceTea("Ice Tea", 3.29);
	
	// Beverage array to hold the beverages	
	    Beverage *beverages[]  = {&water,&soda,&iceTea};

	
	

	char selection = ' ';

	Customer patron = snafu(); // get info, create patron
	patron.simpleInfo(); // greet patron 

						 /*string name = "";
						 string address = "";

						 //Ask user for her/his name
						 cout << "Please enter your name ==> ";
						 getline(cin, name);

						 cout << "Please enter your address ==> ";
						 getline(cin, address);

						 //display user name
						 cout << "Hello " << name << endl;
						 cout << "Address: " << address << endl;
						 */

	do
	{
		// display menu
		displayMenu(patron.value());

		// read user selection
		cout << "Your selection: ";
		cin >> selection;
		cin.get(); // takes last ENTER character in the stream

		switch (selection)
		{
		case '1':
			beverages[0]->simpleInfo();
			// assuming the customer can shop at least 1 and max 100
			beverages[0]->quantity += getValidNumber("Enter quantity", 1, 100);
			break;
		case '2':
			beverages[1]->simpleInfo();
			beverages[1]->quantity += getValidNumber("Enter quantity", 1, 100);
			break;
		case '3':
			beverages[2]->simpleInfo();
			beverages[2]->quantity += getValidNumber("Enter quantity", 1, 100);
			break;
			
		case 'X':
		case 'x':
			orderSummary(beverages, 3);
			if (totalCost(beverages, 3) > 0) {
				cout << "\nGrand total = $" << totalCost(beverages, 3) << endl;
			}
			cout << "\nThank you for your purchase " << patron.value() << ", Come back soon!!!" << endl;
			break;
			// other than 1, 2, 3 and X... */
		default: cout << "Invalid selection. Please try again";
			cin.get();
			// no break in the default case
		}
		cout << endl << endl;
	} while (selection != 'X' && selection != 'x'); // 'X' or 'x' displays the summary
	system("pause"); //Pauses the system until a key is hit, The the program will end.	
}

Last edited on
This is very good. Ultimately what I need is to add an array for the quantity that is what my assignment requires I just do not know how and need help.

This is what my assignment requires:

Use 2 arrays: one for the products, and one for the quantities.
Use loops to assign values to the arrays.
Use loops to read and print values from the arrays.
Reuse the functions that you developed in Week 2 and the customer class that you developed in Week 3. Changes can be done as needed.
Complete the following in your code:
*Provide a list of available products
*Ask the customer to select products and quantities
*Save the provided details in arrays
*Read from the arrays to print the order summary; that is, the products, *quantities, and the total price for each product
*Calculate and print the total price for the order

I already have most of it I believe I just need the 2nd array for quantity which I could use some help with. Otherwise, please advice.
Please explain the purpose of the pointer in the following snippet:
1
2
3
4
5
6
7
	// declare our beverages 		
		Beverage water("Water", 1.45);
		Beverage soda("Soda", 2.98);
	    Beverage iceTea("Ice Tea", 3.29);
	
	// Beverage array to hold the beverages	
	    Beverage *beverages[]  = {&water,&soda,&iceTea};


This is really not doing what you probably think it is. It should probably be something more like:

1
2
3
4
5
6
7
    // declare our beverages
    Beverage water("Water", 1.45);
    Beverage soda("Soda", 2.98);
    Beverage iceTea("Ice Tea", 3.29);

	// Beverage array to hold the beverages
    Beverage beverages[]  = {water, soda, iceTea};


Or even better:
1
2
3
  
    Beverage beverages[] {{"Water", 1.45}, {"Soda", 2.98}, {"Ice Tea", 3.29}};


Next please explain the purpose of the pointer in the following:
1
2
void orderSummary(Beverage *beverages[], int numOfBeverages) //Summary function
{


That was not my code jib.

Here is my code below:
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream> // stringstream
#include <cctype> // isalph, isdigit
using namespace std;


class Customer
{
	string custName; // Customer name
	string custAddress; //Customer Address

public:
	void info()
	{
		cout << setw(10) << left << "Name" << custName << "\n"
			<< setw(10) << left << "Address" << custAddress << "\n" << endl;
	}
	void simpleInfo()
	{
		cout << "\nHello " << custName << "\nAddress: " << custAddress << endl;
	}
	std::string value() const {
		return custName;
	}

	Customer() {}
	~Customer() {}
	Customer(string n, string a) {
		custName = n;
		custAddress = a;
	}
}; // END OF CLASS

struct Beverage
{
	string name;	// The name
	int quantity{}; // The quantity
	double price{}; //The price

	void info()
	{
		cout << setw(10) << left << "Name" << name << "\n"
			<< setw(10) << left << "Price" << "$" << price << "\n"
			<< setw(10) << left << "Quantity" << quantity << "\n"
			<< setw(10) << left << "Total" << "$"
			<< quantity * price << "\n" << endl;
	}
	void simpleInfo()
	{
		cout << "Selection: " << name << ", price per unit: $" << price << endl;
	}

	Beverage() {}
	~Beverage() {}
	Beverage(string n, double p) {
		name = n;
		price = p;
	}
}; // END OF STRUCT

double totalCost(Beverage beverages[], int numOfBeverages);
void orderSummary(Beverage beverages[], int numOfBeverages);
bool isAllDigits(string input);
int getValidNumber(string text, int min, int max);
void doShopping();

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

	return Customer(custName, custAddress);
}


void displayMenu(string userName) //Menu function
{

	cout << endl << endl
		<< userName
		<< ", Please select the beverage you would like to purchase from the menu: "
		<< endl;

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

int main(void)
{
	doShopping();

	return 0;
}


int getQuantity() //Quantity function
{
	int quantity = 0;
	cout << "Enter quantity : ";
	cin >> quantity;
	return quantity;
}


bool isAllDigits(string input)
{
	for (unsigned i = 0; i < input.length(); i++) {
		if (!isdigit(input[i])) {
			return false;
		}
	}
	return true;
}

int getValidNumber(string text, int min, int max)
{
	string input{};
	int num{};
	while (true)
	{
		cout << text << " [" << min << " - " << max << "]: ";
		getline(cin, input);

		stringstream ss(input);

		if (!isAllDigits(ss.str())) {
			cout << "\nInvalid input. Try again!" << endl;
		}
		else {
			if (isAllDigits(ss.str()) && ss >> num) {
				if (num >= min && num <= max) {
					break;
				}
				else {
					std::cout << "\nOut of range. Try again!" << endl;
				}
			}
		}
	}
	return num;
}


double totalCost(Beverage beverages[], int numOfBeverages)  //Total cost function
{
	double totalCost = 0;

	for (int i = 0; i < numOfBeverages; i++) {
		totalCost += (beverages[i].price*beverages[i].quantity);
	}
	return totalCost;

}


void orderSummary(Beverage beverages[], int numOfBeverages) //Summary function
{
	cout << "\n======= ORDER SUMMARY ====" << endl;
	cout << "Items selected" << endl << endl;
	for (int i = 0; i < numOfBeverages; i++) {
		// only show beverages that has at least one order
		if (beverages[i].quantity > 0) {
			beverages[i].info();
		}
	}
}


void doShopping() //Shopping function
{
	// Beverage array to hold the beverages
	Beverage beverages[]{ { "Water", 1.45 },{ "Soda", 2.98 },{ "Ice Tea", 3.29 } };

	char selection = ' ';

	Customer patron = snafu(); // get info, create patron
	patron.simpleInfo(); // greet patron 

						 /*string name = "";
						 string address = "";

						 //Ask user for her/his name
						 cout << "Please enter your name ==> ";
						 getline(cin, name);

						 cout << "Please enter your address ==> ";
						 getline(cin, address);

						 //display user name
						 cout << "Hello " << name << endl;
						 cout << "Address: " << address << endl;
						 */

	do
	{
		// display menu
		displayMenu(patron.value());

		// read user selection
		cout << "Your selection: ";
		cin >> selection;
		cin.get(); // takes last ENTER character in the stream

		switch (selection)
		{
		case '1':
			beverages[0].simpleInfo();
			// assuming the customer can shop at least 1 and max 100
			beverages[0].quantity += getValidNumber("Enter quantity", 1, 100);
			break;
		case '2':
			beverages[1].simpleInfo();
			beverages[1].quantity += getValidNumber("Enter quantity", 1, 100);
			break;
		case '3':
			beverages[2].simpleInfo();
			beverages[2].quantity += getValidNumber("Enter quantity", 1, 100);
			break;
		case 'X':
		case 'x':
			orderSummary(beverages, 3);
			if (totalCost(beverages, 3) > 0) {
				cout << "\nGrand total = $" << totalCost(beverages, 3) << endl;
			}
			cout << "\nThank you for your purchase " << patron.value() << ", Come back soon!!!" << endl;
			break;
			// other than 1, 2, 3 and X...
		default: cout << "Invalid selection. Please try again";
			cin.get();
			// no break in the default case
		}
		cout << endl << endl;
	} while (selection != 'X' && selection != 'x'); // 'X' or 'x' displays the summary
	system("pause"); //Pauses the system until a key is hit, The the program will end.
}


All I need to do is replace quanity I have currently with an array for quantiy so there should be two arrays the one I have and the quantity which I am struggling with.

Please assist.
That was not my code jib.

So? Perhaps I wasn't asking you about the code but the person that posted that crap code.

Here is my code below:

That's nice, pretty much the same code as you had in the first post. I really don't understand your problem. Perhaps you need to simplify your code so that you can better understand what is happening.

How does the Customer relate to the Beverage?

What is the purpose of the quantity in the Beverage class?

Where do you have an array of anything called quantity?

I already have an array
Beverage beverages[]{ { "Water", 1.45 }, { "Soda", 2.98 }, { "Ice Tea", 3.29 } };
but I have to have two
1
2
Beverage beverages[]{ { "Water", 1.45 }, { "Soda", 2.98 }, { "Ice Tea", 3.29 } };
int quantities[ sizeof(beverages)/sizeof(beverages[0]) ] {};



jonnin wrote:
the pointers get rid of some excess copying

What copying?
Can someone review my code and make sure the arrays are appropriately being called and used. Any feedback on changes I need is welcome. Code below:

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
226
227
228
229
230
231
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream> // stringstream
#include <cctype> // isalph, isdigit
using namespace std;


class Customer
{
	string custName; // Customer name
	string custAddress; //Customer Address

public:
	void info()
	{
		cout << setw(10) << left << "Name" << custName << "\n"
			<< setw(10) << left << "Address" << custAddress << "\n" << endl;
	}
	void simpleInfo()
	{
		cout << "\nHello " << custName << "\nAddress: " << custAddress << endl;
	}
	std::string value() const {
		return custName;
	}

	Customer() {}
	~Customer() {}
	Customer(string n, string a) {
		custName = n;
		custAddress = a;
	}
}; // END OF CLASS

struct Beverage
{
	string name;	// The name
	int quantity{}; // The quantity
	double price{}; //The price

	void info()
	{
		cout << setw(10) << left << "Name" << name << "\n"
			<< setw(10) << left << "Price" << "$" << price << "\n"
			<< setw(10) << left << "Quantity" << quantity << "\n"
			<< setw(10) << left << "Total" << "$"
			<< quantity * price << "\n" << endl;
	}
	void simpleInfo()
	{
		cout << "Selection: " << name << ", price per unit: $" << price << endl;
	}

	Beverage() {}
	~Beverage() {}
	Beverage(string n, double p) {
		name = n;
		price = p;
	}
}; // END OF STRUCT

double totalCost(Beverage beverages[], int numOfBeverages);
void orderSummary(Beverage beverages[], int numOfBeverages);
bool isAllDigits(string input);
int getValidNumber(string text, int min, int max);
void doShopping();

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

	return Customer(custName, custAddress);
}


void displayMenu(string userName) //Menu function
{

	cout << endl << endl
		<< userName
		<< ", Please select the beverage you would like to purchase from the menu: "
		<< endl;

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

int main(void)
{
	doShopping();

	return 0;
}


int getQuantity() //Quantity function
{
	int quantity = 0;
	cout << "Enter quantity : ";
	cin >> quantity;
	return quantity;
}


bool isAllDigits(string input)
{
	for (unsigned i = 0; i < input.length(); i++) {
		if (!isdigit(input[i])) {
			return false;
		}
	}
	return true;
}

int getValidNumber(string text, int min, int max)
{
	string input{};
	int num{};
	while (true)
	{
		cout << text << " [" << min << " - " << max << "]: ";
		getline(cin, input);

		stringstream ss(input);

		if (!isAllDigits(ss.str())) {
			cout << "\nInvalid input. Try again!" << endl;
		}
		else {
			if (isAllDigits(ss.str()) && ss >> num) {
				if (num >= min && num <= max) {
					break;
				}
				else {
					std::cout << "\nOut of range. Try again!" << endl;
				}
			}
		}
	}
	return num;
}


double totalCost(Beverage beverages[], int numOfBeverages)  //Total cost function
{
	double totalCost = 0;

	for (int i = 0; i < numOfBeverages; i++) {
		totalCost += (beverages[i].price*beverages[i].quantity);
	}
	return totalCost;

}


void orderSummary(Beverage beverages[], int numOfBeverages) //Summary function
{
	cout << "\n======= ORDER SUMMARY ====" << endl;
	cout << "Items selected" << endl << endl;
	for (int i = 0; i < numOfBeverages; i++) {
		// only show beverages that has at least one order
		if (beverages[i].quantity > 0) {
			beverages[i].info();
		}
	}
}


void doShopping() //Shopping function
{
	// Beverage array to hold the beverages
	Beverage beverages[]{ { "Water", 1.45 },{ "Soda", 2.98 },{ "Ice Tea", 3.29 } };
	int quantities[sizeof(beverages) / sizeof(beverages[0])]{};

	char selection = ' ';

	Customer patron = snafu(); // get info, create patron
	patron.simpleInfo(); // greet patron 

	do
	{
		// display menu
		displayMenu(patron.value());

		// read user selection
		cout << "Your selection: ";
		cin >> selection;
		cin.get(); // takes last ENTER character in the stream

		switch (selection)
		{
		case '1':
			beverages[0].simpleInfo();
			// assuming the customer can shop at least 1 and max 100
			beverages[0].quantity += getValidNumber("Enter quantity", 1, 100);
			break;
		case '2':
			beverages[1].simpleInfo();
			beverages[1].quantity += getValidNumber("Enter quantity", 1, 100);
			break;
		case '3':
			beverages[2].simpleInfo();
			beverages[2].quantity += getValidNumber("Enter quantity", 1, 100);
			break;
		case 'X':
		case 'x':
			orderSummary(beverages, 3);
			if (totalCost(beverages, 3) > 0) {
				cout << "\nGrand total = $" << totalCost(beverages, 3) << endl;
			}
			cout << "\nThank you for your purchase " << patron.value() << ", Come back soon!!!" << endl;
			break;
			// other than 1, 2, 3 and X...
		default: cout << "Invalid selection. Please try again";
			cin.get();
			// no break in the default case
		}
		cout << endl << endl;
	} while (selection != 'X' && selection != 'x'); // 'X' or 'x' displays the summary
	system("pause"); //Pauses the system until a key is hit, The the program will end.
}
Last edited on
I have been using feedback to make changes and am having issues with it.

Code:

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
#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
	{
		std::cout << "Name" << custName << "\n"
			"Address" << custAddress << "\n" << std::endl;
	}
	void custWelcome(std::ostream &) const
	{
		std::cout << "\nHello " << custName << "\nAddress: " << custAddress << std::endl;
	}
	std::string custValue() const;
	Customer(const std::string &custName, const std::string &custAddress);
};

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 &name, double price);
};


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

typedef int Quantities[AVAILABLE_PRODUCTS_COUNT];


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

Customer patName()
{
	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) {
	Quantities ordered = { 0 };
	Customer patron = getCustomerFromUser();
	patron.custInfo(std::cout);

	bool done = false;
	while (!done) {
		displayMenu(patron);
		switch (getCharFromUser("Your selection: ")) {
		case '1':
			loadProductQuantity(AVAILABLE_PRODUCTS[0], ordered);
			break;
		case '2':
			loadProductQuantity(AVAILABLE_PRODUCTS[1], ordered);
			break;
		case '3':
			loadProductQuantity(AVAILABLE_PRODUCTS[2], ordered);
			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);

	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) {
			// print something for AVAILABLE_PRODUCTS[i] here
		}
	}
}


I have errors on lines 19-20, 37 and 53-55 I have green lines. I am confused by what I am doing wrong or not doing I apologize I am new at this so trying to grasp it I really could use the help and advice.
Where is the implementation of those functions?
I was able to create two out of three of the functions. I got help with this so I am unsure what to do with the last one and if I even did this properly. I put this in the post also but I really need help I only have 2hours until this is due. I am stressed and unable to think clearly and will definitely look through these tutorials. Just need to get this done. If you can please look at this and help me.

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
#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
	{
		std::cout << "Name" << custName << "\n"
			"Address" << custAddress << "\n" << std::endl;
	}
	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),
	Beverage("Soda", 2.98),
	Beverage("Ice Tea", 3.29)
};

typedef int Quantities[AVAILABLE_PRODUCTS_COUNT];


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



void getStringFromUser(std::string &prompt)
{
	
}

void getCharFromUser(std::string &prompt)
{
	std::cout << std::endl << std::endl
		<< &prompt
		<< ", 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;
}


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) {
	Quantities ordered = { 0 };
	Customer patron = getCustomerFromUser();
	patron.custInfo(std::cout);

	bool done = false;
	while (!done) {
		displayMenu(patron);
		switch (getCharFromUser("Your selection: ")) {
		case '1':
			loadProductQuantity(AVAILABLE_PRODUCTS[0], ordered);
			break;
		case '2':
			loadProductQuantity(AVAILABLE_PRODUCTS[1], ordered);
			break;
		case '3':
			loadProductQuantity(AVAILABLE_PRODUCTS[2], ordered);
			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);

	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;// print something for AVAILABLE_PRODUCTS[i]
		}
	}
}


This is the only part I am completely lost on:
1
2
3
4
5
void getStringFromUser(std::string &prompt) //Not sure what should go in this function
{

	
}


Please advise me I am trying to understand here.
Last edited on
Topic archived. No new replies allowed.