我在做家庭作业时遇到了麻烦。
除了letter_grade
零件外,一切都按预期工作。我没有收到任何编译错误,但每次运行程序时,letter_grade
都是-858993460。我知道这可能是我忽略的一些简单的事情,但是我碰壁了,几乎没有想法。
#include <iostream>
#include <string>
using namespace std;
// Class Declaration
class StudentRecord
{
string id;
int A, B, C, D, F, grade;
float avg, exam1, exam2;
public:
void input();
void output();
void average();
void letter_grade();
};
// Gathering data
void StudentRecord::input()
{
cout << " Please enter your student ID: ";
cin >> id;
cout << "\n";
cout << " Enter your score for exam 1: ";
cin >> exam1;
cout << "\n";
cout << " Enter your score for exam 2: ";
cin >> exam2;
}
// Calculations
void StudentRecord::average()
{
avg = (exam1 + exam2) / 2;
}
void StudentRecord::letter_grade()
{
if ((avg >= 90) && (avg <= 100))
grade = A;
else if ((avg >= 80) && (avg < 90))
grade = B;
else if ((avg >= 70) && (avg < 80))
grade = C;
else if ((avg >= 60) && (avg < 70))
grade = D;
else (avg < 60);
grade = F;
}
// Output Data
void StudentRecord::output()
{
cout << "\n\n";
cout << " *** Student Record ***" << endl;
cout << "\n";
cout << " Student ID: " << id << endl;
cout << "\n";
cout << " Grade for exam 1: " << exam1 << endl;
cout << "\n";
cout << " Grade for exam 2: " << exam2 << endl;
cout << "\n";
cout << " Average for the class: " << avg << endl;
cout << "\n";
cout << " Letter grade for the class: " << grade << endl;
cout << "\n\n";
}
int main()
{
StudentRecord student;
student.input();
student.average();
student.letter_grade();
student.output();
system ("pause");
return 0;
}