CIS115 GradeBook应用程序不添加等级

时间:2014-01-25 21:06:43

标签: c++ class object math

我是编码的新手,所以它可能很简单。我将发布我的代码的所有部分,但我已经对其中的大部分内容进行了评论,以解决问题。

分配是创建一个成绩簿应用程序,我遇到的问题是addLabGrade函数实际上没有添加到总lab_grade_total,或者我认为不是。我想其他事情可能会发生。这有点奇怪的是,它适用于测验等级总计,我将实验室成绩中的内容复制并粘贴到测验成绩部分并更改了名称。

我知道这是一个糟糕的编码,但是我们有几个要求来展示将数据传递给成员函数,返回数据等的知识。我们还给出了几乎所有的函数名称和参数,以及数据成员。和他们的类型。这些都不会改变。它编译并运行没有错误,但实验室平均值总是为0。

提前感谢您查看;我感谢任何帮助或指导。 “代码”的最后一部分将代码的输入和输出粘贴,所以你可以看到我的意思。

标题:

// Filename: GradeBook.h

class GradeBook
{
    public:
        GradeBook();
        void addLabGrade(double grade);
        void addQuizGrade(double grade);
        double getFinalSemesterGrade();
        double getAvgLabGrade() const;
        double getAvgQuizGrade() const;
    private:
        double lab_grade_total;
        double quiz_grade_total;
        int lab_grade_cnt;
        int quiz_grade_cnt;
        double lab_avg;
        double quiz_avg;
        double final_semester_grade;
        char letter_grade;
};

对象:

// Filename: GradeBook.cpp

#include "GradeBook.h" // includes the objects header

// create a default constructor
GradeBook::GradeBook()
{
    // empty becase there's nothing to do yet
}

void GradeBook::addLabGrade(double grade)
{
    lab_grade_total = lab_grade_total + grade;
    lab_grade_cnt = lab_grade_cnt + 1;
    lab_avg = lab_grade_total / lab_grade_cnt;
}

void GradeBook::addQuizGrade(double grade)
{
    quiz_grade_total = quiz_grade_total + grade;
    quiz_grade_cnt = quiz_grade_cnt + 1;
    quiz_avg = quiz_grade_total / quiz_grade_cnt;
}

double GradeBook::getAvgLabGrade() const
{
    return lab_avg;
}

double GradeBook::getAvgQuizGrade() const
{
    return quiz_avg;
}

主要cpp代码:

// filename : Lab1B.cpp

#include <iostream> 
#include <iomanip> 
#include "GradeBook.h" 

using namespace std;

int main()
{
    double grade = 0.0;
    int userGradeIntiger = 0;

    GradeBook gradeBook;
    cout << fixed << setprecision(2);

    cout << endl << "Please enter six lab grades:" << endl << endl;
    userGradeIntiger = 0;                   
    userGradeIntiger = userGradeIntiger + 1;         
    cout << "Enter Grade " << userGradeIntiger << ": ";
    cin >> grade;           
    gradeBook.addLabGrade(grade);  

    userGradeIntiger = userGradeIntiger + 1;            
    cout << "Enter Grade " << userGradeIntiger << ": ";
    cin >> grade;           
    gradeBook.addLabGrade(grade); 


    cout << endl << "Please enter seven quiz grades:" << endl << endl;
    userGradeIntiger = 0;                   
    userGradeIntiger = userGradeIntiger + 1;         
    cout << "Enter Grade " << userGradeIntiger << ": ";
    cin >> grade;           
    gradeBook.addQuizGrade(grade);  

    cout << endl << endl << "STUDENT GRADE CALCULATION:" << endl
        << "Average Lab Grade: " << gradeBook.getAvgLabGrade() << endl
        << "Average Quiz Grade: " << gradeBook.getAvgQuizGrade() << endl;

    return 0;
}

运行时会发生什么:

Please enter six lab grades:

Enter Grade 1: 100
Enter Grade 2: 100

Please enter seven quiz grades:

Enter Grade 1: 100


STUDENT GRADE CALCULATION:
Average Lab Grade: 0.00
Average Quiz Grade: 100.00

--------------------------------
Process exited with return value 0
Press any key to continue . . .

那里,都被剪掉了!

1 个答案:

答案 0 :(得分:1)

首先,当我编译并运行并输入100时,它会给我随机值,例如-10000428.00,因为你没有在构造函数中初始化变量。所以我这样做了,现在当我输入100时,它说一切都是零,除了最后一个学期的成绩,即百分之五十,实验室成绩是100%。

我不确定是什么让我的输出与你的输出不同,但尝试在构造函数中初始化所有变量。您的平均值应该从100.00开始(我们不希望等级受到影响,因为尚未分配任何内容)。添加成绩后,100.00将被覆盖。你的字母等级可以是任何东西,但我说“我”表示还没有添加成绩:

GradeBook::GradeBook()
{
    lab_grade_total = 0.0;
    quiz_grade_total = 0.0;
    lab_grade_cnt = 0;
    quiz_grade_cnt = 0;
    lab_avg = 100.0;
    quiz_avg = 100.0;
    midterm_exam_grade = 0.0;
    final_exam_grade = 0.0;
    ec_grade = 0.0;
    final_semester_grade = 0.0;
    letter_grade = 'I';
}

其次,你有一个问题:

cout << endl << endl << "STUDENT GRADE CALCULATION:" << endl
    << "Average Lab Grade: " << gradeBook.getAvgLabGrade() << endl
    << "Average Quiz Grade: " << gradeBook.getAvgQuizGrade() << endl
    << "Midterm Exam Grade: " << gradeBook.getMidtermExamGrade() << endl
    << "Final Exam Grade : " << gradeBook.getFinalExamGrade() << endl
    << "Extra Credit Grade: " << gradeBook.getExtraCreditGrade() << endl
    << "Final Semester Grade: " << gradeBook.getFinalSemesterGrade() << endl
    << "Final Letter Grade: " << gradeBook.getFinalLetterGrade() << endl;

你知道,因为你的功能如getAvgLabGrade()加倍,计算值,这很糟糕。因为你在一行中调用它,在巨型流输出或任何它上面调用它们,它们不会按你想要的顺序调用。你可以做两件事来解决这个问题:让你的getters NOT 加倍计算。这将是解决问题的正确方法。

或者,如果你想要一个便宜又简单的出路,只需将它们分开:

cout << endl << endl << "STUDENT GRADE CALCULATION:" << endl;
cout << "Average Lab Grade: " << gradeBook.getAvgLabGrade() << endl;
cout << "Average Quiz Grade: " << gradeBook.getAvgQuizGrade() << endl;
cout << "Midterm Exam Grade: " << gradeBook.getMidtermExamGrade() << endl;
cout << "Final Exam Grade : " << gradeBook.getFinalExamGrade() << endl;
cout << "Extra Credit Grade: " << gradeBook.getExtraCreditGrade() << endl;
cout << "Final Semester Grade: " << gradeBook.getFinalSemesterGrade() << endl;
cout << "Final Letter Grade: " << gradeBook.getFinalLetterGrade() << endl;

执行所有这些更改会使您的示例输入正常工作。