Pointers to objects

Hello,

In this program I am to use pointers and dynamic arrays when creating objects. I can't use subscripts....my professor won't make it that easy.

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

using namespace std;

const int MAX_EXAMS = 4;

class Student
{
private:
	string StudentName;		// Student name.
	float Exam[MAX_EXAMS];	// Exam grades.
	float ExamAverage;		// Computed exam average.
	float MidtermGrade;		// Midterm grade.
	float FinalGrade;		// Final grade.
	float CourseGrade;		// Computed course grade.
	char LetterGrade;		// Assigned letter grade.

	void ComputeExamAverage()
	{
		// FUNCTION OBJECTIVE: Compute exam average after dropping
		// lowest exam grade.

		float MinimumExamGrade = Exam[0];
		float Sum = Exam[0];

		for (int i = 1; i < MAX_EXAMS; i++)
		{
			Sum += Exam[i];
			if (Exam[i] < MinimumExamGrade)
				MinimumExamGrade = Exam[i];
		}

		ExamAverage = (Sum - MinimumExamGrade) / (MAX_EXAMS - 1);
	}

	void ComputeLetterGrade()
	{
		// FUNCTION OBJECTIVE: Assign letter grade based on course grade.

		if (CourseGrade >= 90.0f)
			LetterGrade = 'A';
		else if (CourseGrade >= 80.0f)
			LetterGrade = 'B';
		else if (CourseGrade >= 70.0f)
			LetterGrade = 'C';
		else if (CourseGrade >= 60.0f)
			LetterGrade = 'D';
		else
			LetterGrade = 'F';
	}

	void ComputeNumberGrade()
	{
		// FUNCTION OBJECTIVE: Compute course grade.

		ComputeExamAverage();

		CourseGrade = 0.5f * ExamAverage + 0.3f * FinalGrade + 0.2f * MidtermGrade;
	}

public:
	Student()
	{
		// FUNCTION OBJECTIVE: Default constructor.  Set instance variables
		// to default values.

		StudentName = "";
		ExamAverage = MidtermGrade = FinalGrade = CourseGrade = 0.0f;
		for (int i = 0; i < MAX_EXAMS; i++)
			Exam[i] = 0.0f;
	}

	void InitiateStudentData(string n, float mt, float fin, float e[])
	{
		// FUNCTION OBJECTIVE: Set instance variables to initial values.

		StudentName = n;
		MidtermGrade = mt;
		FinalGrade = fin;

		for (int i = 0; i < MAX_EXAMS; i++)
			Exam[i] = e[i];
	}

	void ComputeCourseGrade()
	{
		// FUNCTION OBJECTIVE: Compute course number and letter grades.

		ComputeNumberGrade();

		ComputeLetterGrade();
	}

	void OutputResults()
	{
		// FUNCTION OBJECTIVE: Output data for a student.

		cout << "\n\nStudent name: " << StudentName;
		cout << "\nNumber grade:   " << CourseGrade;
		cout << "\nLetter grade:   " << LetterGrade;
	}
};

void GetStudentData(string&,float&,float&,float[]);

int main()
{
	Student* sPtr;

	int NoOfStudents;
	cout << "How many students are in the course?  ";
	cin >> NoOfStudents;
	sPtr = new Student[NoOfStudents];

	string Sname;
	float MTgrade;
	float Fgrade;
	float Egrades[MAX_EXAMS];
	for(int i=0;i<NoOfStudents;i++)
	{
		GetStudentData(Sname,MTgrade,Fgrade,Egrades);
		*(sPtr+i).InitiateStudentData(Sname,MTgrade,Fgrade,Egrades); // error C2228: left of '.InitiateStudentData' must have class/struct/union 
		*(sPtr+i).ComputeCourseGrade(); // error C2228: left of '.ComputeCourseGrade' must have class/struct/union 
	}

	system("cls");

	for(int i=0;i<NoOfStudents;i++)
		*(sPtr+i).OutputResults(); // error C2228: left of '.OutputResults' must have class/struct/union 

	delete [] sPtr;

	return 0;
}

void GetStudentData(string &n, float &mt, float &f, float e[])
{
	cout << "Enter student name:   ";
	getline(cin, n);
	cout << "Enter midterm grade:  ";
	cin >> mt;
	cout << "Enter final grade:    ";
	cin >> f;
	for(int i=0;i<MAX_EXAMS;i++)
	{
		cout << "Enter Exam " << i+1 << " grade:  ";
		cin >> e[i];
	}
}


I've included the error codes on the lines that caused them. Could someone please help me with this. I'm assuming the problem is, as usual, some small detail that I'm overlooking.

Thank you,

cobraun85
Hey man, I noticed that ur if statements don't have the { and }.

this is what you typed:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void ComputeLetterGrade()
	{
		// FUNCTION OBJECTIVE: Assign letter grade based on course grade.

		if (CourseGrade >= 90.0f)
			LetterGrade = 'A';
		else if (CourseGrade >= 80.0f)
			LetterGrade = 'B';
		else if (CourseGrade >= 70.0f)
			LetterGrade = 'C';
		else if (CourseGrade >= 60.0f)
			LetterGrade = 'D';
		else
			LetterGrade = 'F';
	}


but try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void ComputeLetterGrade()
	{
		// FUNCTION OBJECTIVE: Assign letter grade based on course grade.

		if (CourseGrade >= 90.0f)
			{LetterGrade = 'A';}
		else if (CourseGrade >= 80.0f)
			{LetterGrade = 'B';}
		else if (CourseGrade >= 70.0f)
			{LetterGrade = 'C';}
		else if (CourseGrade >= 60.0f)
			{LetterGrade = 'D';}
		else {
			LetterGrade = 'F';}
	}


Hope it'll work for ya
In c++, the '.' (member selection) operator has higher precedence than the '*' (dereference) operator, so you are trying to dereference (sPtr+i).InitiateStudentData(Sname,MTgrade,Fgrade,Egrades); instead of just
(sPtr+i)

What you have would be equivellant to:
*((sPtr+i).InitiateStudentData(Sname,MTgrade,Fgrade,Egrades));
To fix it, you can use one of these:
1
2
3
(*(sPtr+i)).InitiateStudentData(Sname,MTgrade,Fgrade,Egrades);
//or
(sPtr+i)->InitiateStudentData(Sname,MTgrade,Fgrade,Egrades);

Last edited on
There is problem with the OPERANTS PRIORITY.

There is no problem with the IF-ELSE statements mate.You have to write (sPtr+i)->initiateStudentData(...)
instead of *(sPtr+i).initiateStudentData(...) OR ( *(Sptr+i) ).iniitiateStudentData(...)
Once you do it,you will have no problem with compiling the code cause I tried it and I had a successful compilation.As for the program algorithm it is up to you and you teacher.
Last edited on
Topic archived. No new replies allowed.