GradeBook程序没有显示每个学生的字母等级

时间:2018-04-03 21:00:07

标签: c++ oop debugging

我正在做一个我们创建了Student类和Course类的作业。该程序将创建一个由7名学生组成的数组,然后使用CourseObj为7名学生调用每种方法,例如:

    `courseObj.evaluate_class();`  
evaluate_class方法中的

将是一个方法,它将调用学生类中的一些方法,例如

for(int i=0; i<7; i++)
    {
        student[i].compute_student_stats();
        student[i].determine_student_grade();
    }
    return;

这只是为了让你了解程序

using namespace std;
ofstream toFile;
class Student
{
     private:
        string name;
        int project[6];
        int exam[2];
        int quiz[5];
        double project_avg;
        double exam_avg;
        double quiz_avg;
        int finalExam;
        double average;
        string grade;
     public: 
        Student();
        void get_student_scores();
        void compute_student_stats();
        void determine_student_grade();
        void display_student_name_gpa_grade();
        double student_average();
};

class Course
{
    private:
        Student student[7];
        int index_of_highest;
        int index_of_lowest;
    public:
        Course();
        ~Course();
        void get_grades();
        void evaluate_class();
        void determine_index_of_highest_lowest();
        void display_highest();
        void display_lowest();
        void display_stats();
};

Student::Student()
{
    name = " ";

    for(int i = 0; i<6; i++)
        project[i] = 0;

    for(int i = 0; i<2; i++)
        exam[i] = 0;

    for(int i = 0; i<5; i++)
        quiz[i] = 0;

    average, project_avg, exam_avg, quiz_avg = 0.0;
    finalExam = 0;
    grade = " ";
}

void Student::get_student_scores()
{
    //Test try catch heavily
    try
    {
        cout << "\n Enter Student Name: \n";
        cin>>name;

        for(int i = 0; i<6; i++)
        {
            cout<<"Enter Student project grade #"<<i+1<<":\n";
            cin>>project[i];
        }

        for(int i = 0; i<2; i++)
        {
            cout<<"Enter Student exam grade #"<<i+1<<":\n";
            cin>>exam[i];
        }

        for(int i = 0; i<5; i++)
        {
            cout<<"Enter Student quiz grade #"<<i+1<<":\n";
            cin>>quiz[i];
        }

        cout << "Enter final exam grade: \n";
        cin>>finalExam;    
    }

    catch(...)
    {
        cin.clear(); 
        cin.ignore(numeric_limits<streamsize>::max(), '\n');

        cout<<"Error, invalid input entered. \n";
    }
    return;
}

void Student::compute_student_stats()
{
    cout<<fixed<<showpoint<<setprecision(2);

    for(int i = 0; i<6; i++)
        project_avg += project[i]; 
    project_avg = project_avg/6;
    cout << "Project Average = " << project_avg << "\n";

    for(int i = 0; i<2; i++)
        exam_avg += exam[i]; 
    exam_avg = exam_avg/2;
    cout << "Exam average = " << exam_avg << "\n";

    for(int i = 0; i<5; i++)
        quiz_avg += quiz[i]; 
    quiz_avg = quiz_avg/5;
    cout << "Quiz average = " << quiz_avg << "\n";

    average = (project_avg*0.25) + (exam_avg*0.4) + (quiz_avg*0.1) + (finalExam*0.25);
    cout << "Semester average = " << average << "\n"; 

    return;
}

void Student::determine_student_grade()
{
    if(average <= 100 && average >= 93)
    {
        grade = "A";
        cout << "Letter Grade = " << grade << "\n";
    }

    else if(average <= 92 && average >= 89)
    {
        grade = "A-";
        cout << "Letter Grade = " << grade << "\n";
    }
    else if(average <= 88 && average >= 87)
    {
        grade = "B+";
        cout << "Letter Grade = " << grade << "\n";
    }
    else if(average <= 86 && average >= 83)
    {
        grade = "B";
        cout << "Letter Grade = " << grade << "\n";
    }
    else if(average <= 82 && average >= 79)
    {
        grade = "B-";
        cout << "Letter Grade = " << grade << "\n";
    }
    else if(average <= 78 && average >= 77)
    {
        grade = "C+";
        cout << "Letter Grade = " << grade << "\n";
    }
    else if(average <= 76 && average >= 73)
    {
        grade = "C";
        cout << "Letter Grade = " << grade << "\n";
    }
    else if(average <= 72 && average >= 69)
    {
        grade = "C-";
        cout << "Letter Grade = " << grade << "\n";
    }
    else if(average <= 68 && average >= 67)
    {
        grade = "D+";
        cout << "Letter Grade = " << grade << "\n";
    }
    else if(average <= 66 && average >= 63)
    {
        grade = "D";
        cout << "Letter Grade = " << grade << "\n";
    }
    else if(average <= 62 && average >= 57)
    {
        grade = "D-";
        cout << "Letter Grade = " << grade << "\n";
    }
    else if(average <= 57)
    {
        grade = "F";
        cout << "Letter Grade = " << grade << "\n";
    }
    return;
}

void Student::display_student_name_gpa_grade()
{
    const int width1 = 19;
    const int width2 = 17;

    cout<<fixed<<showpoint<<setprecision(2);
    cout << left << setw(width1) << "Student = " << name;
    cout << right << setw(width2) << "Average = " << average;
    cout << right << setw(width2) << "Letter Grade = " << grade << "\n";

    toFile.open("student.txt",ios::app);
    toFile<<fixed<<showpoint<<setprecision(2);
    toFile << left << setw(width1) << "Student = " << name;
    toFile << right << setw(width2) << "Average = " << average;
    toFile << right << setw(width2) << "Letter Grade = " << grade << "\n";
    toFile.close();
    return;
}

double Student::student_average()
{
    //remeber to round to nearest tenth in main
    return average;
}



Course::Course()
{
    index_of_highest = 0;
    index_of_highest = 0;
}
Course::~Course()
{
    cout << "Course Object is going out of existence.";
}
void Course::get_grades()
{
    for(int i=0; i<7; i++)
        student[i].get_student_scores();    
    return;
}

void Course::evaluate_class()
{
    for(int i=0; i<7; i++)
    {
        student[i].compute_student_stats();
        student[i].determine_student_grade();
    }
    return;
}

void Course::determine_index_of_highest_lowest()
{
    int tempHighest = 0; 
    int tempLowest = 100;

    for(int i=0; i<7; i++)
    {
        if(student[i].student_average() > tempHighest)
        {
            tempHighest = student[i].student_average();
            index_of_highest = i;
        }
    }

    for(int i=0; i<7; i++)
    {
        if(student[i].student_average() < tempLowest)
        {
            tempLowest = student[i].student_average();
            index_of_lowest = i;
        }
    }
    return;
}

void Course::display_highest()
{
    student[index_of_highest].display_student_name_gpa_grade();
        return;
}

void Course::display_lowest()
{    
    student[index_of_lowest].display_student_name_gpa_grade();
        return;
}

void Course::display_stats()
{    
    for (int i=0; i < 7; i++)
    {
        student[i].display_student_name_gpa_grade();
    }
}

void describeProgram()
{
    cout<<"This program will serve as a grade book for seven students who are"
          " participating in a course, \n and will also analyze the performances"
          "of the students as well. Each student will have six projects (worth 25%"
          " collectively), \n two midterm exams (worth 20% each), and five quizzes (worth 10%"
          " collectively), and a final exam (worth 25%). \n The program will calculate"
          " the class average and will also display the student with the highest"
          " and lowest grade.";  
}

int main(int argc, char** argv) 
{
    Course courseObj;

    describeProgram();
    courseObj.get_grades();
    courseObj.evaluate_class();
    courseObj.determine_index_of_highest_lowest();

    toFile.open("student.txt",ios::out);

    toFile<<fixed<<showpoint<<setprecision(2)<<endl;
    cout<<fixed<<showpoint<<setprecision(2)<<endl;  

    courseObj.display_stats();
    cout << "\n";
    toFile << "\n";

    cout<<"The name, average, and grade of the best student in the class is:\n";
    toFile<<"The name, average, and grade of the best student in the class is:\n";  
    courseObj.display_highest();

    cout<<"The name, average, and grade of the poorest student in the class is:\n";
    toFile<<"The name, average, and grade of the poorest student in the class is:\n";
    courseObj.display_lowest();
    toFile.close();
    system("PAUSE");

    return 0;
}

我目前的问题是,有时,程序不会显示已经计算过平均水平的学生的字母等级。我似乎无法弄清楚为什么程序有时候会把我们的字母评分,有时候也不会。

Heredisplay_student_name_gpa_grade()输出的图片,当我将所有内容硬编码为相同时 Here,你可以看到max和kevin是唯一没有分配字母等级的两个学生。相信我,这也不是投入的问题。我发现字母等级不会显示的唯一情况是你错误点击并输入一个非常大的数字(大于100)。除此之外,我似乎找不到为什么它有时不输出letterGrade的原因。

2 个答案:

答案 0 :(得分:0)

问题是用于计算平均值的变量未初始化。

而不是

double project_avg;

使用:

double project_avg{};

后者会将值初始化为零。

答案 1 :(得分:0)

该行: average,project_avg,exam_avg,quiz_avg = 0.0; 只分配quiz_avg而不分配任何其他变量。请改用a = b = c = 0.0表格。

此外,Student :: determine_student_grade不处理平均值> gt的情况。 100(你可能认为应该永远不会发生,但确实如此)。考虑使用断言。