Issue with showing data from a class

Hey guys, When I run my program, all seems to go well, until I try reach my ShowData function. The specific line I have issues with is line 172(tasktrack.cpp). The console will crash when it reaches line 172, I have tried commenting out the line, and that gives me the following garbled output: http://i.imgur.com/8hPILTC.png

Here is the code for the function I where the issue originates:

1
2
3
4
5
6
7
8
9
10
11
12
  void TaskTrack::showData(TaskTrack taskList[], int size){
	

	char itemDesc[MAXCHAR];
	for (int i = 0; i < size; i++){
		courseDesc(taskList[i].course, itemDesc);
		cout << setw(15) << left << taskList[i].taskName
			 << setw(10) << left <<  itemDesc
			 << setw(5)  <<  taskList[i].dueDate << endl << endl;
	}
	
}


Here is the code for the function call that causes a crash:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void TaskTrack::courseDesc(CourseName tempCourse, char itemDesc[]){

	switch(tempCourse){	
	case 0:
		itemDesc = "CS161";
		strcpy(itemDesc, "CS161");
		break;
	case 1:
		itemDesc = "CS161";
		strcpy(itemDesc, "MATH251");
		break;
	case 2:
		itemDesc = "CS161";
		strcpy(itemDesc, "COMM111");
		break;
	case 3:
		itemDesc = "CS161";
		strcpy(itemDesc, "WR212");
		break;
	default:
		itemDesc = "CS161";
		strcpy(itemDesc, "ILLEGAL");
	}
}


Thanks ahead of time guys, I will keep trying to work through the problem on my own, I will post back here if I can find a solution.
Last edited on
itemDesc = "CS161";
This will set itemDesc to point to the array "CS161". You are not allowed to modify this array. Many compilers will warn you because you use a pointer to non-const char to point to const data.

strcpy(itemDesc, "CS161");
This will now try to write to the array that you were not allowed to modify so that's why it crashes.
Last edited on
Topic archived. No new replies allowed.