My first usefull program.

Hi, I'm fairly new to c++ so I'm looking for some constructive criticism on my first "real" program i've created!

I watch a lot of TV series and cant keep track on what episode I'm on, so i decided to make myself a little program for that!
I'm very aware of that the program doesn't work for others unless they change some code, but this program is intended for my use only.

So here it is:

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
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
#include <sstream>

using namespace std;

int selectSerie();
void checkSerie();
void settingSerie();
void helpSerie();
void exitSerie();
void playSerie();

int main()
{
	system("title AceMice's Series Program");
	system("cls");

	int choice;
	bool running = true;

	while(running)
	{
		system("cls");

		cout << "Welcome, please insert what you would like to do." << endl;
		cout << "1: Watch a series." << endl;
		cout << "2: Check status." << endl;
		cout << "3: Settings." << endl;
		cout << "4: Help." << endl;
		cout << "0: Exit." << endl << endl;

		cout << "Choice: ";

		cin >> choice;

		switch(choice)
		{
		case 1:
			playSerie();
			break;
		case 2:
			checkSerie();
			break;
		case 3:
			settingSerie();
			break;
		case 4:
			helpSerie();
			break;
		case 0:
			exitSerie();
			break;
		default:
			cout << "That's not a valid answer!" << endl;
			system("pause");
			break;
		}
	}

	system("pause");
	return 0;
}

int selectSerie()
{
	system("cls");

	cout << "Please select the TV series!" << endl;
	cout << "1: How I Met Your Mother." << endl;
	cout << "2: Chuck." << endl;
	cout << "0: Go back!" << endl << endl;

	int choice;
	cout << "Choice: ";
	cin >> choice;

	return choice;
}

void checkSerie()
{
	system("cls");

	string serie;
	bool valid = true;

	switch(selectSerie())
	{
	case 1:
		serie = "How I Met Your Mother";
		break;
	case 2:
		serie = "Chuck";
		break;
	case 0:
		valid = false;
		break;
	default:
		cout << endl << "Not a valid choice!" << endl;
		valid = false;
		break;
	}
	
	if(valid)
	{
		system("cls");

		ofstream saveSerie;
		ifstream readSerie;
		int lastSeason;
		int lastEpisode;

		readSerie.open(string(serie) + ".txt");
		readSerie >> lastSeason >> lastEpisode;
		readSerie.close();

		cout << "Last " << serie << " episode watched was episode " << lastEpisode << ", season " << lastSeason << endl << endl;
		
		system("pause");
	}

}

void settingSerie()
{
	system("cls");
	
	string autoPlay;
	ofstream setting;
	int choice;

	setting.open("settingsSerie.txt");

	cout << "This is the settings page of this program!" << endl << endl;
	cout << "What would you like to do?" << endl;
	cout << "1. Change play settings." << endl;
	cout << "2. Change prev episode for a specific Tv Series." << endl << endl;
	cout << "Choice: ";
	cin >> choice;


	if(choice == 1)
	{
		cout << "Would you like your TV Series to play the next episode automaticly? (yes or no): ";
		cin >> autoPlay;

		if(autoPlay == "yes" || autoPlay == "Yes" || autoPlay == "YES")
		{	
			setting.clear();
			setting << 1;
			cout << "Settings saved!" << endl << endl;
		}
		else if(autoPlay == "no" || autoPlay == "No" || autoPlay == "YES")
		{
			setting.clear();
			setting << 0;
			cout << "Settings saved!" << endl << endl;
		}
		else
		{
			cout << autoPlay << " is not a valid answer!" << endl << endl;
		}
	}
	else if(choice == 2)
	{
		bool valid = true;
		string serie; 
		ofstream saveSerie;
		int lastSeason;
		int lastEpisode;

		switch(selectSerie())
		{
		case 1:
			serie = "How I Met Your Mother";
			break;
		case 2:
			serie = "Chuck";
			break;
		case 0:
			valid = false;
			break;
		default:
			cout << endl << "Not a valid choice!" << endl;
			valid = false;
			break;
		}

		cout << "What season are you on?(eg: 6): ";
		cin >> lastSeason;
		cout << "What episode where you on last?(eg: 12): ";
		cin >> lastEpisode;

		saveSerie.open(string(serie) + ".txt");
		saveSerie.clear();
		saveSerie << lastSeason << ' ' << lastEpisode;
		saveSerie.close();
	}
	else
	{
		cout << "That's not a valid choice!" << endl << endl;
	}
	system("pause");
}

void helpSerie()
{
	system("cls");

	cout << "Here you will find help you'll need to use this program properly." << endl << endl;
	
	system("pause");
}

void exitSerie()
{
	exit(1);
}


void playSerie()
{
	system("cls");

	int Fseason = 0;
	int Sseason = 1;
	int Fepisode = 0;
	int Sepisode = 2;
	int lastSeason = 0;
	int lastEpisode = 0;
	bool valid = true;
	string serie;



	switch(selectSerie())
	{
	case 1:
		serie = "How I Met Your Mother";
		break;
	case 2:
		serie = "Chuck";
		break;
	case 0:
		valid = false;
		break;
	default:
		cout << endl << "Not a valid choice!" << endl;
		valid = false;
		break;
	}

	if(valid)
	{
		system("cls");

		ofstream saveSerie;
		ifstream readSerie;
		ifstream readSetting;
		int setting;
		bool settingsValid;

		/*
		saveSerie.open(string(serie) + ".txt");
		readSerie.open(string(serie) + ".txt");

		int isEmpty = 0;
		
		if(isEmpty == 0)
		{
			saveSerie << lastSeason << ' ' << lastEpisode;
			saveSerie.close();
		}
		*/
		readSerie.open(string(serie) + ".txt");
		readSerie >> lastSeason >> lastEpisode;
		readSerie.close();

		string Fpath = "e:\\Engborg\\Filmer\\Serier\\";

		readSetting.open("settingsSerie.txt");
		readSetting >> setting;

		if(setting == 0)
		{
			cout << "Last season watched was: " << lastSeason << endl;
			cout << "Type a season number: ";

			cin >> Sseason;

			if(Sseason >= 10)
			{
				Sseason = Sseason - 10;
				Fseason = 1;
			}

			cout << "Last episode watched was: " << lastEpisode << endl;
			cout << "Type a episode number: ";

			cin >> Sepisode;

			if(Sepisode >= 10)
			{
				Sepisode = Sepisode - 10;
				Fepisode = 1;
			}
			settingsValid = true;
		}
		else if(setting == 1)
		{
			if(lastSeason >= 10)
			{
				Sseason = lastSeason - 10;
				Fseason = 1;
			}
			else
			{
				Sseason = lastSeason;
			}
			if(lastEpisode >= 10)
			{
				Sepisode = lastEpisode - 9;
				Fepisode = 1;
			}
			else if(lastEpisode >= 20)
			{
				Sepisode = lastEpisode - 19;
				Fepisode = 2;
			}
			else
			{
				Sepisode = lastEpisode;
			}
			settingsValid = true;
		}
		else
		{
			cout << "Something was wrong with the settings!" << endl;
			cout << "Cannot procceed!" << endl << endl;
			system("pause");
			settingsValid = false;
		}

		if(settingsValid)
		{
			stringstream sstm;
			sstm << "\"" << Fpath << serie << "//" << serie << " s" << Fseason << Sseason << "\\" << serie << " S" << Fseason << Sseason << "E" << Fepisode << Sepisode << ".avi" << "\"";
			string path = sstm.str();

			system(path.c_str());	

			if(Fseason == 1)
				lastSeason = Sseason + 10;
			else
				lastSeason = Sseason;
			if(Fepisode == 1)
				lastEpisode = Sepisode + 10;
			else if(Fepisode == 2)
				lastEpisode = Sepisode + 20;
			else
				lastEpisode = Sepisode;

			saveSerie.open(string(serie) + ".txt");
			saveSerie.clear();
			saveSerie << lastSeason << ' ' << lastEpisode;
			saveSerie.close();

			cout << endl;
		}

		system("pause");
	}
}


I hope it's not too bad! And sorry if my english isn't the best, it's not my primary language.

Thanks in advance,
AceMice
For a first program... That is ten times better than what I was doing when I first started. However, you're mainly going to get criticism of using 'system'. From what I have read on this forum and other websites, its a big 'no no'. Causes some security issues or something like that... Anyway great job!
Thanks! I've done some other small stuff, but not a complete program.
Yeah, I kinda saw that coming aswell, but since the program is for me only, I don't have to worry about security and stuff. But then again, it's best not to get used to using 'system' i guess.
Anyone else got any comments?
I think you did a great job!
Here's a twist (shortens code in main) just for fun.

Arrays of function pointers!!
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
int selectSerie();
void checkSerie();
void settingSerie();
void helpSerie();
void exitSerie();
void playSerie();

// declare and initialize an array of pointers to the 5 functions called in the switch.
void (*pf[])(void) = { exitSerie, playSerie, checkSerie,
                                  settingSerie, helpSerie };

int main()
{
	system("title AceMice's Series Program");
	system("cls");

	int choice;
	bool running = true;

	while(running)
	{
		system("cls");

		cout << "Welcome, please insert what you would like to do." << endl;
		cout << "1: Watch a series." << endl;
		cout << "2: Check status." << endl;
		cout << "3: Settings." << endl;
		cout << "4: Help." << endl;
		cout << "0: Exit." << endl << endl;

		cout << "Choice: ";

		cin >> choice;

                if( (choice>=0) && (choice<=4) )
                    pf[choice]();// calling chosen function
                else
                {
                    cout << "That's not a valid answer!" << endl;
                    system("pause");// added on edit
                }          
	}

	system("pause");
	return 0;
}
Last edited on
Thanks! Hmm.. I've never really understood pointers, guess it's what I should learn next?
Using function pointers wasn't necessary.

I just thought you'd think it was cool that it eliminated the need for a switch statement in main, but I see that didn't make much of an impression.

Learning pointers would be good, but it can be postponed for a long time because there is almost always some way to do without them (as with your switch statement).
Aha, okey! Thanks for the comments anyway, really appreciate it!
You're welcome.
I think I'm about ready for my next 6 month break from this place.
Why are you taking a break?
Periodic burnout. It's clearly cyclic for me here.
That's why it has taken me nearly 3 years to acquire the post count I have.
I just feel it coming. It's a familiar feeling now.

EDIT: I'll pull the plug as soon as the only other (semi) active thread I'm in dies, which should be soon since OP is the only respondent and I just gave him what he's here looking for.
Last edited on
Aha! Well, I was on this forum a couple of years back and it felt more active then.
Really? I think its quite active right now. (Well, I wasnt here a couple years ago. )
Last edited on
@Hazique: Maybe, haven't been here for that long this time, yet!

@usandfriends: I'll stop using that command. Always had my suspicions that it wasn't good, and know I know WHY! Thanks!
Looks good! Good job!!

I use the following code to pause my programs:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//Call from another function.
while( ! getKeyPressed() )
{ /* do nothing until key press */ }


//Include this for _kbhit()
#include <conio.h>

bool getKeyPressed()
{
if( _kbhit() )
	return true;

return false;
}


Hope this helps in some way (:
Last edited on
Thanks! Really appreciate it! Will add that to my program right away.
Is there any good alternative for the system("cls"); command? Makes the program a hole lot neater when you clear the cmd!

Here's how the program looks right now:

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
419
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
#include <sstream>
#include <conio.h>

using namespace std;

int selectSerie();
void checkSerie();
void settingSerie();
void helpSerie();
void exitSerie();
void playSerie();
bool keyPressed();

int main()
{
	system("title AceMice's Series Program");
	system("cls");

	int choice;
	bool running = true;

	while(running)
	{
		system("cls");

		cout << "Welcome, please insert what you would like to do." << endl;
		cout << "1: Watch a series." << endl;
		cout << "2: Check status." << endl;
		cout << "3: Settings." << endl;
		cout << "4: Help." << endl;
		cout << "0: Exit." << endl << endl;

		cout << "Choice: ";

		cin >> choice;

		switch(choice)
		{
		case 1:
			playSerie();
			break;
		case 2:
			checkSerie();
			break;
		case 3:
			settingSerie();
			break;
		case 4:
			helpSerie();
			break;
		case 0:
			exitSerie();
			break;
		default:
			cout << "That's not a valid answer!";
			while( ! keyPressed() );
			break;
		}
	}

	while( ! keyPressed() );
	return 0;
}

int selectSerie()
{
	system("cls");

	cout << "Please select the TV series!" << endl;
	cout << "1: How I Met Your Mother." << endl;
	cout << "2: Chuck." << endl;
	cout << "3: The Big Bang Theory." << endl;
	cout << "0: Go back!" << endl << endl;

	int choice;
	cout << "Choice: ";
	cin >> choice;

	return choice;
}

void checkSerie()
{
	system("cls");

	string serie;
	bool valid = true;

	switch(selectSerie())
	{
	case 1:
		serie = "How I Met Your Mother";
		break;
	case 2:
		serie = "Chuck";
		break;
	case 3:
		serie = "The Big Bang Theory";
		break;
	case 0:
		valid = false;
		break;
	default:
		cout << endl << "Not a valid choice!" << endl;
		valid = false;
		break;
	}
	
	if(valid)
	{
		system("cls");

		ofstream saveSerie;
		ifstream readSerie;
		int lastSeason;
		int lastEpisode;

		readSerie.open(string(serie) + ".txt");
		readSerie >> lastSeason >> lastEpisode;
		readSerie.close();

		cout << "Last " << serie << " episode watched was episode " << lastEpisode << ", season " << lastSeason << endl << endl;
		
		while( ! keyPressed() );
	}

}

void settingSerie()
{
	system("cls");
	
	string autoPlay;
	ofstream setting;
	int choice;

	cout << "This is the settings page of this program!" << endl << endl;
	cout << "What would you like to do?" << endl;
	cout << "1. Change play settings." << endl;
	cout << "2. Change prev episode for a specific Tv Series." << endl;
	cout << "0. Go back!" << endl << endl;
	cout << "Choice: ";
	cin >> choice;


	if(choice == 1)
	{
		cout << "Would you like your TV Series to play the next episode automaticly? (yes or no): ";
		cin >> autoPlay;

		if(autoPlay == "yes" || autoPlay == "Yes" || autoPlay == "YES")
		{	
			setting.open("settingsSerie.txt");
			setting.clear();
			setting << 1;
			setting.close();
			cout << "Settings saved!" << endl << endl;
		}
		else if(autoPlay == "no" || autoPlay == "No" || autoPlay == "YES")
		{
			setting.open("settingsSerie.txt");
			setting.clear();
			setting << 0;
			setting.close();
			cout << "Settings saved!" << endl << endl;
		}
		else
		{
			cout << autoPlay << " is not a valid answer!" << endl << endl;
		}
		while( ! keyPressed() );
	}
	else if(choice == 2)
	{
		bool valid = true;
		string serie; 
		ofstream saveSerie;
		int lastSeason;
		int lastEpisode;

		switch(selectSerie())
		{
		case 1:
			serie = "How I Met Your Mother";
			break;
		case 2:
			serie = "Chuck";
			break;
		case 3:
			serie = "The Big Bang Theory";
			break;
		case 0:
			valid = false;
			break;
		default:
			cout << endl << "Not a valid choice!" << endl;
			valid = false;
			break;
		}

		if(valid)
		{
			cout << "What season are you on?(eg: 6): ";
			cin >> lastSeason;
			cout << "What episode where you on last?(eg: 12): ";
			cin >> lastEpisode;

			saveSerie.open(string(serie) + ".txt");
			saveSerie.clear();
			saveSerie << lastSeason << ' ' << lastEpisode;
			saveSerie.close();
		}
		while( ! keyPressed() );
	}
	else if(choice == 0)
	{
		
	}
	else
	{
		cout << "That's not a valid choice!" << endl << endl;
		while( ! keyPressed() );
	}
}

void helpSerie()
{
	system("cls");

	cout << "Here you will find help you'll need to use this program properly." << endl << endl;
	cout << "If you've watched the last episode in a season you'll have to set the new season in settings and specify episode by typing 0." << endl << endl;
	
	while( ! keyPressed() );
}

void exitSerie()
{
	exit(1);
}


void playSerie()
{
	system("cls");

	int Fseason = 0;
	int Sseason = 1;
	int Fepisode = 0;
	int Sepisode = 2;
	int lastSeason = 0;
	int lastEpisode = 0;
	bool valid = true;
	bool exit = false;
	string serie;
	ifstream checkDisk;



	switch(selectSerie())
	{
	case 1:
		serie = "How I Met Your Mother";
		break;
	case 2:
		serie = "Chuck";
		break;
	case 3:
		serie = "The Big Bang Theory";
		break;
	case 0:
		valid = false;
		exit = true;
		break;
	default:
		cout << endl << "Not a valid choice!" << endl;
		valid = false;
		break;
	}

	if(!exit)
	{
		checkDisk.open("e:\\Engborg\\Filmer\\Serier\\checkDisk.txt");
	
		if(checkDisk.is_open())
		{
			valid = true;
			checkDisk.close();
		}
		else
		{
			valid = false;
			cout << "The disk was not ready, please try again in a couple of seconds!" << endl << endl;
			while( ! keyPressed() );
		}
	}

	if(valid)
	{
		system("cls");

		ofstream saveSerie;
		ifstream readSerie;
		ifstream readSetting;
		int setting;
		bool settingsValid;

		readSerie.open(string(serie) + ".txt");
		readSerie >> lastSeason >> lastEpisode;
		readSerie.close();

		string Fpath = "e:\\Engborg\\Filmer\\Serier\\";

		readSetting.open("settingsSerie.txt");
		readSetting >> setting;
		readSetting.close();

		if(setting == 0)
		{
			cout << "Last season watched was: " << lastSeason << endl;
			cout << "Type a season number: ";

			cin >> Sseason;

			if(Sseason >= 10)
			{
				Sseason = Sseason - 10;
				Fseason = 1;
			}

			cout << "Last episode watched was: " << lastEpisode << endl;
			cout << "Type a episode number: ";

			cin >> Sepisode;

			if(Sepisode >= 10)
			{
				Sepisode = Sepisode - 10;
				Fepisode = 1;
			}
			settingsValid = true;
		}
		else if(setting == 1)
		{
			if(lastSeason >= 10)
			{
				Sseason = lastSeason - 10;
				Fseason = 1;
			}
			else
			{
				Sseason = lastSeason;
			}
			if(lastEpisode >= 10)
			{
				Sepisode = lastEpisode - 9;
				Fepisode = 1;
			}
			else if(lastEpisode >= 20)
			{
				Sepisode = lastEpisode - 19;
				Fepisode = 2;
			}
			else
			{
				Sepisode = lastEpisode + 1;
			}
			settingsValid = true;
		}
		else
		{
			cout << "Something was wrong with the settings!" << endl;
			cout << "Cannot procceed!" << endl << endl;
			while( ! keyPressed() );
			settingsValid = false;
		}

		if(settingsValid)
		{
			stringstream sstm;
			sstm << "\"" << Fpath << serie << "//" << serie << " s" << Fseason << Sseason << "\\" << serie << " S" << Fseason << Sseason << "E" << Fepisode << Sepisode << ".avi" << "\"";
			string path = sstm.str();

			system(path.c_str());	

			if(Fseason == 1)
				lastSeason = Sseason + 10;
			else
				lastSeason = Sseason;
			if(Fepisode == 1)
				lastEpisode = Sepisode + 10;
			else if(Fepisode == 2)
				lastEpisode = Sepisode + 20;
			else
				lastEpisode = Sepisode;

			saveSerie.open(string(serie) + ".txt");
			saveSerie.clear();
			saveSerie << lastSeason << ' ' << lastEpisode;
			saveSerie.close();

			cout << endl;
		}

		while( ! keyPressed() );
	}
}

bool keyPressed()
{
if( _kbhit() )
	return true;

return false;
}
Last edited on
Topic archived. No new replies allowed.