Compiles successfully but program crashes (sometimes)

Hi everyone.
I have a program that compiles successfully and runs fine for the most part.
The first thing the program does is declares a class object to invoke a constructor. The constructor opens a file and is supposed to read in data from the file.

When the file is empty, everything works fine, but when the file contains data, the program crashes as soon as I start it.

I have an if to check whether or not the file exists in the specified location, and that works fine. I also checked the format of my data, and I can't find anything wrong there.

At the end of the program, it writes data to the file and does that fine, but when I try to run it again when the file contains data it crashes. I'm stumped.

What could be causing this?

Thanks for your help.
closed account (3bfGNwbp)
Could you post the code? That would help us all in finding the solution to your problem.
Sorry it's so long. Thanks for your help.

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
#include <iostream>
using namespace std;
#include <fstream>
#include "myutil.h"
#include "tasklist.h"


void displayMenu();
char readInOption();
void performOption(char option, TaskList& list);

void readInTask(Task& aTask);
void readInCourseName(char courseName[], const char prompt[]);
void readInTaskName(char taskName[], const char prompt[]);
void readInDueDate(char dueDate[], const char prompt[]);

int main()
{
	char option;
	char fileName[] = "tasks.txt";
	TaskList list(fileName);

	cout << "Welcome to the assignment database.\n" << endl;

	displayMenu();
	option = readInOption();
	while(option != 'q')
	{
		performOption(option, list);
		displayMenu();
		option = readInOption();
	}
	
	return 0;
}


void performOption(char option, TaskList& list)
{	

	Task task;
	switch (option)
	{
	case 'a' : 
		readInTask(task);
		list.addTask(task);
		break;
	case 'd':
		list.displayAll();
		break;
	case 'f': cout << "search is invoked" << endl;
		break;
	default:
		cout << "Invalid entry. Please enter a, d, f, or q." << endl;
		break;
	}
}

void displayMenu()
{
	cout << "a. Add a task.\n"
		 << "d. Display all tasks.\n"
		 << "f. Find a task by course.\n"
		 << "q. Quit.\n";
}

char readInOption()
{
	char option;

	cout << "Please enter an option (a, d, f, q): ";
	cin >> option;
	cin.ignore(100, '\n');

	return tolower(option);
}

void readInTask(Task& aTask)
{
	char courseName[MAX_CHAR];
	char taskName[MAX_CHAR];
	char dueDate[MAX_CHAR];

	readInCourseName(courseName, "Please enter the course name: ");
	readInTaskName(taskName, "Please enter the task name: ");
	readInDueDate(dueDate, "Please enter the due date: ");

	aTask.setCourseName(courseName);
	aTask.setTaskName(taskName);
	aTask.setDueDate(dueDate);
	
}
void readInCourseName(char courseName[], const char prompt[])
{
	getString(prompt, courseName, MAX_CHAR);
}

void readInTaskName(char taskName[], const char prompt[])
{
	getString(prompt, taskName, MAX_CHAR);
}

void readInDueDate(char dueDate[], const char prompt[])
{
	getString(prompt, dueDate, MAX_CHAR);
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef TASK_H
#define TASK_H

const int MAX_CHAR = 101;
class Task
{
public:
	Task();
	
	void getCourseName(char outCN[]) const;
	void setCourseName(const char inCN[]);
	void getTaskName(char outTN[]) const;
	void setTaskName(const char inTN[]);
	void getDueDate(char outDD[]) const;
	void setDueDate(const char inDD[]);

	void print() const;
private:
	char courseName[MAX_CHAR];
	char taskName[MAX_CHAR];
	char dueDate[MAX_CHAR];
};

#endif 


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
#include "task.h"
#include <iostream>
using namespace std;

Task::Task()
{
	strcpy(courseName, "No course name.");
	strcpy(taskName, "No task name.");
	strcpy(dueDate, "No due date.");
}

void Task::getCourseName(char outCN[]) const
{
	strcpy(outCN, courseName);
}

void Task::setCourseName (const char inCN[])
{
	strcpy(courseName, inCN);
}

void Task::getTaskName(char outTN[]) const
{
	strcpy(outTN, taskName);
}

void Task::setTaskName (const char inTN[])
{
	strcpy(taskName, inTN);
}

void Task::getDueDate(char outDD[]) const
{
	strcpy(outDD, dueDate);
}

void Task::setDueDate(const char inDD[])
{
	strcpy(dueDate, inDD);
}

void Task::print() const
{
	cout << courseName << '\t'
		 << taskName << '\t'
		 << dueDate << endl;
}


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
#ifndef DB_H
#define DB_H

#include "task.h"

const int CAPACITY = 50;

class TaskList
{
public:
	TaskList();
	TaskList(const char fileName[]);
	~TaskList();

	void addTask(const Task& aTask);
	void displayAll() const;
	int search() const;

private:
	Task list[CAPACITY];

	int size;
	char fileName[MAX_CHAR];

	void loadTasks(const char fileName[]);
	void saveTasks(const char fileName[]) const;
};

#endif 


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
#include "tasklist.h"
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

TaskList::TaskList()
{
	size = 0;
	strcpy(fileName, "tasks.txt");
	loadTasks(fileName);
}

TaskList::TaskList(const char file[])
{
	size = 0;
	strcpy(fileName, file);
	loadTasks(fileName);
}

TaskList::~TaskList()
{
	saveTasks(fileName);
}

void TaskList::loadTasks(const char fileName[])
{
	ifstream in;
	in.open(fileName);
	if(!in)
	{
		cerr << "Failed to open " << fileName << " for input." << endl;
		exit(1);
	}

	char courseName[MAX_CHAR];
	char taskName[MAX_CHAR];
	char dueDate[MAX_CHAR];
	Task task;

	while(!in.eof())
	{
		//in.get();
		//in.ignore(100, '\n');
		in.get(courseName, MAX_CHAR, ';');
		in.get(taskName, MAX_CHAR, ';');
		in.get(dueDate, MAX_CHAR, '\n');

		task.setCourseName(courseName);
		task.setTaskName(taskName);
		task.setDueDate(dueDate);

		addTask(task);

		//in.get(courseName, MAX_CHAR, ';');
		//in.get(taskName, MAX_CHAR, ';');
		//in.get(dueDate, MAX_CHAR, '\n');
	}
	in.close();

}

void TaskList::saveTasks(const char fileName[]) const
{
	ofstream out;
	out.open(fileName);
	char courseName[MAX_CHAR];
	char taskName[MAX_CHAR];
	char dueDate[MAX_CHAR];

	if(!out)
	{
		cerr << "Failed to open " << fileName << " for output." << endl;
		exit(1);
	}
	int index;
	for(index=0; index<size; index++)
	{
		list[index].getCourseName(courseName);
		list[index].getTaskName(taskName);
		list[index].getDueDate(dueDate);
		
		out << courseName << ';'
			<< taskName << ';'
			<< dueDate << endl;
	}
	out.close();
}

void TaskList::addTask(const Task& aTask)
{
	list[size] = aTask;
	size++;
}

void TaskList::displayAll() const
{
	int index;
	char courseName[MAX_CHAR];
	char taskName[MAX_CHAR];
	char dueDate[MAX_CHAR];

	cout << endl << setw(20) << left << "Class"
		 << setw(30) << left << "Task"
		 << setw(20) << left << "Due date" << endl;

	for(index=0; index<size; index++)
	{
		list[index].getCourseName(courseName);
		list[index].getTaskName(taskName);
		list[index].getDueDate(dueDate);

		cout << setw(20) << left << courseName
			 << setw(30) << left << taskName
			 << setw(20) << left << dueDate << endl;
	}
	cout << endl;
}

int search()
{

}


1
2
3
4
5
6
7
8
9
#ifndef MYUTIL_H
#define MYUTIL_H

#include <iostream>
using namespace std;

void getString(const char prompt[], char str[], int capacity);

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;
#include "myutil.h"

void getString(const char prompt[], char str[], int capacity)
{
	cout << prompt;
	cin.get(str, capacity, '\n');
	while(!cin)
	{
		cin.clear();
		cin.ignore(100, '\n');
		cout << prompt;
		cin.get(str, capacity, '\n');
	}
	cin.ignore(100, '\n');
}
Last edited on
This is just begging to be run under a debugger. If the crash is caused by a segFault or the like, you'll find it in seconds. What OS are you using? Do you have a debugger?
I'm using Visual C++ 2010 Express on Windows 7.
Unfortunately I'm really new to programming and don't know much (anything) about debugging.
Topic archived. No new replies allowed.