help with finding bugs

program works fine, just would like help with spotting bugs and removing them.
also need to find out how to stop the song that plays when alarm goes off.

there is a little more work to do i know that, wanting to get it bug free before i finish the smaller details.

thanks

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
# include <iostream>
# include <iomanip>
# include <fstream>
# include <conio.h>
# include <ctime>
# include <string.h>
# include <string>
# include <windows.h>

using namespace std;

struct Alarm
{
	int alarmCounter;  // max 3 alarms
	int HourAlarm;
	int MinuteAlarm;
	int DayAlarm;
	int MonthAlarm;
	int YearAlarm;

	char password[20];
	int emptyCounter;
};

const char FILE_PATH[] = "F:\\alarm.txt";
const char FILE_PATH2[] = "F:\\password.txt";    //have password in main file, can move to another file when working

fstream MyFile( FILE_PATH, ios :: binary | ios :: in | ios :: out );
fstream MyFile2( FILE_PATH2, ios :: binary | ios :: in | ios :: out );

void createFile();
void displayMenu();
void switchFunction();
void addAlarm();
void changeAlarm();
void displayAlarms();
int checkAlarmCounter();
int checkPassword( char[] );
void createEmptyPassword();
void startClock();
void pause( int );

int main()
{
	char password[20] = {"abc"};    //if month is this then password is diff.

	Alarm temp;
	system("COLOR 0A");
	system("CLS");

	//PlaySound("D:\\Wide_Awake.wav",NULL,SND_FILENAME|SND_LOOP|SND_ASYNC);

	createFile();

	createEmptyPassword();

	MyFile.open( FILE_PATH, ios :: binary | ios :: in | ios :: out );
	MyFile.clear();
	MyFile.seekg( 0, ios :: beg );
	MyFile.read( ( char* ) &temp, sizeof( Alarm ) );

	int checkedPass = 0;
	checkedPass = checkPassword( temp.password );

	if( checkedPass == 1 )
	{
		while( strcmp( temp.password, password ) == 0 )
		{
			switchFunction();
		}
	}
	if( checkedPass == 4 )
	{
		switchFunction();
	}

	if( checkedPass == 2 )
	{
		cout << "enter password to continue: ";
		cin >> temp.password;

		while( strcmp ( temp.password, password ) != 0 )
		{
			cout << "error: wrong password\n"
				  << "enter new password: ";
			cin >> temp.password;
		}
	}

	else
		if( checkedPass == 3 )
		{
			cout << "\n\aError: with password. \n\a";
		}

	system("CLS");
	cout << "GoodBye!";
	getch();

	MyFile2.close();
	MyFile.close();
	return 0;
}
//-------------------------------------------------------------------------
void createFile()
{
	if ( ! MyFile )
	{
		fstream MyFile( FILE_PATH, ios :: binary | ios :: out );
	}
	MyFile.close();

	if ( ! MyFile2 )
	{
		fstream MyFile2( FILE_PATH2, ios :: binary | ios :: out );
	}
	MyFile2.close();
}
//--------------------------------------------------------
int checkPassword( char inputPassword[] )
{
	char password[20] = {"abc"};    // if month is this then password is diff.

	Alarm temp;
	MyFile.open( FILE_PATH, ios :: binary | ios :: in );
	MyFile.clear();
	MyFile.seekg( 0, ios :: beg );
	MyFile.read( ( char* ) &temp, sizeof( Alarm ) );

	int returnCorrect = 1;
	int returnWrong = 2;
	int error = 3;
	int continueWithProgram = 4;

	while( !MyFile.eof() )
	{
		if( strcmp( temp.password, "EMPTY" )== 0 )
		{
			cout << "Enter new password to save to file ";
			cin >> temp.password;
			while( strcmp( temp.password, password ) != 0 )
			{
				cout << "error: incorrect password attempt to save to file.\n"
					  << "please try again. ";
				cin >> temp.password;
			}
				MyFile.close();

				temp.emptyCounter = 1;
				MyFile.open( FILE_PATH, ios :: binary | ios :: in | ios :: out );
				MyFile.clear();
				MyFile.write( ( const char* ) &temp, sizeof( Alarm ) );
				MyFile.close();

			return continueWithProgram;
		}

		if( strcmp( temp.password, password )== 0 )
		{
			cout << "\n\ncorrect password in file.\n\n" ;
			return returnCorrect;
		}
		else
			if( strcmp( temp.password, password )!= 0 )
			{
				cout << "\n\nwrong password in file\n\n";
				return returnWrong;
			}
		MyFile.read( ( char* ) &temp, sizeof( Alarm ) );
	}

	cout << "Error: courrupt files, delete them. "; getch();
	return error;
}
//--------------------------------------------------------------------
void displayMenu()
{
	cout << "do you want to add a new alarm time or change one?" << endl
		  << "\tpress 'd' to display alarms " << endl
		  << "\tpress 'a' to add " << endl
		  << "\tpress 'c' to change " << endl
		  << "\tpress 's' to start clock" << endl << endl;
}
//-------------------------------------------------------------
void switchFunction()
{
	char choice;

	displayMenu();
	choice = getch();

	while( choice != 27 )
	{
		switch ( choice )
		{
			case 'a':
			case 'A':
				addAlarm();
				break;

			case 'c':
			case 'C':
				changeAlarm();
				break;

			case 'd':
			case 'D':
				displayAlarms();
				break;

			case 's':
			case 'S':
				startClock();
				break;

			default:
				cout << "error:";
		}

		displayMenu();
		choice = getch();
	}
}
//----------------------------------------------------------
void addAlarm()
{
	Alarm temp;
	MyFile.open( FILE_PATH, ios :: binary | ios :: in | ios :: out );
	MyFile.clear();
	MyFile.seekg( 0, ios :: beg );
	MyFile.read( ( char* ) &temp, sizeof( Alarm ) );

	int alarmCount = 0;

	alarmCount = checkAlarmCounter();

	cout << "\n\n" << alarmCount << "\n\n";

	if( alarmCount == 2 )
	{
		system("CLS");
		cout << "\aError: with alarm count\n";
	}

	while( !MyFile.eof() )
	{
		if( temp.alarmCounter < 4 )
		{
			if( temp.alarmCounter == 0 )
			{
				MyFile.close();
				cout << "enter first alarm hour: ";
				cin >> temp.HourAlarm;

				while( temp.HourAlarm > 24 || temp.HourAlarm < 0 )
				{
					cout << "Error: enter hours within range 0-24 \n\n"
						  << "enter first alarm hour: ";
					 cin >> temp.HourAlarm;
				}

				cout << "enter first alarm minute: ";
				cin >> temp.MinuteAlarm;

				while( temp.MinuteAlarm  > 59 || temp.MinuteAlarm < 0 )
				{
					cout << "Error: enter minutes within range 0-59 "
						  << "enter first alarm minute: ";
					 cin >> temp.MinuteAlarm;
				}

				temp.alarmCounter++;
				cout << "\nAlarm Count is: " << temp.alarmCounter << "\n" ;

				MyFile.open( FILE_PATH, ios :: binary | ios :: in | ios :: out );
				MyFile.seekg( ( 1 - 1 ) * sizeof( Alarm ) );
				MyFile.clear();
				MyFile.write( ( const char* ) &temp, sizeof( Alarm ) );

				MyFile.close();
			}

			if( temp.alarmCounter <= 2 || temp.alarmCounter != 1 || temp.alarmCounter != 3)
			{
				MyFile.close();

				cout << "enter second alarm hour: ";
				cin >> temp.HourAlarm;

				while( temp.HourAlarm > 24 || temp.HourAlarm < 0 )
				{
					cout << "Error: enter hours within range 0-24 \n\n"
						  << "enter second alarm hour: ";
					cin >> temp.HourAlarm;
				}

				cout << "enter second alarm minute: ";
				cin >> temp.MinuteAlarm;

				while( temp.MinuteAlarm  > 59 || temp.MinuteAlarm < 0 )
				{
					cout << "Error: enter minutes within range 0-59 "
						  << "enter second alarm minute: ";
					cin >> temp.MinuteAlarm;
				}

				temp.alarmCounter++;
				cout << "\nAlarm Count is: " << temp.alarmCounter << "\n" ;

				MyFile.open( FILE_PATH, ios :: binary | ios :: app );
				MyFile.clear();
				MyFile.write( ( const char* ) &temp, sizeof( Alarm ) );
				MyFile.close();
			}

			if( temp.alarmCounter <= 3 || temp.alarmCounter != 1 || temp.alarmCounter != 2 )
			{
				MyFile.close();

				cout << "enter third alarm hour: ";
				cin >> temp.HourAlarm;

				while( temp.HourAlarm > 24 || temp.HourAlarm < 0 )
				{
					cout << "Error: enter hours within range 0-24 \n\n"
						  << "enter third alarm hour: ";
					cin >> temp.HourAlarm;
				}

				cout << "enter third alarm minute: ";
				cin >> temp.MinuteAlarm;

				while( temp.MinuteAlarm  > 59 || temp.MinuteAlarm < 0 )
				{
					cout << "Error: enter minutes within range 0-59 "
						  << "enter third alarm minute: ";
					cin >> temp.MinuteAlarm;
				}

				temp.alarmCounter++;
				cout << "\nAlarm Count is: " << temp.alarmCounter << "\n" ;

				MyFile.open( FILE_PATH, ios :: binary | ios :: app );
				MyFile.clear();
				MyFile.write( ( const char* ) &temp, sizeof( Alarm ) );
				MyFile.close();
			}
		}
		MyFile.read( ( char* ) &temp, sizeof( Alarm ) );
	}
	MyFile.close();
}
//---------------------
//next slide. 
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
void changeAlarm()
{
	Alarm temp;
	int changeAlarm = 0;
	int yesNo = 0;
	int update = 0;
	int exitCorrectly = 0;

	MyFile.open( FILE_PATH, ios :: binary | ios :: in );
	MyFile.clear();
	MyFile.seekg( 0, ios :: beg );
	MyFile.read( ( char* ) &temp, sizeof( Alarm ) );

	cout << "enter alarm number to change ";
	cin >> changeAlarm;


	while( !MyFile.eof() && exitCorrectly != 1 )
	{
		if( temp.alarmCounter == changeAlarm )
		{
			MyFile.close();
			system("CLS");

			cout << "H " << temp.HourAlarm << " M " << temp.MinuteAlarm << "\n";

			MyFile.seekg( ( changeAlarm - 1 ) * sizeof( Alarm ) );

			cout << " is this the alarm you are wanting to change? "

				 << endl << " 'y' yes or 'n' no "
				 << endl << endl;
			yesNo = getch();

			switch( yesNo )
			{
				case 'y':
				case 'Y':
					cout << "\npress 'h' to change hour"
						  << "\npress 'm' to change minutes\n";

					update = getch();

					switch( update )
					{
						case 'h':
						case 'H':
							cout << "enter new hour: ";
							cin >> temp.HourAlarm;
							break;

						case 'm':
						case 'M':
							cout << "enter new minutes: ";
							cin >> temp.MinuteAlarm;
							break;

						default:
							cout << "ERROR: invalid choice" << endl;
					}
					system("CLS");
					cout << "Updateing details" << endl; _getch();

					MyFile.open( FILE_PATH, ios :: binary | ios :: in | ios :: out );
					MyFile.clear();
					MyFile.seekg( ( changeAlarm - 1 ) * sizeof( Alarm ) );
					MyFile.write( ( const char* ) &temp, sizeof( Alarm ) );

					MyFile.close();
					break;

				case 'n':
				case 'N':
					system("CLS");
					cout << "CANCLED\n";
					exitCorrectly = 1;

					MyFile.close();
					break;


				default:
					cout << "ERROR: invalid choice" << endl;
					exitCorrectly = 1;
			}
		}
		if( temp.alarmCounter != changeAlarm )
		{
			system("CLS");
			cout << "Error: Not found ! \n";
			//MyFile.close();
		}
		MyFile.read( ( char* ) &temp, sizeof( Alarm ) );
	}
	MyFile.close();
}
//-----------------------------
void displayAlarms()
{
	Alarm temp;
	MyFile.open( FILE_PATH, ios :: binary | ios :: in | ios :: out );
	MyFile.clear();
	MyFile.seekg( 0, ios :: beg );
	MyFile.read( ( char* ) &temp, sizeof( Alarm ) );

	system("CLS");
	cout << "\n\n";

	while( !MyFile.eof() )
	{
		cout << "\nAlarm Count is: " << temp.alarmCounter << "\n" ;

		cout << "H " << temp.HourAlarm << " M " << temp.MinuteAlarm << "\n";

		MyFile.read( ( char* ) &temp, sizeof( Alarm ) );
	}
	MyFile.close();
}
int checkAlarmCounter()
{
	MyFile.close();

	Alarm temp;
	int correct = 1;
	int error = 2;
	int MAX = 3;

	MyFile.open( FILE_PATH, ios :: binary | ios :: in );
	MyFile.clear();
	MyFile.seekg( 0, ios :: beg );
	MyFile.read( ( char* ) &temp, sizeof( Alarm ) );

	while( ! MyFile.eof() )
	{
		if( temp.alarmCounter > MAX  || temp.alarmCounter == 0 )
		{
			return correct;
		}

		MyFile.read( ( char* ) &temp, sizeof( Alarm ) );
	}

	MyFile.close();
	return error;
}
void createEmptyPassword()
{
	Alarm temp;
	MyFile.open( FILE_PATH, ios :: binary | ios :: in );
	MyFile.clear();
	MyFile.seekg( 0, ios :: beg );
	MyFile.read( ( char* ) &temp, sizeof( Alarm ) );

	if( strcmp( temp.password, "EMPTY" ) == 0 && temp.emptyCounter == 1 )
	{
		cout << "\n File not Empty \n";
	}

	else
		if( strcmp( temp.password, "EMPTY" ) != 0 && temp.emptyCounter != 1 )
		{
			MyFile.close();

			cout << "\n File Empty First time opening file \n";
			strcpy( temp.password, "EMPTY" );
			temp.emptyCounter = 1;

			temp.alarmCounter = 0;
			temp.HourAlarm = 0;
			temp.MinuteAlarm = 0;

			MyFile.open( FILE_PATH, ios :: binary | ios :: in | ios :: out );
			MyFile.clear();
			MyFile.seekg( ( 1 - 1 ) * sizeof( Alarm ) );
			MyFile.write( ( const char* ) &temp, sizeof( Alarm ) );
			MyFile.close();
		}

}
void startClock()
{
	Alarm temp;
	MyFile.open( FILE_PATH, ios :: binary | ios :: in | ios :: out );
	MyFile.clear();
	MyFile.seekg( 0, ios :: beg );
	MyFile.read( ( char* ) &temp, sizeof( Alarm ) );

	time_t currentTime;
	struct tm *localTime;

	time( &currentTime );
	localTime = localtime( &currentTime );

	int Day    = localTime->tm_mday;
	int Month  = localTime->tm_mon + 1;
	int Year   = localTime->tm_year + 1900;
	int Hour   = localTime->tm_hour;
	int Min    = localTime->tm_min;
	int Sec    = localTime->tm_sec;

	int alarmToRun = 0;
	int runYesNo = 0;
	int yesNo = 0;
	int pleaseWait = 1;
	int pleaseWait2 = 0;

	cout << "what alarm do you want to run? ";
	cin >> alarmToRun;

	while( !MyFile.eof() )
	{
		if( temp.alarmCounter == alarmToRun )
		{
			cout << "is this the alarm you would like to run?\n"
				  << "'y' yes, 'n' no \n "
				  << "H " << temp.HourAlarm << " M " << temp.MinuteAlarm << "\n";
			runYesNo = getch();

			switch( runYesNo )
			{
				case 'y':
				case 'Y':
					yesNo = 1;
					break;

				case 'n':
				case 'N':
					yesNo = 0;
					break;

				default:
					cout <<"\n\nError: with alarm to run.\n\n";
			}
		}
		MyFile.read( ( char* ) &temp, sizeof( Alarm ) );
	}
	MyFile.close();

	MyFile.open( FILE_PATH, ios :: binary | ios :: in | ios :: out );
	MyFile.clear();
	MyFile.seekg( ( alarmToRun - 1 ) * sizeof( Alarm ) );
	MyFile.read( ( char* ) &temp, sizeof( Alarm ) );

	cout << "you selected: ";
	if( yesNo == 1 )
	{
		cout << "yes\n"
			  << "current system time is: H " << Hour << " M " << Min << " \n\n";

		while( pleaseWait == 1 )
		{
			time_t currentTime;
			struct tm *localTime;

			time( &currentTime );
			localTime = localtime( &currentTime );

			int Day    = localTime->tm_mday;
			int Month  = localTime->tm_mon + 1;
			int Year   = localTime->tm_year + 1900;
			int Hour   = localTime->tm_hour;
			int Min    = localTime->tm_min;
			int Sec    = localTime->tm_sec;

			pleaseWait2++;

			if( pleaseWait2 % 2 == 0 )
			{
				system("CLS");
				cout << "please Wait for alarm.\n";

				cout << "current system time is: H " << Hour << " M " << Min << " S " << Sec << " \n\n";
				cout << "Your alarm is: H " << temp.HourAlarm << " M " << temp.MinuteAlarm << "\n\n";
				pause(1);
			}
				if( pleaseWait2 % 2 == 1 )
				{
					system("CLS");

					cout << "please Wait for alarm..\n";
					cout << "current system time is: H " << Hour << " M " << Min << " S " << Sec << " \n\n";
					cout << "Your alarm is: H " << temp.HourAlarm << " M " << temp.MinuteAlarm << "\n\n";
					pause(1);
				}

			if( Min == temp.MinuteAlarm && Hour == temp.HourAlarm )
			{
				system("CLS");
				cout << "current system time is: H " << Hour << " M " << Min << " \n\n";
				cout << "SUCCESS!!!";
				pleaseWait = 0;  //to make exit infinitfy loop

				PlaySound("D:\\Wide_Awake.wav",NULL,SND_FILENAME|SND_LOOP|SND_ASYNC);
				getch();
			}
		}
	}
	else
	{
		cout << "No\n";
	}

	cout << "exited loop\n\n";
	getch();

	PauseSound("D:\\Wide_Awake.wav",NULL,SND_FILENAME|SND_LOOP|SND_ASYNC);
	MyFile.close();
}
//---------------------------------------------
void pause( int dur )
{
	int temp = time(NULL) + dur;

	while(temp > time(NULL));
}
closed account (48T7M4Gy)
If it's working fine, what makes you think your 600+ lines might contain bugs?
when i try input parts where it requires numbers and i input letters. that breaks it.
i'm no professional programmer so i don't know "all" the bugs.

also how do i stop the song playing when the alarm reaches its timer?
closed account (48T7M4Gy)
OK, but what I suggest you do is copy out the parts that don't work and concentrate on that sub unit.

Debugging is about the hardest part of programming and trying to do it with 600+ lines instead of a test unit of say 20 lines, is basically a waste of time.

Once you have the test unit show us that.

Cheers :)
Topic archived. No new replies allowed.