I've only written one simple class before, yesterday. I am required in this assignment to use all of these functions, even though I don't think they are all necessary. I believe that my problem is just a matter of not knowing exactly how to pass one variable, which is created within a function, to another function.

I get an error message that says that class_percent does not fall within the scope of the function grade_set(). I get why it doesn't--it's new to the function. I tried to pass it within calculations (), but that is apparently illegal.

My question is essentially, can a variable be passed as I am trying to do it--i.e. passed from a function to a new function where it has not been originally defined?

Or, should I just have one variable (ex: temp) that first holds the value of class_percent, then gets passed to grade_set() where it holds what is now the value final_grade, and then gets passed to output?

I'd like to get better at passing variables though...the syntax for doing so eludes me when things get a bit complicated.

Any help would be very much appreciated.

#include <iostream>

using namespace std;

class All_Grades
{
public:
    void output_grades(double final_grade);
    void take_input();
    void calculations();
    void grade_set(double& class_percent);
private:
    double quiz_1, quiz_2, midterm, final_exam;
};

int main()
{
	All_Grades student;
    double class_percent, final_grade;

    student.take_input();
    student.calculations();
    student.grade_set(class_percent);
    student.output_grades(final_grade);

    return 0;
}

void All_Grades::take_input()
{
	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 quiz one, " <<endl;
	cout << "which has a maximum value of 10, is not valid." <<endl;

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

void All_Grades::calculations()
{
	double quiz_percent, midterm_percent, final_percent, class_percent;

	    quiz_percent = ((quiz_1 + quiz_2) / 20) * 25;
	    midterm_percent = (midterm / 100) * 25;
	    final_percent = (final_exam / 100) * 50;
	    class_percent = quiz_percent + midterm_percent + final_percent;
	    grade_set(class_percent);//need to pass class percent to grade_set()
}

void All_Grades::grade_set(class_percent)//needs to take class_percent
{

	if ((class_percent >= 90) && (class_percent <= 100))
	    {
			final_grade = 'A';
	    }
	    else if((class_percent >= 80) && (class_percent <= 89.99))
	    {
	    	final_grade = 'B';
	    }
	    else if ((class_percent >= 70) && (class_percent <= 79.99))
	    {
	    	final_grade = 'C';
	    }
	    else if ((class_percent >= 60) && (class_percent <= 69.99))
	    {
	    	final_grade = 'D';
	    }
	    else if ((class_percent >= 1) && (class_percent <= 59.99))
	    {
	    	final_grade = 'F';
	    }
		student.output_grades(final_grade);//needs to take final_grade and pass
		//to output_grades
}

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

Recommended Answers

All 2 Replies

In the function definition: void All_Grades::grade_set(class_percent)//needs to take class_percent what type is class_percent? Don't you need to specify?

In the function definition: void All_Grades::grade_set(class_percent)//needs to take class_percent what type is class_percent? Don't you need to specify?

You absolutely do. I got the program to work. You are right. I just had to specify the type. :ooh: It is posted below as a future example.

#include <iostream>

using namespace std;

class All_Grades
{
public:
    void output_grades(double final_grade);
    void take_input();
    void calculations();
    void grade_set(double class_percent);
private:
    double quiz_1, quiz_2, midterm, final_exam;
};

int main()
{
	All_Grades student;
    double class_percent, final_grade;

    student.take_input();
    student.calculations();
    student.grade_set(class_percent);
    student.output_grades(final_grade);

    return 0;
}

void All_Grades::take_input()
{
	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 quiz one, " <<endl;
	cout << "which has a maximum value of 10, is not valid." <<endl;

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

void All_Grades::calculations()
{
	double quiz_percent, midterm_percent, final_percent, class_percent;

	    quiz_percent = ((quiz_1 + quiz_2) / 20) * 25;
	    midterm_percent = (midterm / 100) * 25;
	    final_percent = (final_exam / 100) * 50;
	    class_percent = quiz_percent + midterm_percent + final_percent;
	    grade_set(class_percent);//need to pass class percent to grade_set()
}

void All_Grades::grade_set(double class_percent)//needs to take class_percent
{
	char final_grade;
	if ((class_percent >= 90) && (class_percent <= 100))
	    {
			final_grade = 'A';
	    }
	    else if((class_percent >= 80) && (class_percent <= 89.99))
	    {
	    	final_grade = 'B';
	    }
	    else if ((class_percent >= 70) && (class_percent <= 79.99))
	    {
	    	final_grade = 'C';
	    }
	    else if ((class_percent >= 60) && (class_percent <= 69.99))
	    {
	    	final_grade = 'D';
	    }
	    else if ((class_percent >= 1) && (class_percent <= 59.99))
	    {
	    	final_grade = 'F';
	    }
		output_grades(final_grade);//needs to take final_grade and pass
		//to output_grades
}

void All_Grades::output_grades(double final_grade)
{
	cout << "The final grade for this student is an " << final_grade;
}
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.