Class Problems

I have been trying to figure out how to use classes, I looked at the references and believe I have used it correctly, the code compiles successfully, but VS13 freezes after running it. Any ideas?

Thanks

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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>
#include <Vector>

using namespace std;

string user = "163";
string Directory = "Root";
string Key = "";

bool Mail1 = true;
bool Mail2 = false;
bool Mail3 = false;
bool Mail4 = false;
bool Mail5 = false;

class ConsoleClass
{
public:
	void inbox(int msgnum)
	{
		system("CLS");
		switch (msgnum)
		{
		case 1:
			string line;
			Mail1 = false;

			cout << user << "," << endl << endl;

			ifstream InboxMsgs("Inbox\\Msg1.dat");

			if (InboxMsgs.is_open())
			{
				while (!InboxMsgs.eof())
				{
					getline(InboxMsgs, line);
					cout << line << endl;
				}
			}
			break;
		}
	}

	void ckcmd(string cmd, string flag, int num)
	{
		switch (num)
		{
			//CD Commnad
		case 0:
			cout << "You are now browsing \"" << flag << "\"" << endl;
			break;
			//Start Command
		case 1:
			cout << "Starting " << flag << "..." << endl;
			break;
			//Help Command
		case 2:
			system("CLS");
			cout << "Commands" << endl;
			cout << "==================" << endl << endl;
			cout << "CD (Option) - Changes your current Directory to Option, directories ARE case sensitive" << endl;
			cout << "Start (Option) - Starts Option on localhost, programs ARE case sensitive" << endl;
			cout << "Inbox (Email #) - Opens Email (Email #)" << endl;
			cout << "Netuser (Option) - Option can be 'New'" << endl;
			cout << "Clear - Clears the screen" << endl;
			cout << "Help - Displays this page" << endl;
			cout << "Exit - Close's the terminal" << endl << endl;
			break;
			//Exit Command
		case 3:
			cout << "Exiting...." << endl;
			exit(0);
			break;
			//Clear Command
		case 4:
			system("CLS");
			break;
			//Netuser Command
		case 5:
			if (flag == "New" || flag == "new")
			{
				string newuser;
				string newpass;

				cout << "Username: ";
				cin >> newuser;

				ifstream newuseract("Accounts\\" + newuser + ".dat");


				if (newuseract.is_open())
				{
					cout << "Account already exists!" << endl;
					newuseract.close();
					getcmd();
				}
				else
				{
					cout << "Leave blank for no password" << endl;
					cout << "Password: ";
					cin.ignore();
					getline(cin, newpass);

					ofstream useract;
					useract.open("Accounts\\" + newuser + ".dat");
					useract << newpass;
					useract.close();

					cout << "Account successfully created!" << endl;
				}
			}
			break;
			//Search Command
		case 6:

			break;
			//Inbox Command
		case 10:
			int msgnum = atoi(flag.c_str());
			inbox(msgnum);
			break;
		}
	}

	void parse(string cmdtxt)
	{
		unsigned pos = cmdtxt.find(" ");
		string cmdbody = cmdtxt.substr(0, pos);
		string cmdflagtmp = cmdtxt.substr(pos + 1, 200);
		unsigned pos1 = cmdflagtmp.find(" ");
		string cmdflag = cmdtxt.substr(pos + 1, pos1);
		int cmdnum;

		if (cmdbody == "CD" || cmdbody == "Cd" || cmdbody == "cd")
		{
			cmdnum = 0;

			if (cmdbody == cmdflag)
			{
				cout << "ERROR: Error#762 - Command requires flag/Flag cannot be command" << endl;
				return;
			}

			bool DirAvail = ckdir(cmdflag);

			if (DirAvail == false)
			{
				cout << "ERROR: Error#103 - Directory not found" << endl;
				return;
			}
			ckcmd(cmdbody, cmdflag, cmdnum);
			return;
		}

		if (cmdbody == "Start" || cmdbody == "start")
		{
			cmdnum = 1;

			if (cmdbody == cmdflag)
			{
				cout << "ERROR: Error#762 - Command requires flag/Flag cannot be command" << endl;
				return;
			}
			ckcmd(cmdbody, cmdflag, cmdnum);
			return;
		}

		if (cmdbody == "Help" || cmdbody == "help" || cmdbody == "?")
		{
			cmdnum = 2;
			ckcmd(cmdbody, cmdflag, cmdnum);
			return;
		}

		if (cmdbody == "Exit" || cmdbody == "exit")
		{
			cmdnum = 3;
			ckcmd(cmdbody, cmdflag, cmdnum);
			return;
		}

		if (cmdbody == "Clear" || cmdbody == "clear")
		{
			cmdnum = 4;
			ckcmd
				(cmdbody, cmdflag, cmdnum);
			return;
		}

		if (cmdbody == "Inbox" || cmdbody == "inbox")
		{
			cmdnum = 10;

			if (cmdbody == cmdflag)
			{
				cout << "ERROR: Error#762 - Command requires flag/Flag cannot be command" << endl;
				return;
			}
			ckcmd(cmdbody, cmdflag, cmdnum);
			return;
		}

		if (cmdbody == "NetUser" || cmdbody == "Netuser" || cmdbody == "netuser")
		{
			cmdnum = 5;

			if (cmdbody == cmdflag)
			{
				cout << "ERROR: Error#762 - Command requires flag/Flag cannot be command" << endl;
				return;
			}
			ckcmd(cmdbody, cmdflag, cmdnum);
			return;
		}

		if (cmdbody == "Search" || cmdbody == "search")
		{
			cmdnum = 6;

			if (cmdbody == cmdflag)
			{
				cout << "ERROR: Error#762 - Command requires flag/Flag cannot be command" << endl;
				return;
			}
			ckcmd(cmdbody, cmdflag, cmdnum);
			return;
		}
		cout << "ERROR: Error#925 - Bad Command" << endl;
		return;
	}

	bool ckdir(string Dir)
	{
		int dirmax;
		int i = 0;
		string a;
		string line;
		vector<string> AllDir;

		ifstream Directories("Directories.dat");

		if (Directories.is_open())
		{
			while (!Directories.eof())
			{
				getline(Directories, line);

				if (i > 0)
					AllDir.push_back(line);

				if (i == 0)
				{
					a = line;
					int b = atoi(a.c_str());
					dirmax = b;
				}

				i++;
			}
		}

		for (int i = 0; i < dirmax; i++)
		{
			string LastDir = AllDir[i];
			if (Dir == LastDir)
			{
				Directory = Dir;
				return true;
			}
		}

		return false;
	}

	string login()
	{
		string user;
		string pass;

		cout << "Username: ";
		cin >> user;
		string haspass = ckuser(user);

		if (haspass == "no")
		{
			cout << "Successfully logged into " << user << endl;
			return user;
		}

		if (haspass == "163")
		{
			return "163";
		}

		cout << "Password: ";
		cin >> pass;

		bool IsVaild = cklogin(user, pass);
		if (IsVaild == false)
		{
			cout << "ERROR: Error#164 - Bad Login" << endl;
			return "164";
		}

		cout << "Successfully logged into " << user << endl;
		return user;
	}

	bool cklogin(string user, string passtmp)
	{
		string pass;
		ifstream Accounts("Accounts\\" + user + ".dat");

		if (Accounts.is_open())
		{
			while (getline(Accounts, pass))
			{
			}
			Accounts.close();
		}

		if (passtmp == pass)
			return true;

		if (passtmp != pass)
			return false;
	}

	string ckuser(string user)
	{
		string pass;
		ifstream Accounts("Accounts\\" + user + ".dat");

		if (Accounts.is_open())
		{
			while (getline(Accounts, pass))
			{
			}
			Accounts.close();
		}
		else
		{
			cout << "ERROR: Error#163 - Bad Login" << endl;
			return "163";
		}

		if (pass == "")
			return "no";

		if (pass != "")
			return "yes";
	}

	string getcmd()
	{
		string cmd;
		int i = 0;

		for (;;)
		{
			cout << endl << "User>>" << user << ">>" << Directory << ">> ";

			if (i == 1)
				cin.ignore(0);

			if (i == 0)
				cin.ignore(1);

			getline(cin, cmd);

			parse(cmd);
			i = 1;
		}

	}

	string encrypt(string msg, string Key)
	{
		std::string tmp(Key);
		while (Key.size() < msg.size())
			Key += tmp;

		for (std::string::size_type i = 0; i < msg.size(); ++i)
			msg[i] ^= Key[i];
		return msg;
	}

	string decrypt(string msg, string Key)
	{
		return encrypt(msg, Key);
	}

	void shop()
	{

	}
};


int main()
{
	ConsoleClass con;

	while (user == "163")
	{
		user = con.login();
	}

	if (Mail1 || Mail2 || Mail3 || Mail4 || Mail5)
	{
		cout << "Welcome, " << user << ". You have mail!" << endl;
	}

	con.getcmd();
}
400 lines is a lot to browse for an error.

Can you describe what you mean by "Freezes"? Do you know how to use your debugger? If so, try and break during this portion and see where you are in your code. Check in while statements for anything that could cause an infinite loop.

In terms of achieving object orientation, you've done what I would consider to be descent namespace implementation, but it's not really required for a class.

When using classes, you want to encapsulate data and the methods used to manipulate that data. What you've done here is you've put a bunch of functions into a class without any private data to be tracked by con. You also have several global variables.

I would turn my global variables into private members then access it like this:
1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
  ConsoleClass con;  // constructor sets member "user" to "163".

  while (con.GetUser() == "163") // Returns the user string
    con.Login();  // This runs the login scrip and sets the user internally

  if ( con.HasMail() ) // Mail1/2/3/4/5 are initialized in the constructor.  This method does the || for you.
    cout << "Welcome, " << con.GetUser() << ". You have mail!" << endl;

  con.getCmd();
}


Anything functions that you don't intend to use in main should be private or protected. Those functions clutter the interface so that it's tough to know what can/should be called from main. Only expose the functions which you want to be available to a user of con.
Last edited on
I know it is a lot, and I apologize, I am unsure of where or what the problem is. When I mean freezes, it becomes unresponsive and makes me restart the program. Thank you for the advice.
After initializing the variables in the class as you suggested, I decided to see if me running it in VS10 would make a difference, and it did, instead of crashing I got a compile error
 
error C2864: 'ConsoleClass::Mail2' : only static const integral data members can be initialized within a class


That is the error thrown by Mail2, but there is one for Mail1-5 and all the other variables in the previously listed code. Any ideas how I can fix this?
if you put bool Mail1 = true; or similar in class declaration then the error is because this is not allowed in class declarations. You initialize the class members in the constructor of the class.

Like
1
2
ConsoleClass(): Mail1(true), Mail2(false), Mail3(false), Mail4(false), Mail5(false)
{ //other things } 


I anyway suspect the compiler tool chain is not culprit in your program becoming unresponsive.

Also, Stewbond suggested good OO programming, that is not the solution for "freezing" problem.
I suggest you "single step" through your program in debug mode and find out what is causing this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
        for (;;)
	{
		cout << endl << "User>>" << user << ">>" << Directory << ">> ";

		if (i == 1)
			cin.ignore(0);

		if (i == 0)
			cin.ignore(1);

		getline(cin, cmd);

		parse(cmd);
		i = 1;
	}


At line 362 is a good candidate for causing this
Topic archived. No new replies allowed.