-1
我遇到了頭文件中的getGrade()函數無法根據用戶在主文件中輸入的標記返回字母等級的問題。當我編譯並運行該程序時,不會根據輸入的標記顯示字母等級。爲什麼mycourses [i] .getGrade()不返回任何東西(C++)?
頭文件(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;
}
};
主文件
#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();
}
你不叫'setGrade'那爲什麼呢? – kfsone
在getGrade之前,你必須使用'setGrade'。 – NonCreature0714
謝謝你們兩個......現在它顯示字母等級 – nma