Problem storing values to an array

So I'm struggling here. I'm having problems with my member function "readCourseArray". I have dynamically allocated an array to hold students information about a number of courses that a student is taking. It needs to store course name, number, credit hours, and grade. The thing is the variables belong to another class and I cannot figure out how to make it work. Any help would be appreciated. Thanks!

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
 #include <iostream>
#include <string>

using namespace std;

class Student
{
	friend class Course;
private:
	string firstName,
		   lastName,
		   A_Number;

	static int numCourses;
	double gpa;
	Course *courses;

public:
	Student();
	void readStudent();
	void readCourseArray();



};

class Course
{
	friend class Student;
private:
	 int courseNumber,
		 creditHours; 
	string courseName;
	char letterGrade;
};


int main()
{
	Student stu;
	stu.readStudent();
	stu.readCourseArray();
	
	return 0;
}

Student::Student()
{
	firstName = " ",
	lastName = " ",
	A_Number = " ",
	numCourses = 0,
	gpa = 0.0,
	courses = NULL;
}

void Student::readStudent()
{
	cout << "Enter student A-number: ";
	cin >> A_Number;
	cout << "Enter student first name: ";
	cin >> firstName; 
	cout << "Enter student last name: ";
	cin >> lastName;
	cout << "Enter student number of courses: ";
	cin >> numCourses;

}

 void Student::readCourseArray()
{	// Dynamically allocated array for course information  // Where I am Stuck!!!!
	courses = new Course[numCourses];
	
	for (int i = 0; i < numCourses; i++)
	{
		cout << "Enter class " << i + 1 << " number: ";
		cin >> courseNumber.courses[i];
		cout << "Enter class " << i + 1 << " name: ";
		cin >> courseName.courses[i];
		cout << "Enter class " << i + 1 << " hours: ";
		cin >> creditHours.courses[i];
		cout << "Enter class " << i + 1 << " grade: ";
		cin >> letterGrade.courses[i];
	}
}
Change the following:
1
2
3
4
5
	class Course; // Note forward declaration required
class Student
{
	friend class Course; // Course does not need to know anything about Student
...

and this:
1
2
3
4
5
6
7
8
9
 void Student::readCourseArray()
{	// Dynamically allocated array for course information  // Where I am Stuck!!!!
	courses = new Course[numCourses];
	
	for (int i = 0; i < numCourses; i++)
	{
		cout << "Enter class " << i + 1 << " number: ";
		cin >> courseNumber.courses[i].courseNumber; // Note the order of the member access
...


Instead of making Student a friend of Course I suggest that you provide public read function for Course which readCourseArray() calls in the loop.
Thanks for your help!
Instead of making Student a friend of Course I suggest that you provide public read function for Course which readCourseArray() calls in the loop

I'm not really following this part though(care to explain further?). This transition from procedural to OOP is really kicking my A$$.
Well I think I figured it out. It is working so I assume it is correct.
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
 #include <iostream>
#include <string>
using namespace std;
class Course
{
public:
	// Public read functions
	void readCourseNum(int n) {courseNumber = n;}
	void readCreditHours(int c) { creditHours - c; }
	void readCourseName(string n) { courseName = n; }
	void readLetterGrade(char g) { letterGrade = g; }
protected:
	int courseNumber, creditHours;
	string courseName;
	char letterGrade;
};
class Student : protected Course
{
private:
	string firstName, lastName, A_Number;
	int numCourses;
	double gpa;
	Course *courses;
public:
	Student();
	void readStudent();
	void readCourseArray();
	void setFirstName(string n) { firstName = n; }
	void setLastName(string l) { lastName = l; }
	void setA_Number(string a) { A_Number = a; }
	void setNumCourses(int num) { numCourses = num; }
};
Student::Student()
{
	firstName = " ";
	lastName = " ";
	A_Number = " ";
	numCourses = 0;
	gpa = 0.0;
	courses = NULL;
}
void Student::readStudent()
{
	cout << "Enter student A-number: ";
	cin >> A_Number;
	setA_Number(A_Number);
	cout << "Enter student first name: ";
	cin >> firstName;
	setFirstName(firstName);
	cout << "Enter student last name: ";
	cin >> lastName;
	setLastName(lastName);
	cout << "Enter student number of courses: ";
	cin >> numCourses;
	setNumCourses(numCourses);
}
void Student::readCourseArray()
{ // Dynamically allocated array for course information
	courses = new Course[numCourses];
	int num,
		 cHours;
	string cName;
	char grade;
	for (int i = 0; i < numCourses; i++)
	{
		cout << "Enter class " << i + 1 << " number: ";
		cin >> num;
		courses[i].readCourseNum(num);      // call to public read function
		cout << "Enter class " << i + 1 << " name: ";
		cin >> cName;
		courses[i].readCourseName(cName);
		cout << "Enter class " << i + 1 << " hours: ";
		cin >> cHours;
		courses[i].readCreditHours(cHours);
		cout << "Enter class " << i + 1 << " grade: ";
		cin >> grade;
		courses[i].readLetterGrade(grade);
	}
}
int main()
{
	Student stu;
	stu.readStudent();
	stu.readCourseArray();
	return 0;
}
I thought of something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void Course::readCourse(int n)
{
		cout << "Enter class " << n + 1 << " number: ";
		cin >> courseNumber;
		cout << "Enter class " << n + 1 << " name: ";
		cin >> courseName;
		cout << "Enter class " << n + 1 << " hours: ";
		cin >> creditHours;
		cout << "Enter class " << n + 1 << " grade: ";
		cin >> letterGrade;
}

...

void Student::readCourseArray()
{ // Dynamically allocated array for course information
	courses = new Course[numCourses];

	for (int i = 0; i < numCourses; i++)
	{
		courses[i].readCourse(i);      // call to public read function
	}
}
Topic archived. No new replies allowed.