Help adding a Class in C++ need help!!!

Pages: 12
Hello,

I am trying to incorporate the following into my code I have a difficult time adding these features please assist with adding the following to my program:

Design the customer class.
Include at least the customer name and address for the class.
Create class functions to set the class variables.
Create class functions to get the values of the class variables.
Create additional functions as necessary.
Complete the following in your code:
*Implement the new class
*Create an object from the new class
*Set the customer details using the class functions
*Use the class functions to access the customer details

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
  // Use of functions
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream> // stringstream
#include <cctype> // isalph, isdigit
using namespace std;


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();


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 = ' ';
	string name = "";

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

	//display user name
	cout << "Hello " + name << endl;

	do
	{
		system("cls");
		// display menu
		displayMenu(name);

		// 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, " << name << " 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.
}


I appreciate the assistance or any constructive advise thank you!
A struct is a class. The only difference is whether members are public or private by default.

You already have Beverage. Customer is no different. Both have names, one has price, the other should have address.
Hello keskiverto,

I appreciate this feedback, I am confused by this adding in the customer with address I am trying t figure it out but not having any luck. Would I add it just like I did with Beverage?

Then how do I add it below as well a little confused.
I created my Customer class now I need help figuring out how to call it throughout my program. My program first asks you to enter your name I need this to act when entered Name is stored in the class and need to ask to enter the address and store that as well I am having trouble figuring this out to successfully call my class and then any time name is asked for or displayed it will be using the class please help my following 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
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
#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

	void info()
	{
		cout << setw(10) << left << "Name" << custName << "\n"
			<< setw(10) << left << "Address" << custAddress << "\n" << endl;
	}
	void simpleInfo()
	{
		cout << "Hello: " << custName << " , Address: " << custAddress << endl;
	}

	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();


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 = ' ';
	string name = "";

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

	//display user name
	cout << "Hello " + name << endl;

	do
	{
		system("cls");
		// display menu
		displayMenu(name);

		// 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, " << name << " 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.
}


Thank you for any help!!!
Last edited on
Your lines 175-182 get customer info and greet the customer.

You could add one more member to the Customer: one that returns name as a std::string. You could call that member function on lines 188 and 216, where you now use plain string name.

You could make standalone function that asks things from user and returns a Customer object. Something almost, but not quite like:
1
2
3
Customer snafu() {
  return Customer( "John Doe", "North Pole" );
}


Then you can rewrite lines 175-182:
1
2
Customer beggar = snafu(); // get info, create beggar
beggar.simpleInfo(); // greet beggar 

So for my situation though if I put in a function like you said if I am asking the customer to enter their name and address in the beginning of the program how would I do this I am confused I do understand what you are saying but how would I sore the user input then return that value from what the customer entered.

If I did:

1
2
3
Customer snafu() {
  return Customer( "John Doe", "North Pole" );
}


This wouldn't be the customers input which is what I need correct. I just need advise on how to do this.
Last edited on
You can already ask, or you don't know the code you have already written.
Nevertheless, a stronger hint:
1
2
3
4
5
Customer snafu() {
  std::string foo;
  std::getline( std::cin, foo ); // ask
  return Customer( foo, "North Pole" ); // set and return
}
I know I can already ask I am getting confused because you said to replace those lines where it is asked. and currently it does not ask for the address I need to add that.

So program starts it says Please enter your name: TYPE NAME

Need to add please enter your address: TYPE ADDRESS

Hello NAME
Address: ADDRESS

these will be using the class but I am confused by your post that said to remove lines 175-182 because that would take out the promt to enter this information. I see if I do this:

1
2
3
4
5
6
Customer snafu() {
  std::string custName;
  std::string custAddress;
  std::getline( std::cin, custName, custAddress ); // ask
  return Customer( custName, custAddress ); // set and return
}


Would this work and does it just pull the input from what I currently have in my code? I apologize for my ignorance.

Then you can rewrite lines 175-182:
1
2
Customer patron = snafu(); // get info, create patron
patron.simpleInfo(); // greet patron 


I would just take the code below out:
Lines 175-182
1
2
3
4
5
6
7
8
string name = "";

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

	//display user name
	cout << "Hello " + name << endl;

I am also not sure where in my code to implement:

1
2
3
4
5
6
7
Customer snafu() {
  std::string custName;
  std::string custAddress;
  std::getline( std::cin, custName ); // ask, not sure if there should be two getlines or one
 std::getline( std::cin, custAddress ); // ask
  return Customer( custName, custAddress ); // set and return
}


Please advise on this and my last response please I am having a hard time.
So program starts it says Please enter your name: TYPE NAME

Need to add please enter your address: TYPE ADDRESS
1
2
3
4
5
6
7
8
9
10
Customer snafu() {
  std::string custName;
  std::string custAddress;
  // Should you show at this point: "Please enter your name: " ?
  std::getline( std::cin, custName );
  // Should you show at this point: "Please enter your address: " ?
  std::getline( std::cin, custAddress );

  return Customer( custName, custAddress );
}
So I added the code to my class. I am getting an error lines 188-189 I commented out my old code to test can you please review it and let me know. 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
246
247
248
#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

	void info()
	{
		cout << setw(10) << left << "Name" << custName << "\n"
			<< setw(10) << left << "Address" << custAddress << "\n" << endl;
	}
	void simpleInfo()
	{
		cout << "Hello: " << custName << " , Address: " << custAddress << endl;
	}

	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);
	}

	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();


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
	{
		system("cls");
		// display menu
		displayMenu(name);

		// 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, " << name << " 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.
}


Please let me know if there is anything I have done incorrectly. The function I am assuming does go into the class. Now just figuring out why it isn't being called just giving me an error.
Stating that something "gives an error" is not enough. The exact compiler error message has useful information.

One compiler says this about your code:
 In function 'void doShopping()':

188:26: error: 'snafu' was not declared in this scope

37:2: error: 'Customer::~Customer()' is private
188:26: error: within this context

19:7: error: 'void Customer::simpleInfo()' is private
189:20: error: within this context

210:15: error: 'name' was not declared in this scope
snafu() on line 188 is a function call (just like system("cls") on line 208 and displayMenu(name) on line 210). We (and compiler) can see void displayMenu(string) on global scope on line 78. There is no standalone function Customer snafu() on the global scope. Hence error. No, snafu was not supposed to be a member of Customer.

Line 188 calls (implicitly) destructor of class Customer. Alas, all members of class are private by default, just like all members of struct are public. You can selectively make class members public. See http://www.cplusplus.com/doc/tutorial/classes/

Line 189 calls another private member of Customer. Member void simpleInfo() has to be public.

Line 210. There is no longer variable 'name' in doShopping(). You were supposed to add a (public) member function to Customer that returns the customer's name (as a std::string) and call that member function where the 'name' is now.
Okay I fixed my code other than these following errors I am calling custName and it gives the error the two places I call it:


In function 'void doShopping()':
212:15: error: 'custName' was not declared in this scope


Here is my improved 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
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
#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;
	}
public:
	void simpleInfo()
	{
		cout << "Hello: " << custName << " , Address: " << custAddress << endl;
	}

	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
	{
		system("cls");
		// display menu
		displayMenu(custName);

		// 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, " << custName << " 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.
}


Now I just need to know where to declare this in the scope because
custName
is what the (std::string is) std::string custName;
Last edited on
Look at line 191. There you have patron.simpleInfo();.
The patron is a Customer and you are calling its member function simpleInfo.

However, the simpleInfo does not return anything.

Write a second member function that does return the value of patron's customer name.
I read through some stuff online about functions and I also took out the second public:

My problem is they show math concepts but I am having trouble understanding the concept of returning the value of the customer name and address asked by the user at the beginning of the program so if I used:

1
2
3
4
5
6
7
8
9
bool customerName(string input)
	{
		for (unsigned i = 0; i < input.length(); i++) {
			if (!isdigit(input[i])) {
				return false;
			}
		}
		return true;
	}


This is using numbers like I did for my quantity, not name values I am having a hard time understanding that. I am sure I don't need a loop just using this, for example, I am unsure is there any tutorials you can provide me to understand this better?
Last edited on
Number, string, Customer ... a returned object is a returned object.
Another example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

class Foo {
  int x;
public:
  Foo( int v ) : x( v ) {
  }

  int value() const {
    return x;
  }
};

int main() {
  Foo foo( 42 );
  std::cout << foo.value() << '\n';
}

Class Foo has a member function value() (lines 9-11).
The return type of that function is int (line 9).
The implementation of the function returns (line 10) value of the Foo's member x.

Code that has a Foo object, calls the member on the object (foo.value() on line 16). The returned value is then passed to cout.


Do you see classes Foo and Bar as entirely different, or as rather similar?


Lets do it again:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>

class Bar {
  std::string x;
public:
  Bar( std::string v ) : x( v ) {
  }

  std::string value() const {
    return x;
  }
};

int main() {
  Bar foo( "Hello world" );
  std::cout << foo.value() << '\n';
}

For this case we did change two things:
1. Name of class from Foo to Bar. No real change.
2. Type of member x from int to std::string. Types in constructor and value() were updated to match. The data that we create foo with was touched too.
I do see them as rather similar and that is easier to understand. So here is my updated Class code:
Does this look accurate?
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
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 << "Hello: " << custName << " , Address: " << custAddress << endl;
	}
	std::string value() const {
		return custName;
	}

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


Now I am still getting the error calling displayMenu(custName) line 214 with system("cls")

Here is my full 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
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
252
#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 << "Hello: " << custName << " , Address: " << 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
	{
		system("cls");
		// display menu
		displayMenu(custName);

		// 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, " << custName << " 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.
}


Error received:

In function 'void doShopping()':
214:15: error: 'custName' was not declared in this scope
Last edited on
so I declared custName below like so:

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
char selection = ' ';
	std::string custName;

	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
	{
		system("cls");
		// display menu
		displayMenu(custName);


So I am assuming this is not correct. I need to declare/call it from my class I am unsure how to do this for these parts to function like they should. I am unsure about why my Welcome message will not display?

Now it asks what it should but after you enter your name and address it should show "Hello: " << custName << " , Address: " << custAddress it just goes straight to the menu. Also when it gets down to the menu it should say "'custName', Please select the beverage you would like to purchase from the menu:" instead it shows no custName. and at the end it should show "Thank you for your purchase, " << custName << " Come back soon!!!" no custName displays what is the problem?

My 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
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
252
253
254
255
256
#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;
	}
public:
	void simpleInfo()
	{
		cout << "Hello: " << custName << " , Address: " << custAddress << endl;
	}
public:
	void simpleReturn()
	{

	}

	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 = ' ';
	std::string custName;

	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
	{
		system("cls");
		// display menu
		displayMenu(custName);

		// 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, " << custName << " 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
what is the problem?


You have equivalent of this:
1
2
3
4
5
6
7
#include <iostream>
#include <string>

int main() {
  std::string dummy;
  std::cout << "(" << dummy << ")\n";
}

That shows:
()

Why? Why there is nothing inside the parentheses?
That is almost true. There is nothing, but that is exactly what the dummy contains.

You do create on line 194 std::string custName;, an empty string that you then use.


Lets go back to my previous example program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>

class Bar {
  std::string x;
public:
  Bar( std::string v ) : x( v ) {
  }

  std::string value() const {
    return x;
  }
};

int main() {
  Bar foo( "Hello world" );
  std::cout << foo.value() << '\n';
}


What do I do on line 17?

Do I have std::cout << x << '\n'; in there?
No. That would give the error: 'x' was not declared in this scope.

Do I have the main as:
1
2
3
4
5
int main() {
  std::string custName;
  Bar foo( "Hello world" );
  std::cout << custName << '\n';
}

No. I don't add dummy variables to hide logical errors.

What is in there then?
foo.value()
Object foo calling its member variable value().
Nothing more, nothing less.
Okay I finally understand!!!


The only thing that is not working is after you enter you name and address it does not show the greeting message like it should?


Hello: custName, Adress: custAdress


Here is my updated 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
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
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream> // stringstream
#include <cctype> // isalph, isdigit
using namespace std;


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

public:
	void info()
	{
		cout << setw(10) << left << "Name" << custName << "\n"
			<< setw(10) << left << "Address" << custAddress << "\n" << endl;
	}

	void simpleInfo()
	{
		cout << "Hello: " << custName << " , Address: " << 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 = ' ';
	//std::string custName;

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

	do
	{
		system("cls");
		// 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.
}
Pages: 12