Unhandled exception? Need Help!

Here is my 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
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

struct task {
	char myClass[100];
	char assign[100];
	char due[10];
};

void getString(char input[], int num);
void loadTasks(task tasks[], int& numTasks);
void addTask(task from, task to[], int& num);
void saveTasks(task newTask[], const int num);
void listTasks(task tasks[], const int num);
void processCommand(const int com, task tasks[], int& num);
void inputTask(task tasks[], int& num);
void deleteTask(task currentTasks[], const int whatTask, int& num);
int askCommand();
void pause();


//Main Function
int main() {
	task myTasks[100];
	int numTasks = 0;
	int command;
	loadTasks(myTasks, numTasks);
	do {
		command = askCommand();
		processCommand(command, myTasks, numTasks);
	}while(command != 4);
	saveTasks(myTasks, numTasks);
	pause();
}


//Reads in a string
void getString(char input[], int num) {
	cin.get(input, num, '\n');
	while(!cin) {
		cin.clear();
		cin.ignore(100, '\n');
		cout << "Error!!  Please enter something else: ";
		cin.get(input, num, '\n');
	}
	cin.ignore(100, '\n');
}


//Loads tasks from file into program
void loadTasks(task tasks[], int& numTasks) {
	ifstream inFile("Saved Tasks.txt");
	if(!inFile) {
		cout << "File failed to open!!\n";
		pause();
		exit(1);
	}
	char aClass[100];
	char assignment[100];
	char date[10];
	task aTask;
	while(!inFile.eof()) {
		inFile.get(aClass, 100, ';');
		inFile.ignore(100, ';');
		inFile.get(assignment, 100, ';');
		inFile.ignore(100, ';');
		inFile.get(date, 10, '\n');
		inFile.ignore(100, '\n');
		strcpy(aTask.myClass, aClass);
		strcpy(aTask.assign, assignment);
		strcpy(aTask.due, date);
		addTask(aTask, tasks, numTasks);
	}
	inFile.close();
}

//Adds task to the list
void addTask(task from, task to[], int& num) {
	strcpy(to[num].myClass, from.myClass);
	strcpy(to[num].assign, from.assign);
	strcpy(to[num].due, from.due);
	num++;
}

void inputTask(task tasks[], int& num) {
	char newClass[100], newAssign[100], newDueDate[100];
	task newTask;
	cout << endl;
	cout << "New Task Form\n";
	cout << "--- ---- ----\n\n";
	cout << "Class: ";
	getString(newClass, 100);
	cout << "Assignment Description: ";
	getString(newAssign, 100);
	cout << "Due Date (e.g. 9/25/12): ";
	getString(newDueDate, 100);
	strcpy(newTask.myClass, newClass);
	strcpy(newTask.assign, newAssign);
	strcpy(newTask.due, newDueDate);
	addTask(newTask, tasks, num);
	cout << "Task added Successfully!\n";
}

//Stops the program
void pause() {
	char end;
	cout << "Enter anything to continue....... ";
	cin >> end;
}

//Saves all of the tasks at the end of the program
void saveTasks(task newTask[], const int num) {
	ofstream outFile("Saved Tasks.txt");
	if(!outFile) {
		cout << "File failed to open!!\n";
		pause();
		exit(1);
	}
	for(int x = 0; x < num; x++) {
		outFile << newTask[x].myClass << ';' << newTask[x].assign << ';' << newTask[x].due << endl; 
	}
	outFile.close();
}


//Deletes a Task
void deleteTask(task currentTasks[], const int whatTask, int& num) {
	for(int x = whatTask; x < num; x++) {
		strcpy(currentTasks[x - 1].myClass, currentTasks[x].myClass);
		strcpy(currentTasks[x - 1].assign, currentTasks[x].assign);
		strcpy(currentTasks[x - 1].due, currentTasks[x].due);
	}
	num--;
	cout << "Task number " << whatTask << " deleted successfully\n";
}

//Lists all tasks on screen
void listTasks(task tasks[], const int num) {
	if(strcmp(tasks[0].myClass, "") == 0 || num < 0) {
		cout << "You do not have any tasks!\n";
	}else {
		cout << setfill(' ');
		cout << "   " << "|Class|" << setw(15) << "|Assignment|" << setw(15) << "|Due Date|\n\n";
		for(int x = 0; x < num; x++) {
			cout << x + 1 << ". " << "|" << tasks[x].myClass << "|" << " " << setw(15) << "|" << tasks[x].assign << "|" << " " << setw(15) << "|" << tasks[x].due << "|" << endl;
		}
	}
	cout << endl;
	pause();
}

//Asks for the user to input a command
int askCommand() {
	int com;
	cout << endl;
	cout << "Menu\n";
	cout << "----\n";
	cout << endl << endl;
	cout << "1 " << setfill ('-') << setw(24) << " See list of Tasks\n";
	cout << "2 " << setfill('-') << setw(19) << " Add new Task\n";
	cout << "3 " << setfill('-') << setw(20) << " Delete a Task\n";
	cout << "4 " << setfill('-') << setw(20) << " Save and Quit\n";
	cout << endl;
	cout << "Choice: ";
	cin >> com;
	cout << endl;
	while(com < 1 || com > 4) {
		cin.ignore(100, '\n');
		cout << "Please enter either 1, 2, 3, or 4: ";
		cin >> com;
		cout << endl;
	}
	while(!cin) {
		cin.clear();
		cin.ignore(100, '\n');
		cout << "Invalid number... please try again: ";
		cin >> com;
		cout << endl;
		while(com < 1 || com > 4) {
			cin.ignore(100, '\n');
			cout << "Please enter either 1, 2, 3, or 4: ";
			cin >> com;
			cout << endl;
		}
	}
	cin.ignore(100, '\n');
	return com;
}

//Processes the users command and decides what to do with it
void processCommand(const int com, task tasks[], int& num) {
	int taskNum;
	switch(com) {
	case 1:
		listTasks(tasks, num);
		break;
	case 2:
		inputTask(tasks, num);
		break;
	case 3:
		cout << "What is the number of the task you want to delete?: ";
		cin >> taskNum;
		deleteTask(tasks, taskNum, num);
		break;
	case 4:
		cout << "Bye Bye!\n";
		break;
	}
}


I am using Visual C++ 2010 and every time I try to debug the program it always comes up with an error...

Unhandled exception at 0x76ee15de in Assignment Calender.exe: 0xC0000005: Access violation writing location 0x003a0032.


and points at one of the strcpy() functions here...

1
2
3
4
5
6
void addTask(task from, task to[], int& num) {
	strcpy(to[num].myClass, from.myClass);
	strcpy(to[num].assign, from.assign);
	strcpy(to[num].due, from.due);
	num++;
}


I've looked for a solution to this problem, but haven't found one yet!! Please help.
1. task myTasks[100]; This value is limited to 100. There is no way of handling the situation of exceeding this quantity in void addTask().

2. void addTask() will always increment int numTasks, even if "Saved Tasks.txt" is empty.

3. Because of the above void saveTasks() will always add empty task ;; to the "Saved Tasks.txt"

4. Now I am not sure, but while(!inFile.eof()) in void loadTasks is executed infinitely. I guess it is because there is no eofbit bit set, but failbit, meaning no operations inside this loop can end successfully, but the loop cannot be broken. This may be due to the fact, that inFile.get(aClass, 100, ';'); encountered delim symbol ; before it was able to extract even one character, due to #3.

5. Due to infinite loop, addTask(aTask, tasks, numTasks); is executed over task myTasks[100]; capacity

To fix your crash change while(!inFile.eof()) to while(inFile.good()) in void loadTasks
To fix adding extra task, add if(inFile.good()) before addTask(aTask, tasks, numTasks); in line 75.

Also, please change your pause() so that it acceps ENTER as valid key press, and not require to input any other characters.

And open file like this:
1
2
3
4
5
6
7
ifstream inFile;
inFile.open("Saved Tasks.txt", ios::app | ios::in | ios::out);
if(!inFile.is_open()) {
	cout << "File failed to open!!\n";
	pause();
	exit(1);
}

so that it is created, if not already exisiting in the system.
It seems to work for me with these changes, but make sure to clear "Saved Tasks.txt" before testing.

Pretty neat code, btw. Good luck.
Last edited on
Topic archived. No new replies allowed.