Help with displaying my array?

So I am in a C++ class for school and am doing a project where I must create 2 classes CStudent and CCourse where CStudent includes an array of 5 CCourses being the classes the student takes. I need to overload the insertion operator to display all of the information in CStudent, including all of the classes, except when the data in the array position is the default data for CCourse... I realize I am probably not explaining this very well, but I created a function to display courses when the course in the array does not have the default data, but I cannot get it to be called in the overloaded insertion operator... I can't figure out what to do. Any help would be great?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void CStudent::displayClasses()
{
	CCourse blank;
	int x = 0;
	while (x < 5 && COURSES[x].getid() != blank.getid())
	{
		cout << COURSES[x] << endl;
		x += 1;
	}
}

  ostream& operator << (ostream& outs, const CStudent& c)
{
	outs << "[" << c.ID << "] " << c.FNAME << " " << c.MINIT << " " << c.LNAME << "\nGender: " << c.GENDER << "\nGPA: " << c.GPA << "\nCurrently enrolled units: " << c.UNITS << "\nCurrently enrolled classes:\n";
	displayCourses();
	return outs;
}


I use getid for courses because the assignment requires that the id is a private data member.

I get this error

Error 1 error C2352: 'CStudent::displayClasses' : illegal call of non-static member function c:location 155 1 Project 4
Last edited on
Line 15 should be c.displayCourses(); the operator << is a friend not part of the class.
Then I get this error.

Error 1 error C2662: 'void CStudent::displayClasses(void)' : cannot convert 'this' pointer from 'const CStudent' to 'CStudent &' c:location 155 1 Project 4
It would help to see your class and where you are calling operator <<. Maybe even enough code to compile myself.
http://www.parashift.com/c++-faq/const-correctness.html
A `display()' function should not modify the object, so you must tell the compiler that.
void CStudent::displayClasses() const
This is all of my code. Not looking for a cleanup or anything, just why I can't get this one thing to work (I am also finding that the total enrolled units is only displaying as 0 no matter what).

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
//Name
//Project 4
//Student.h

#include <string>
#include <iostream>
#include "Course.h"
#pragma once
using namespace std;

class CStudent
{
public:
	CStudent();
	CStudent(string id, string fName, string lName, char mInit, char gender, double gpa);
	string FNAME, LNAME;
	char MINIT, GENDER;
	CCourse COURSES[5];
	int UNITS;
	void replaceClass();
	void addCourse(CCourse add);
	void dropCourse();
	void dropCourse(string id);
	void dropCourse(CCourse drop);
	string getId();
	double getGpa();
	void setId(string id);
	void setGpa(double gpa);
	void displayClasses();
	friend istream& operator >>(istream& ins, CStudent& c);
	friend ostream& operator <<(ostream& outs, const CStudent& c);
	~CStudent();
private:
	string ID;
	double GPA;
};


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
//Student.cpp

#include "Student.h"


CStudent::CStudent()
{
	ID = "0000";
	FNAME = "First";
	LNAME = "Last";
	MINIT = 'M';
	GENDER = 'U';
	UNITS = 0;
	GPA = 0.0;
}

CStudent::CStudent(string id, string fName, string lName, char mInit, char gender, double gpa)
{
	ID = id;
	FNAME = fName;
	LNAME = lName;
	MINIT = mInit;
	GENDER = gender;
	UNITS = COURSES[0].units + COURSES[1].units + COURSES[2].units + COURSES[3].units + COURSES[4].units;
	GPA = gpa;
}


CStudent::~CStudent()
{
}

string CStudent::getId()
{
	return ID;
}

double CStudent::getGpa()
{
	return GPA;
}

void CStudent::setId(string x)
{
	ID = x;
}

void CStudent::setGpa(double z)
{
	GPA = z;
}

void CStudent::addCourse(CCourse add)
{
	CCourse newCourse;
	if (COURSES[0].getid() == newCourse.getid())
	{
		COURSES[0] = add;
	}
	else
	{
		if (COURSES[1].getid() == newCourse.getid())
		{
			COURSES[1] = add;
		}
		else
		{
			if (COURSES[2].getid() == newCourse.getid())
			{
				COURSES[2] = add;
			}
			else
			{
				if (COURSES[3].getid() == newCourse.getid())
				{
					COURSES[3] = add;
				}
				else
				{
					if (COURSES[4].getid() == newCourse.getid())
					{
						COURSES[4] = add;
					}
					else
					{
						cout << "You cannot have more than 5 classes";
					}
				}
			}
		}
	}
}

void CStudent::dropCourse(string id)
{
	int x = 0;
	CCourse blank;
	while (x < 5 && COURSES[x].getid() != id)
	{
		x += 1;
	}
	if (x < 5)
	{
		COURSES[x] = blank;
		replaceClass();
	}
	else
	{
		cout << "The class is not enrolled.\n";
	}
}

void CStudent::dropCourse()
{
	string id;
	cout << "Please enter a Course ID\n";
	cin >> id;
	dropCourse(id);
	replaceClass();
}

void CStudent::dropCourse(CCourse drop)
{
	dropCourse(drop.getid());
}

void CStudent::displayClasses()
{
	CCourse blank;
	int x = 0;
	while (x < 5 && COURSES[x].getid() != blank.getid())
	{
		cout << COURSES[x] << endl;
		x += 1;
	}
}

istream& operator >> (istream& ins, CStudent& c)
{
	cout << "\nFirst name: ";
	ins >> c.FNAME;
	cout << "\nLast name: ";
	ins >> c.LNAME;
	cout << "\nMiddle initial: ";
	ins >> c.MINIT;
	cout << "\nGender: ";
	ins >> c.GENDER;
	cout << "\nGPA: ";
	ins >> c.GPA;

	return ins;
}

ostream& operator << (ostream& outs, const CStudent& c)
{
	outs << "[" << c.ID << "] " << c.FNAME << " " << c.MINIT << " " << c.LNAME << "\nGender: " << c.GENDER << "\nGPA: " << c.GPA << "\nCurrently enrolled units: " << c.UNITS << "\nCurrently enrolled course(s):\n";
	c.displayClasses();
	return outs;
}

void CStudent::replaceClass()
{
	CCourse newCourse, lastCourse;
	if (COURSES[4].getid() != newCourse.getid())
	{
		lastCourse = COURSES[4];
		COURSES[4] = newCourse;
		CStudent::addCourse(lastCourse);
	}
	else
	{
		if (COURSES[3].getid() != newCourse.getid())
		{
			lastCourse = COURSES[3];
			COURSES[3] = newCourse;
			CStudent::addCourse(lastCourse);
		}
		else
		{
			if (COURSES[2].getid() != newCourse.getid())
			{
				lastCourse = COURSES[2];
				COURSES[2] = newCourse;
				CStudent::addCourse(lastCourse);
			}
			else
			{
				if (COURSES[1].getid() != newCourse.getid())
				{
					lastCourse = COURSES[1];
					COURSES[1] = newCourse;
					CStudent::addCourse(lastCourse);
				}
				else
				{
					if (COURSES[0].getid() != newCourse.getid())
					{
						lastCourse = COURSES[0];
						COURSES[0] = newCourse;
						CStudent::addCourse(lastCourse);
					}
					else
					{
						return;
					}
				}
			}
		}
	}
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//Course.h

#pragma once
#include <string>
using namespace std;

class CCourse
{
public:
	CCourse();
	~CCourse();
	string title;
	int units;
	string getid();
	void setId(string id);
	CCourse(string id, string title, int units);
	friend istream& operator >>(istream& ins, CCourse& c);
	friend ostream& operator <<(ostream& outs, const CCourse& c);
private:
	string id;
};


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
//Course.cpp

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


CCourse::CCourse()
{
	id = "C0000";
	title = "Unknown";
	units = 0;
}

CCourse::CCourse(string Id, string Title, int Units)
{
	id = Id;
	title = Title;
	units = Units;
}

CCourse::~CCourse()
{
}

string CCourse::getid()
{
	return id;
}

void CCourse::setId(string s)
{
	id = s;
}

istream& operator >> (istream& ins, CCourse& c)
{
	cout << "\nCourse ID: ";
	ins >> c.id;
	cout << "\nCourse Title: ";
	ins >> c.title;
	cout << "\nCourse Units: ";
	ins >> c.units;

	return ins;
}

ostream& operator << (ostream& outs, const CCourse& c)
{
	outs << "(" << c.id << ")\t[" << c.title << "]\t" << c.units << endl;

	return outs;
}
> I am also finding that the total enrolled units is only displaying as 0 no matter what
¿how did you see that if your code does not compile?
also, there isn't a main function.


fyi you may write else if
My teacher provided me with a main function that does work when the code we have written works properly. I simply turned that line into a comment to test the rest of my code. Everything works properly except for that one line and the total enrolled units displays as 0.

Here is the main function.

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
//Source.cpp

#include <iostream>

#include "Course.h"

#include "Student.h"

using namespace std;

void main()

{

	CCourse defaultCourse;

	cout << "Information of the default course: \n" << defaultCourse;

	CCourse math101("MA101", "Arithemetic Math", 3);

	cout << "Information of the course Math101: \n" << math101;

	CCourse csci123("C0123", "Introduction To Programming", 4);

	cout << "Current ID of the course Computer Science 123: \n" << csci123.getid() << endl;

	csci123.setId("CS123");

	csci123.title = "Introduction To Programming with C++";

	cout << "Updated information of the course Computer Science 123: \n" << csci123 << endl;

	CStudent defaultStudent;

	cout << "Information of the default student: \n" << defaultStudent;

	CStudent firstStudent("S0000", "Toyota", "Camry", 'S', 'M', 3.5);

	cout << "\nInformation of the first student: \n" << firstStudent;

	cout << "\nChange the ID of the first student to S0001. \n";

	firstStudent.setId("S0001");

	cout << "Update the GPA of the first student to 3.65. \n";

	firstStudent.setGpa(3.65);

	cout << "\nThe new ID of the first student is: " << firstStudent.getId() << endl;

	cout << "\nThe new GPA of the first student is: " << firstStudent.getGpa() << endl;

	cout << "\nEnroll Math 101 for the first student.";

	firstStudent.addCourse(math101);

	cout << "\nEnroll CSCI123 for the first student.";

	firstStudent.addCourse(csci123);

	cout << "\nUpdated information of the first student: \n" << firstStudent;

	cout << "\nDrop Math 101 for the first student.";

	firstStudent.dropCourse(math101);

	cout << "\nLast information of the first student: \n" << firstStudent;



	system("Pause");

}
Last edited on
> "\nCurrently enrolled units: " << c.UNITS
¿where do you ever modify `UNITS'?


PS: tell your teacher that main must return int.
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
//Student.cpp

#include "Student.h"


CStudent::CStudent()
{
	ID = "0000";
	FNAME = "First";
	LNAME = "Last";
	MINIT = 'M';
	GENDER = 'U';
	UNITS = 0;
	GPA = 0.0;
}

CStudent::CStudent(string id, string fName, string lName, char mInit, char gender, double gpa)
{
	ID = id;
	FNAME = fName;
	LNAME = lName;
	MINIT = mInit;
	GENDER = gender;
	UNITS = COURSES[0].units + COURSES[1].units + COURSES[2].units + COURSES[3].units + COURSES[4].units;
	GPA = gpa;
}


I'm obviously modifying Units improperly there. And we have been using void main for all of our main functions since our second assignment, so that isn't the problem. void mains work fine. That way we dont have to return 0 at the end. I also still need to find a way to get it to display the classes..
I got the output working properly except for the Units now! If anyone could help tell me what I did wrong with that that would be great.
Just because it compiles doesn't mean you should use it. Void main is non-standard. The cpp standard is int main(void) or int main(int argc, char **argv).
Thank you for the input but that is not what I require assistance with. The sloppiness of my code should show I'm not going for perfection, just for it to do the job. This is only my first programming class, I am a beginner, I imagine I will get better as I go along. Right now, all I need is help with getting the units to display properly.
> I'm obviously modifying Units improperly there
of course, at that point you haven't enrolled to anything yet.
you may update it when you `addCourse()' and `dropCourse()'


> I also still need to find a way to get it to display the classes..
Again, const-correctness.
Or a quick and dirty hack ostream& operator << (ostream& outs, CStudent c)
Thank you for that ne555, that's what I couldn't think of for some reason.

And ya I fixed the const thing earlier, I just didn't post the fixed code lol.
Topic archived. No new replies allowed.