Problem with computing grades program

I have to create two methods called computeGrade. One of the computeGrade method takes in a double argument and converts the marks into grade (A, B, C, D, F) and set the value to grade attribute. Another computeGrade method takes in a string argument which is either “Pass” or “Fail” for a non-graded pass student and converts the result into grade (P or F) and set the value to grade attribute. The method that i'm having problem with is the string argument.

This are the codes i did so far.

Student.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <string>
#include <sstream>

using namespace std;


class Student {

private:

	string name;
	string subject;
	double grade;
	
public:
	Student(string name, string subject, double grade);
	void getGrade();
	void computeGrade(double marks);
	void computeGrade(string marks);
	string toString();

}


student.cpp
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
#include "Student.h"
#include <string>
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

Student::Student(string name, string subject, double grade)
{
	this->name = name;
	this->subject = subject;
	this->grade = grade;
	
}

void Student::getGrade(){

}
void Student::computeGrade(double marks){ 
	if (marks >= 80)
	{
		cout << "Grade: A";
	}
	else if (marks >= 70)
	{
		cout << "Grade: B";
	}
	else if (marks >= 60)
	{
		cout << "Grade: C";
	}
	else if (marks >= 50)
	{
		cout << "Grade: D";
	}
	else if (marks < 50)
	{
		cout << "Grade: F";
	}
}

void Student::computeGrade(string marks){ // the part i'm having problems with.
	if (marks == pass){
		cout << "Grade : P";
	}
	else if (marks == fail)
	{
		cout << "Grade: F";
	}
}
string Student::toString(){
	stringstream info;
	info << "Name:" << name << "Subject:" << subject << "Grade" << grade;
}

int main(){
	vector <Student*> myStudent;
	myStudent.push_back(new Student("Clara Ng","Basic Accounting",79));
	myStudent.push_back(new Student("Kate Sun", "Basic Accounting", pass));
	myStudent.push_back(new Student("Ray Wong", "French Language", 65.5));
	myStudent.push_back(new Student("Nadirah", "Creative Writing", 85));
	myStudent.push_back(new Student("Fabian Ho", "Basic Accounting", Fail));
	myStudent.push_back(new Student("Tan Kexin", "Creative Writing", 74.5));
}

return 0;
}
Last edited on
What does it do that you don't like, or what does it not do that wish it did do?

myStudent.push_back(new Student("Kate Sun", "Basic Accounting", pass));
This looks wrong. The third parameter in the Student constructor function is a double, but you're trying to use the variable pass, which doesn't exist. What is pass? What are you trying to do? Likewise the one where you try to use the non-existent variable Fail. In C++, variables must exist before you can use them.

Likewise on lines 43 and 46, where you try to use the variables pass and fail. Those variables don't exist. Step back and think about what you're trying to do here. That function makes little sense. Surely it's meant to take a numerical score in (not a string), and check if that numerical score is above a certain value.


I see you're using a vector of pointers-to-student. Why? Why not just use a vector<Student>?
Last edited on
Topic archived. No new replies allowed.