I believe this to be a simple problem. My program runs fine, but I cannot figure why the variable "final_grades" is not being passed to the function and output. alternatively, I may be calling to the function incorrectly. This is the first class that I have written, so it may be that I haven't declared it correctly. The book that I'm using is usually great, but for this problem it's not helping me.

When I run the program and input values I simply don't get any output, but when I put the output statement in the main function body it works fine. I need to use a function in the class, however, as specified in this assignment.

Thank you in advance for your help.

#include <iostream>

using namespace std;

class All_Grades
{
public:
    void output_grades(double final_grade);
    double quiz_1, quiz_2, midterm, final_exam;
};

int main()
{
    All_Grades student;
    double quiz_percent, midterm_percent, final_percent, final_grade;

    cout << "Please input the grades for this student as you are prompted on the screen ";
    cout << ",making sure to push enter between each grade." <<endl;
    cout <<endl;
    cout << "Note: If the numbers entered do not fall within acceptable values,";
    cout << "the program will terminate. For example, entering 12 as a grade for quize one" <<endl;
    cout << "(which has a maximum value of 10, is not valid." <<endl;

    cout << "The grade for quiz 1: ";
    cin >> student.quiz_1;
    cout << "for quiz 2: ";
    cin >> student.quiz_2;
    cout << "for the midterm: ";
    cin >> student.midterm;
    cout << "for the final exam: ";
    cin >> student.final_exam;

    quiz_percent = ((student.quiz_1 + student.quiz_2) / 20) * 25;
    midterm_percent = (student.midterm / 100) * 25;
    final_percent = (student.final_exam / 100) * 50;
    final_grade = quiz_percent + midterm_percent + final_percent;
    student.output_grades();
    return 0;
}

void All_Grades::output_grades(double final_grade)
{
	cout << "The final grade for this student is " << final_grade;
}

What I can see is that your function definition and implementation require a parameter. However when you call it you dont pass a parameter.

This should do what you want?

student.output_grades(final_grade);

Another option would be to define a member variable final_grade. You then can define your functions without parameter and still can call the variable, as it is a member of the function.

On another note: be careful, normally you declare member variables as private and you can't access them from outside the class. You then use getter and setter functions (which are public) to change the private member variables.

/* grades.hpp */
class grades {
private:
   double grade;
   double mid_term_grade;

public:
   void set_grade(double grade_);
   double get_grade();
   void set_mid_term_grade(double mid_term_grade_);
   double get_mid_term_grade();
};


/* grades.cpp */
#include grades.hpp
void grades::set_grade(double grade_) {
   grade = grade_;
}

What I can see is that your function definition and implementation require a parameter. However when you call it you dont pass a parameter.

This should do what you want?

student.output_grades(final_grade);

Another option would be to define a member variable final_grade. You then can define your functions without parameter and still can call the variable, as it is a member of the function.

On another note: be careful, normally you declare member variables as private and you can't access them from outside the class. You then use getter and setter functions (which are public) to change the private member variables.

/* grades.hpp */
class grades {
private:
   double grade;
   double mid_term_grade;

public:
   void set_grade(double grade_);
   double get_grade();
   void set_mid_term_grade(double mid_term_grade_);
   double get_mid_term_grade();
};


/* grades.cpp */
#include grades.hpp
void grades::set_grade(double grade_) {
   grade = grade_;
}

Some days....I wonder how I can even put my pants on in the morning. I wont mention how long I stared at this and missed that I didn't even pass a parameter.

Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.