I am having problem with the getGrade() function in header file not returning letter grade based on marks entered by the user in the main file. When I compile and run the program doesn't display the letter grade according to the marks entered.
Header file (course.h)
#include <iostream>
#include <string>
using namespace std;
class Course {
private:
int totalMarks;
char grade;
public:
void marksInfo(int tm)
{
totalMarks = tm;
}
int getMarks(void)
{
return totalMarks;
}
void setGrade(char c)
{
if(totalMarks <= 39)
c = 'E';
if(totalMarks >= 40 && totalMarks <= 49)
c = 'D';
if(totalMarks >= 50 && totalMarks <= 64)
c = 'C'
}
char getGrade(void)
{
return grade;
}
};
Main file
#include <iostream>
#include <string>
#include "course.h"
using namespace std;
int main()
{
int tm;
Course course[5];
for (int i = 0; i < 5; i++)
{
cout << "Subject #" << i+1 << endl;
cout << "Total Marks #" << i+1 << ": ";
cin >> tm;
course[i].marksInfo(tm);
cout << endl;
course[i].getGrade();
}
cout << "Grade: " << course[0].getGrade();
}
Your code never sets grade
to anything. You also have a problem-in-waiting:
void setGrade(char c)
{
if(totalMarks <= 39)
c = 'E';
if(totalMarks >= 40 && totalMarks <= 49)
c = 'D';
if(totalMarks >= 50 && totalMarks <= 64)
c = 'C'
}
this function never changes grade
, what it does is it populates a local variable called 'C' with a value based on totalMarks and then immediately forgets it. I think what you wanted was more something like this:
class Course {
private:
int totalMarks;
char grade;
public:
void marksInfo(int tm)
{
totalMarks = tm;
if(totalMarks <= 39)
grade = 'E';
else if(totalMarks >= 40 && totalMarks <= 49)
grade = 'D';
else if(totalMarks >= 50 && totalMarks <= 64)
grade = 'C';
else if(totalMarks >= 65 && totalMarks <= 84)
grade = 'B';
else
grade = 'A';
}
int getMarks(void)
{
return totalMarks;
}
char getGrade(void)
{
return grade;
}
};