Program compiles but crashes

Im updating a previous lab assignment in which i have to use classes. I pretty much have everything coded and working but when i try to add a task it crashes on me something about not having access possibly.


heres the code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//----------------------------------------------------------------------
//this is the header file for labThree

#ifndef ADD_H
#define ADD_H

class TaskList
{
public:
	 void readTasksFromFile();
	 void writeTasksToFile(char courseName[25], char courseDescription[200], char dueDate[10]);
	 void showChoices(int);
	 void choiceThree();
	 void setChoice(int);
	 int  getChoice();

private:
	int  choice;
};
#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
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
// implementation file follows

//implementation file
#include "functions.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int TaskList::getChoice()
{
	return choice;
}

void TaskList::setChoice(int choices)
{
	choice = choices;
}

//function to show choices
void TaskList::showChoices(int choice)
{
	TaskList Task;

	cout << "Welcome to your task organizer!\n";
	cout << "*****************************************************************************\n\n";
	cout << "		1 Enter your task or assignment.\n";
	cout << "		2 Display all of the tasks that are in the file.\n";
	cout <<	"		3 Find a task by Course.\n";
	cout << "		4 Quit\n\n";
	cout << "Please Enter a Number(1,2,3,4): ";
	
	do
	{	
	cin  >> choice;	
	
	//error control
	while(!cin)
	{
		cin.clear();
		cin.ignore(100, '\n');

		cout << endl << "You've entered an illegal integer, please try again!\n\n";
		cout <<			"Please Enter a Number(1,2,3,4): ";
		cin  >> choice;
	}
	cin.ignore(100, '\n');
	
	
	
	//switch that controls which choice
	switch(choice)
	{
	case 1:
		char* courseName[25];
		char* courseDescription[200]; 
		char* dueDate[10];
		Task.writeTasksToFile(courseName[25],courseDescription[200],dueDate[10]);
		break;
	case 2:		
		Task.readTasksFromFile();
		cout << "\n\n";
		break;
	case 3:		
		Task.choiceThree();
		break;
	case 4:
		cout << "Goodbye :)";
		break;
	}
	}
	while(choice !=4);
	
}

//-------------------------------------------------------------------------------------------------------
void TaskList::readTasksFromFile()
{
	string getData;
    ifstream openFile;
    openFile.open("tasks.txt");

	    //open tasks.txt
	openFile.open("tasks.txt");
    if(openFile.is_open())
    {
        //print everything to end of file 
		while(!openFile.eof())
        {
			getline(openFile, getData);
			cout << getData << endl;
        }
    }

	//close tasks.txt
	openFile.close();
}

//-------------------------------------------------------------------------------------------------------
void TaskList::writeTasksToFile(char courseName[25], char courseDescription[200], char dueDate[10])
{
	char correctInfo;
	int n = 0, y = 1;
	ofstream outData;
	
	//gather information
	cout << "*****************************************************************************\n";
	cout << "\n\nPlease enter the name of your course: ";
	cin.getline(courseName, 25, '\n');
	cout << "Please enter the description of your assignment: ";
	cin.getline(courseDescription, 200, '\n');
	cout << "Please enter the Due Date(9/26/2012): ";
	cin.getline(dueDate, 10, '\n');
	
	cout << "\n" << courseName << "\n" 
		 << courseDescription << "\n"
		 << dueDate << "\n";

	cout << "\nWould you like to store this information(y/n)? ";
	cin  >> correctInfo;
	
	if(correctInfo = y)
	{
		//open tasks.txt
		outData.open("tasks.txt", ios::app);
		
		//print data to file tasks.txt
		outData << courseName << "\n" << courseDescription;
		outData << "\n" << dueDate << "\n\n";
		outData.close();

		cout << "\n";
	}
}

//-------------------------------------------------------------------------------------------------------
void TaskList::choiceThree()
{
	string searchStr;
	string line;
	int lineNumber = 0;
	
	cout << "\nPlease enter the name of the task you want to find: ";
	cin  >> searchStr;
	cout << "\n";
	
	ifstream file("tasks.txt");

    while(getline(file, line))
    {
		if(line.find(searchStr) != string::npos)
		for(lineNumber = 0; lineNumber <= 2; lineNumber++)
		{			
			cout << line << '\n';
			if(lineNumber!=3)
				getline(file, line);
		}
    }

	cout << "\n";
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//main.cpp

//trying to do my assignment using classes


#include <iostream>
#include "functions.h"
using namespace std;


int main()
{
	TaskList Task;
	Task.showChoices(4);

	return 0;
}


so everything compiles and it seems to work but like i said when i enter a task i get an error. anyone know whats up?
Last edited on
What line is it crashing on?
it crashes in the program when i try to enter an assignment name for option one. Im thinking the problem lies in the switch function in functions.cpp
I meant what line number in the code.
line 57 in functions.cpp or wait actually its line 108 in functions.cpp
Last edited on
I probably should have asked this as well in the beginning, but could you format the code with [code][/code] tags so line numbers show on the side? Then just make a note of which code is which file.
there you go. thanks for that. so ya looks like from this it crashes when it gets to line 110
On line 59, you are passing a single character from your various arrays to the write function. If you want to pass an array, just use the name by itself. The problem here is that the one character you are passing is one beyond the element of the array, which is probably causing a crashing later on when you try to use it.

EDIT: Also, on line 123, you probably want to be doing comparison (==) rather than assignment (=) inside the if statement's condition. Also note that that will check if correctInfo is equal to the character with the ASCII value of 1, which is a non-printable character (not the character 'y' as you seem to have been thinking). To get the value of the character 'y' simply write [code]'y'[code].
Last edited on
im still lost. i thought i was passing a character array by declaring
 
char* courseName[25]
That doesn't declare an array of characters; that declares an array of 25 pointers to characters. An array of 25 characters looks like this: char myArray[25];.

The other problem was when you were 'passing the array' to the function. Suppose I had a function that looked like this:
void func(char takesAnArrayAsAParam[]);
To call it this way is incorrect:
func(myArray[25]);
The reason is because myArray[25] tries to get a character one past the end of myArray as the characters in the array are numbered from 0 to 24.

If this is still confusing, you probably need to review how arrays work.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//switch that controls which choice
	switch(choice)
	{
	case 1:
		Task.writeTasksToFile(courseName[25],courseDescription[200],dueDate[10]);
		break;
	case 2:		
		Task.readTasksFromFile();
		cout << "\n\n";
		break;
	case 3:		
		Task.choiceThree();
		break;
	case 4:
		cout << "Goodbye :)";
		break;
	}
	}
	while(choice !=4);


ok so if i get rid of the the pointers, and just pass the array with its name only it says the name is undefined. then when i define it there as a real c-string it says somethng about char not being compatible with char*
Last edited on
Line 5 (as you have it posted) still has the error with passing arrays to functions I mentioned in my second paragraph above.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
switch(choice)
	{
	case 1:
		void writeTasksToFile(char courseName[25],char courseDescription[200],char dueDate[10]);
		break;
	case 2:		
		Task.readTasksFromFile();
		cout << "\n\n";
		break;
	case 3:		
		Task.choiceThree();
		break;
	case 4:
		cout << "Goodbye :)";
		break;
	}
	}
	while(choice !=4);


ok now it wont accept option one when i run the program...
You didn't fix the problem. In fact, you've replaced the call to the function with a function definition, which will do nothing when the program actually runs.

I think you may need to go over how functions work (defining/calling them) as well.
alright well thanks for the help anyways ill look over functions again. but i dont think im gonna get it. we'll c. wish me luck
Here is a better way to quit out of something:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bool Quit = false;

while(!Quit) {
    switch (choice) {
            case 1:
               //your code
             case 2:
               //your code
             case 3:
               //your code 
             case 4:  //user wants to quit
               Quit = true;
     }
}


Consider making things like courseName etc member variables, also can you use strings rather than pointer to char arrays.

HTH
Topic archived. No new replies allowed.