Comparing strings and recursion

closed account (Sw07fSEw)
Hi guys,

This is my first post here, so please correct me if I'm doing anything wrong. I started getting into C++ a lot the past week and have been trying to go beyond what I'm being taught in class. The past few days I've spent quite a few hours updating a scenario which prompts the user to order a pizza. I'm trying to make it as realistic as possible, while using basic procedures and concepts that I'm familiar with. In my program asking users whether or not they would like to add toppings to their pizza. "Would you like to add pepperoni to your pizza?". These questions being yes/no makes me want to use something boolean-related, but at the same time, I'd like the user to be able to input a string "Yes" or something similar instead of having input 1 or 0 to set the boolean values. So far I've come up with the code below, and while it works, I'm certain there's a more efficient way to compose it, but I'm not sure how. I tried using the 'Or' operator || and that didn't seem to work. Would any of you know a better way to compare the cases?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  void Order::promptSausage(){
	string yN;
	cout << "Would you like to add sausage to your pizza?" << endl;
	cin >> yN;
	sausage = false;
	if(yN == "Yes"){ // I want to use if(yN == "Yes" || yN == "yes" || .....) but it doesn't seem to work, what's a more efficent way to check the cases?
	    sausage = true;
	    numToppings = numToppings + 1;
	}
	else if(yN == "yes"){
	    sausage = true;
	    numToppings = numToppings + 1;
	}
	else if(yN == "Y"){
	    sausage = true;
	    numToppings = numToppings + 1;
	}
	else if(yN == "y"){
	    sausage = true;
	    numToppings = numToppings + 1;
	}	
	promptOnion();


Hopefully that's formatting correctly. The other portion I'm struggling with in my program is recursion. After a user inputs information to set the size and toppings on a pizza, a function is called to display the details of the pizza. The function I have works partially. It adds pizzas to the detailed list, but the details (size, toppings) only relate to the most recent pizza. So if I'm ordering 5 pizzas, it'll show a list of 5 pizzas, but they'll have have the details as the 5th pizza. I'm almost certain this is because the local variables are being set overwritten by the most recent pizza, but I'm not sure how fix it. I'll show a snippet of the 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
void Order::displayPizza(){
	string displayPepperoni; //shows topping if topping is set to true
	string displaySausage;
	string displayOnion;
	
	if(pepperoni == true)
		displayPepperoni = "Pepperoni";
	else{
		displayPepperoni = "";
	}
	if(sausage == true)
		displaySausage = "Sausage";
	else{
		displaySausage = "";
	}
	if(onion == true)
		displayOnion = "Onion";
	else{
		displayOnion = "";
	}
	
	for(int i=1; i<=quantity; i++){ //calculates number of pizzas to display based on quantity
		string toppings;
		string _size;
		if(size == 'S') 
			_size = "Small";
		else if(size == 'M')
			_size = "Medium";// I want this to output the detail for each pizza, currently it creates a list of 
		else if(size == 'L')//  pizzas, but they all have the same details as most recent pizza, how can I modify this 
			_size = "Large";// to display the size and topping of each pizza? Arrays?
		else if(size == 'X')
			_size = "Extra Large";
		toppings = "Toppings: " + displayPepperoni + " " + displaySausage + " " + displayOnion; // combines all toppings into a string 
		cout << "Pizza #" << i << ":\t" <<  "Size: " << _size << "\t "<< toppings << endl; 
	}
	
	
}



I didn't want to blow up this post with 300 lines of code so I just tried to extract the most important pieces which I've been having trouble with. The whole program itself works with the exception of the recursion issue I mentioned above. I'd really appreciate your feedback.
closed account (Sw07fSEw)
Here's the program which compiles.
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include <iostream> 
#include <string>
using namespace std;

class Order{
	private:
		int quantity, numToppings;
		char size;
		double cost, totalCost;
		bool pepperoni, sausage, onion;
		long creditcardNumber, phoneNumber;
		string customerName;
		
	public:
		Order();
		Order(long creditcard, long phone);
		double updateCost();
		void setCustomerName(string name);
		void promptSize();
		void promptPepperoni();
		void promptSausage();
		void promptOnion();
		void promptAddPizza();
		void promptCreditCard(long card);
		void displayPizza();
		void setCreditCard(long card);
		void setPhone(long phone);
		void resetOrder();
		void reviewOrder();
		string getcustomerName();
		long getCreditCard();
		long getPhone();
		
};
Order::Order(){
	quantity = 0;
	totalCost = 0;
	numToppings = 0;
	customerName = "John Doe";
}
Order::Order(long creditcard, long phone){
	creditcardNumber = creditcard;
	phoneNumber = phone;
}
void Order::setCustomerName(string name){
	customerName = name;
}
void Order::promptSize(){ // asks user to input size.
	cout << "Choose your pizza size." << endl;
	cout << "Enter 'S' for small (12in)." << endl;
	cout << "Enter 'M' for medium (14in)." << endl;
	cout << "Enter 'L' for large (16in)." << endl;
	cout << "Enter 'X' for extra large (18in)." << endl;
	cin >> size;
	promptPepperoni(); // begining of toppings loop
}
void Order::promptPepperoni(){
	string yN;
	cout << "would you like to add pepperoni to your pizza?" << endl;
	cin >> yN;
	pepperoni = false; //indicates no topping, useful for adding another pizza so boolean values don't 
	if(yN == "Yes"){ // carry over 
	    pepperoni = true;
	    numToppings = numToppings + 1;
	}
	else if(yN == "yes"){
	    pepperoni = true; //variety of input will add pepperoni as a topping
	    numToppings = numToppings + 1;
	}
	else if(yN == "Y"){
	    pepperoni = true;
	    numToppings = numToppings + 1;
	}
	else if(yN == "y"){
	    pepperoni = true;
	    numToppings = numToppings + 1;
	}	
	promptSausage(); // moves onto next topping
}
void Order::promptSausage(){
	string yN;
	cout << "Would you like to add sausage to your pizza?" << endl;
	cin >> yN;
	sausage = false;
	if(yN == "Yes"){ // I want to use if(yN == "Yes" || yN == "yes" || .....) but it doesn't seem to work,
	    sausage = true; // what's a more efficent way to check the cases?
	    numToppings = numToppings + 1;
	}
	else if(yN == "yes"){
	    sausage = true;
	    numToppings = numToppings + 1;
	}
	else if(yN == "Y"){
	    sausage = true;
	    numToppings = numToppings + 1;
	}
	else if(yN == "y"){
	    sausage = true;
	    numToppings = numToppings + 1;
	}	
	promptOnion();
}
void Order::promptOnion(){
	string yN;
	cout << "would you like to add onions to your pizza?" << endl;
	cin >> yN;
	onion = false;
	if(yN == "Yes"){ 
	    onion = true;
	    numToppings = numToppings + 1;
	}
	else if(yN == "yes"){
	    onion = true;
	    numToppings = numToppings + 1;
	}
	else if(yN == "Y"){
	    onion = true;
	    numToppings = numToppings + 1;
	}
	else if(yN == "y"){
	    onion = true;
	    numToppings = numToppings + 1;
	}	
	quantity = quantity + 1; //updates the number of pizzas. mainly for displayPizza function
	displayPizza(); // shows the Pizza details
	updateCost(); 
	promptAddPizza(); 
}
void Order::promptAddPizza(){
	string addPizza;
	cout << "Would you like to add another pizza to your order? " ;
	cin >> addPizza;
	if(addPizza == "Yes"){ // if any of the conditions are met, the loop starts over at promptSize
	   promptSize();
	}
	else if(addPizza == "yes"){
	    promptSize();
	}
	else if(addPizza == "Y"){
	    promptSize();
	}
	else if(addPizza == "y"){
	    promptSize();
	}	
}
void Order::promptCreditCard(long card){ //called if creditcard# is less than 16 digits
	cout << "Please re-enter your credit card number: ";
	cin >> card;
	setCreditCard(card);
}
double Order::updateCost(){
	double cost =0; // sets LOCAL variable cost to 0, needs to be reset if more than 1 pizza
	double toppingsPerPizza = 0; // same as above
	if(size == 'S'){
		cost = 10;
	}
	else if(size == 'M'){
		cost = 12;
	}
	else if(size == 'L'){
		cost = 14;
	}
	else if(size == 'X'){
		cost = 16;
	}
	toppingsPerPizza = numToppings; 
	cost = cost + (toppingsPerPizza * 2); // $2 per topping, med pizza is 12+(2*2
	totalCost = totalCost + cost; //updates total cost by adding cost (subtotal + cost of new pizza)
	numToppings = 0; 
	toppingsPerPizza = 0;
	
	cout << "Your subtotal is $" << totalCost << endl;	//displays subtotal after every pizza added
}
void Order::displayPizza(){
	string displayPepperoni; //shows topping if topping is set to true
	string displaySausage;
	string displayOnion;
	
	if(pepperoni == true)
		displayPepperoni = "Pepperoni";
	else{
		displayPepperoni = "";
	}
	if(sausage == true)
		displaySausage = "Sausage";
	else{
		displaySausage = "";
	}
	if(onion == true)
		displayOnion = "Onion";
	else{
		displayOnion = "";
	}
	for(int i=1; i<=quantity; i++){ //calculates number of pizzas to display based on quantity
		string toppings;
		string _size;
		if(size == 'S') 
			_size = "Small";
		else if(size == 'M')
			_size = "Medium";// I want this to output the detail for each pizza, currently it creates a list of 
		else if(size == 'L')//  pizzas, but they all have the same details as most recent pizza, how can I modify this 
			_size = "Large";// to display the size and topping of each pizza? Arrays?
		else if(size == 'X')
			_size = "Extra Large";
		toppings = "Toppings: " + displayPepperoni + " " + displaySausage + " " + displayOnion;  
		cout << "Pizza #" << i << ":\t" <<  "Size: " << _size << "\t "<< toppings << endl; 
	}
}
void Order::setCreditCard(long card){ //receives card from int main 
	if (card > 1000000000000000){
		creditcardNumber = card;
		cout << "Thank you" << endl; // checks if card is valid (16 + digits), 
	}
	else {
		promptCreditCard(card); // prompts user to re-enter number if invalid
	}
}
void Order::setPhone(long phone){
    phoneNumber = phone;
}
void Order::resetOrder(){ //resets cost, toppings and quantity
	totalCost = 0;
	numToppings = 0;
	quantity = 0;
}
void Order::reviewOrder(){ //asks users to review information. Proceeds to creditcard if they comply, 
	string proceed; //otherwise calls function to reset order
	cout << "Proceed to checkout?" << endl;
	cin >> proceed;
	if(proceed == "No"){
	  	cout << "Your Order has been reset. Please re-enter the information." << endl; // same deal here as above,
			resetOrder(); //                         a lot of extra code, but I'm tyring to make it more advanced
			promptSize();//                             entering something case specific. 
			reviewOrder();
	}
	else if(proceed == "no"){
	    	cout << "Your Order has been reset. Please re-enter the information." << endl;
			resetOrder();
			promptSize();
			reviewOrder();
	}
	else if(proceed == "N"){
	    	cout << "Your Order has been reset. Please re-enter the information." << endl;
			resetOrder();
			promptSize();
			reviewOrder();
	}
	else if(proceed == "n"){
	    	cout << "Your Order has been reset. Please re-enter the information." << endl;
			resetOrder();
			promptSize();
			reviewOrder();
	}	
}
string Order::getcustomerName(){
return customerName;
}
long Order::getCreditCard(){
	return creditcardNumber;
}
long Order::getPhone(){
	return phoneNumber;
}
int main(){
	
	string name;	
	long phone;
	long card;
	
	cout << "Please enter your name: ";
	getline(cin, name);
			
	Order order1;
	order1.setCustomerName(name);
	order1.promptSize(); // this function starts a chain for 4 or 5 more functions before proceeding to review order
	order1.reviewOrder();
	cout << "Please Enter your credit card number: " << endl;
	cin >> card;
	order1.setCreditCard(card);	
	cout << "Please Enter your phone number: ";
	cin >> phone;
	order1.setPhone(phone);
	cout << "Name: " << order1.getcustomerName() << endl;
	cout << "Credit Card #: " << order1.getCreditCard() << endl;
	cout << "Phone: " << order1.getPhone() << endl;
	cout << "Thanks for your order " << order1.getcustomerName() << endl;
	
	return 0;
}
closed account (48T7M4Gy)
What you need to do and especially because you are working ahead of class, is to use a 'container' to hold each pizza and another 'container' that goes with each pizza that holds information on toppings etc.

An array ( or vector ) are very suited to this.

so, for pizza[2] on the customer order you could have topping[1], topping[6] and topping[67] on that pizzas list of chosen toppings whatever they might be.

http://www.cplusplus.com/doc/tutorial/arrays/
http://www.cplusplus.com/reference/vector/vector/?kw=vector
Last edited on
All of the comparisons that do exactly the same thing if true is really painful to write and unsightly to read. There is no reason using the "||" operator shouldn't work if done coreectly. You must've made some other error before/after those tests. Try:

1
2
3
4
5
6
7
8
void function1()
{
   string str;
   //Do stuff with string here...
   if ((str == "something") || (str == "Something") || (str == "something2"))
      //Do whatever here...

}


Better, you can cut the number of comparisons by invoking toupper() or tolower() if you #include <cctype>. See:

1
2
3
4
5
6
7
8
9
10
#include<cctype>
void function1()
{
   string str;
   //Do stuff with string here... Make sure string is NOT empty() before calling front() !!
   str.front() = toupper(str.front());
   if ((str == "Something") || (str == "Something2"))
      //Do whatever here...

}


Assuming both blocks are trying to do the same thing, the 2nd one saves you and your readers, and the CPU for that matter, some work by having fewer comparisons.
Topic archived. No new replies allowed.