loop goes forever at wrong spot

Pages: 12
i'm doing a do-while loop and all the braces match up, but when the user enters logout, it goes to the wrong spot then loops forever.
do while loops are generally bad practice. Are you 100% sure that you want to execute the body of the loop at least once?

You can easily convert any do while loop into a while loop which will not execute the loop unless a certain condition is met.

And how the hell do you expect us to help you out when you don't even list the code? lol

C'mon man!
Last edited on
Code please?
the brackets are all screwed up because i needed to selective cut and paste from my giant code. sorry for all the work

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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// project2.1.cpp : Defines the entry point for the console application.
//

// newproject2.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <fstream>
#include <string>
#include <math.h>
#include <cstdlib>

using namespace std;

class managerMenu{
public:
	managerMenu();
	string homeMenu;
};

managerMenu::managerMenu(){
	cout<<"Select an option below (1,2,3)"<<endl
		<<"1.  Add Booking"<<endl
		<<"2.  Edit Booking"<<endl
		<<"3.  Remove Booking"<<endl
		<<"4.  Change Rates"<<endl
		<<"Press '0' to return to main menu."<<endl;
};

class userMenu{
public:
	userMenu();
	string homeMenu;
	string add;
	string remove;
	string edit;
};		//webUser menu class

userMenu::userMenu(){
	cout<<"Select an option below (1,2,3)"<<endl
		<<"1.  Add Booking"<<endl
		<<"2.  Edit Booking"<<endl
		<<"3.  Remove Booking"<<endl
		<<"Press '0' to return to main menu."<<endl;
};



class addBooking{			
	public:
	addBooking();
	string addMenu;
	string bookName;
	int startDate;
	int currentDate;
	int lengthStay;
};							//add booking class
\

addBooking::addBooking(){
	

};			 //contructor  

class rateChange{
public:
	rateChange();
	int monthChange;
	int weekChange;
	int dayChange;
};

rateChange::rateChange(){
};

class rates{
public:
	rates();
	int displayMonthRates();
	int displayWeekRates();
	int displayDayRates();
	int monthRate;
	int weekRate;
	int dayRate;
};

rates::rates(){

	dayRate=100;
	weekRate=525;
	monthRate=1575;
};





class newUser{				//new user class

public:
	newUser();
	string name;
	string password;
	
};		
newUser::newUser(){		//new user constrctor

	

};	 


int main(){

	
	int add=1;
	int edit=2;
	int remove=3;
	newUser newUserOne;
	int opselect;
	int userselect;
	int accesslevel;
	int rateSelect;
	int paySelect;
	rates showRate;
	rateChange rates;
	int mainRateSelect;
	int returnMenu=0;
	addBooking addMenu;
	addBooking addBook;
	string*bN=&addBook.bookName;
	int checkSelect;
	string logout=logout;
	
	

	fstream reservationFile;												//creating output stream file to write reservation input

	reservationFile.open("reservationLog.txt", fstream::in | fstream::out);
	(ios::app);

	if(reservationFile.fail()){
		cout<<"File reservation unsuccessfully opened."<<endl
			<<"Please check to see if file exists."<<endl;
		exit(1);
	}


	fstream rateFile;
	
	rateFile.open("rate.txt");
		if(rateFile.fail()){
			cout<<"File reservation unsuccessfully opened."<<endl
			<<"Please check to see if file exists."<<endl;
		exit(1);
	}
cout<<"File 'rate.txt' opened for writing"<<endl;

	fstream userFile;													//create output stream file to write user login input

	userFile.open("userinfo.txt.");
	(ios::app);
	if(userFile.fail()){
		cout<<"File userinfo unsuccessfully opened."<<endl
			<<"Please check to see if file exists."<<endl;
		exit(1);
	}

	cout<<"\n the file userinfo.txt has been successfully opened for writing."<<endl;

	do{
	cout<<"Welcome to the Shady Inn Motel Reservation System!"<<endl
		<<"Are you a new user?"<<endl
		<<"1.  Yes"<<endl
		<<"2.  No"<<endl;
	cin>>userselect;
	switch(userselect){
	case 1:{

	cout<<"Welcome new user!:"<<endl;

	

	
	cout<<"Please enter user name:"<<endl;					//enter username
	cin>>newUserOne.name;
	userFile<<newUserOne.name;								//write username to userFile	
		cout<<endl;
	cout<<"Please enter password:"<<endl;					//enter password
	cin>>newUserOne.password;
		userFile<<newUserOne.password;						//write password to user file
		cout<<"Please go to manager first to input rates."<<endl;
		cout<<"Please select access level:"<<endl
			<<"1.  Manager"<<endl
			<<"2.  User"<<endl
			<<"3.  Clerk"<<endl
			<<"4.  Travel Agent"<<endl;
	
		cin>>accesslevel;
		userFile<<accesslevel;
		switch(accesslevel){
		case 1:{						//manager
			
		
			do{
				
				cout<<"Select an option below (1,2,3,4,5)"<<endl
		<<"1.  Add Booking"<<endl
		<<"2.  Edit Booking"<<endl
		<<"3.  Remove Booking"<<endl
		<<"4.  Change Rates"<<endl
		<<"5.  Check-in/Check-out"<<endl
		<<"enter to logout to start over."<<endl;
				
			//cout<<menu.homeMenu<<endl;
			cin>>opselect;
			
			
			
		switch(opselect){								//switch statement to help with menu selection
		case 1:{				//selected add bookin menu
			
				cout<<"If Booked 2 weeks in advanced, 3% off lodging only"<<endl;			
				
				cout<<"Please enter book name:"<<endl;								//enter book name
				
				cin>>addBook.bookName;
				
				reservationFile<<addBook.bookName;									//write book name to reservationFile file
				
		
				cout<<endl<<"please enter toady's date(yyyymmdd):"<<endl;			//enter current date
				cin>>addMenu.currentDate;	
				reservationFile<<addMenu.currentDate;								//write current date to reservationfile
				cout<<endl<<"please enter start date:(yyyymmdd)"<<endl;				//enter start date
				cin>>addMenu.startDate;
				reservationFile<<addMenu.startDate;									//write start date to reservationfile
			 
				

				cout<<"Monthly Rate:"<<rates.monthChange<<endl				//display month rate
					<<"Weekly Rate:"<<rates.weekChange<<endl				//display week rate
					<<"Daily Rate:"<<rates.dayChange<<endl;				//display day rate

				cout<<"Please enter length of stay:"<<endl;						//enter length of stay
				cin>>addMenu.lengthStay;
				reservationFile<<addMenu.lengthStay;							//write length of stay to reservation file


				cout<<addMenu.lengthStay/30<<"Months"<<endl							//use algorithm to find out # of months,weeks, and days
				<<addMenu.lengthStay%30/7<<"Weeks"<<endl
				<<addMenu.lengthStay%30%7<<"Days"<<endl;

				cout<<"Wholesale or Retail?"<<endl
					<<"1.  Retail"<<endl
					<<"2.  Wholesale"<<endl;
				cin>>mainRateSelect;
				switch(mainRateSelect){
					
				case 1:{
					cout<<((addMenu.lengthStay/30*rates.monthChange)+(addMenu.lengthStay%30/7*rates.weekChange)+(addMenu.lengthStay%30%7*rates.dayChange))<<endl;
					cout<<"How would you like to pay?"<<endl
					<<"1.  cash(10%off):"<<endl
					<<"2.  credit card(8% charge):"<<endl;
				cin>>paySelect;
				switch(paySelect){
		case 1:{
			cout<<"Your total cost is:"<<endl
				<<((addMenu.lengthStay/30*rates.monthChange)+(addMenu.lengthStay%30/7*rates.weekChange)+(addMenu.lengthStay%30%7*rates.dayChange))*.9<<endl;
			   break;
			   }
		case 2:{cout<<"Your total cost is:"<<endl
				<<((addMenu.lengthStay/30*rates.monthChange)+(addMenu.lengthStay%30/7*rates.weekChange)+(addMenu.lengthStay%30%7*rates.dayChange))*1.08<<endl;
			   break;
			   }
					break;
				}break;}
					   
				case 2:{
					cout<<((addMenu.lengthStay/30*rates.monthChange)+(addMenu.lengthStay%30/7*rates.weekChange)+(addMenu.lengthStay%30%7*rates.dayChange))*.75<<endl;
				cout<<"How would you like to pay?"<<endl
					<<"1.  cash(10%off):"<<endl
					<<"2.  credit card(8% charge):"<<endl;
				cin>>paySelect;
				switch(paySelect){
		case 1:{
			cout<<"Your total cost is:"<<endl
				<<((addMenu.lengthStay/30*rates.monthChange)+(addMenu.lengthStay%30/7*rates.weekChange)+(addMenu.lengthStay%30%7*rates.dayChange))*.75*.9<<endl;
			   break;
			   };
		case 2:{cout<<"Your total cost is:"<<endl
				<<((addMenu.lengthStay/30*rates.monthChange)+(addMenu.lengthStay%30/7*rates.weekChange)+(addMenu.lengthStay%30%7*rates.dayChange))*.75*1.08<<endl;
			   break;
			   };
				}}}
				break;
			   }
				cout<<endl<<"Press '0' to return to menu."<<endl;
				cin>>returnMenu;
			
		
		}	}		
	

			
			
		
	while(cin>>logout);
	return 0;
	}}}}}
Seriously bro?

Just paste the loop that is giving you trouble. Nobody wants to read through all that. 100 lines is acceptable but 300 is pushing it.
Last edited on
so does the user hit [enter] to logout ? // is that what you mean with this:
<<"enter to logout to start over."<<endl;
Last edited on
Ok, Now I know who you are since you have a couple threads with this program split up. I'm done giving you code examples but I will give you some overall advice.

Don't use classes unless you plan to actually hide data from the client.

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
class addBooking{			
	public:
	addBooking();
	string addMenu;
	string bookName;
	int startDate;
	int currentDate;
	int lengthStay;
};							//add booking class
\

addBooking::addBooking(){
	

};			 //contructor  

class rateChange{
public:
	rateChange();
	int monthChange;
	int weekChange;
	int dayChange;
};

rateChange::rateChange(){
};

class rates{
public:
	rates();
	int displayMonthRates();
	int displayWeekRates();
	int displayDayRates();
	int monthRate;
	int weekRate;
	int dayRate;
};


It's all useless. Classes are private by default so if you just use structs you can save yourself from typing public.

Also this:

1
2
3
4
5
addBooking::addBooking(){
};			 //contructor  

rateChange::rateChange(){
};


is 100% useless. The compiler will issue it's own default constructor so unless you plan to actually do something in there you do not need to define it. The same goes for the copy constructor, assignment operator and destructor. UNLESS you have dynamic data or an inheritance hierarchy you shouldn't have to worry about defining them yourself.

And switch statements inside of switch statements is a bit much. Try using else if statements instead.

Hope this helps.
Last edited on
just a question...i have do string data types exist in C++??????????? and secondly i see that the second case of the most outer switch statement hasnt been declared at all...............lastly i wud say that the condition in the while is always true ?????????????
hey icethatjaw: i use do-while loops all of the time and my programs work just fine what do you mean they are bad practice
I have had conversations with a few experienced C++ programmers and whenever do while loops were brought up they all said that while loops were superior.

There is nothing wrong with a do while as long as you are 100% sure you want it to execute at least once. But in most i/o situations a simple primed while loop will be the simplest route.
Last edited on
oh yes i agree in most cases a for or while loop is better but i have had situations where a do while is more effective
hey im using Borland Compiler for C++ ver 3.0 and it does not accept string type of data......??????????????????????????????????????????????????
did you remember the header file? and using namespace std
@aramil ive included string.h wat els is neded
just string no .h then where you would put char, int, double, etc. just put string then the variable name
Why on earth would do-while be "bad practice"? There's a world of difference between "bad practice" and you're using it wrong.
gamecodezs:

devide this by 0. This will keep the variable a real number


Not sure what 'this' is. But dividing any varible by 0 is not going to keep it a real number.

IceThatJaw:

Don't use classes unless you plan to actually hide data from the client.


Not sure I agree. Yes, sometimes there is a use for entirely public objects, and using a struct would save the 'public:'. But its a real pain remembering which objects are structs and which are classes - which we need to do to avoid complier complain when declaring objects that are only pointers or references in headers. I think its easiest just to use classes.

Why on earth would do-while be "bad practice"? There's a world of difference between "bad practice" and you're using it wrong.


Memories of goto surface...


why is it a pain to remember? im pretty sure that unless it is going to be a huge project like world of warcraft, with multiple working on (probably) a million lines of code, there won't be more than 20 objects. Memorization should be a habit in c++. When I declare variables I know exactly what data type it is. Wouldn't that be same with an object because it is essentially a variable of the struct or class?
Memorization should be a habit in c++
I disagree. Memorization is a waste of time.
You've got the documentation to look for.
Hmm. I think memorization is important in C++. That way you wont even have to look at the documentation. You'll just know =].
Last edited on
Pages: 12