Charles Carrington
Charles Carrington

Reputation: 111

Parameter not being used in function C++

I am making a program that will read from a file and output the data both on screen and in a file. I had it working prior to adding an Overall Average and Total Count, but now it just crashes with no errors in the compliling process.

I have searched similar threads, but am unable to find anything that helps me fix these errors. Any assistance is appreciated.

Edit: updated teh code with some of the changes suggested. Now just crashing w/ no errors.

                          #include <iostream>
            #include <string>
            #include <limits>
            #include <cmath>
            #include <iomanip>
            #include <fstream>

            using namespace std;

            void gradeCalc(int studentID, int count, double midterm, double finalExam, double researchPaper, double groupProject, double participation, double studentAvg, double& overallAvg, double gradeSum);
            void printGrade(int studentID, int count, double midterm, double finalExam, double researchPaper, double groupProject, double participation, double studentAvg, double& overallAvg, double gradeSum);

            int main()
            {
                int studentID;
                int count = 0;

                double midterm;
                double finalExam;
                double researchPaper;
                double groupProject;
                double participation;
                double studentAvg;
                double overallAvg;
                double gradeSum;

                gradeCalc(studentID, count, midterm, finalExam, researchPaper, groupProject, participation, studentAvg, overallAvg, gradeSum);
                printGrade(studentID, count, midterm, finalExam, researchPaper, groupProject, participation, studentAvg, overallAvg, gradeSum);

                return 0;
            }

            void printGrade(int studentID, int count, double midterm, double finalExam, double researchPaper, double groupProject, double participation, double studentAvg, double& overallAvg, double gradeSum)
            {

                ifstream infile;
                ofstream outfile;

                infile.open("grades.dat");
                outfile.open("grades.txt");
                outfile << "STUDENT GRADING REPORT: ";
                outfile << fixed << showpoint << setprecision(2);

                while (!infile.eof()) {

                    infile >> studentID >> midterm >> finalExam >> researchPaper >> groupProject >> participation;

                    if (studentID > 1 && studentID <= 999) {

                        outfile << endl
                                << "Student ID: " << studentID << ' ' << "Midterm: " << midterm << ' ' << "Final Exam: " << finalExam << ' ' << "Research Paper: " << researchPaper << ' ' << "Group Project: " << groupProject
                                << "Participation: " << participation << ' ' << "Student Average: " << studentAvg;
                    }
                    overallAvg = gradeSum / count;
                    outfile << endl
                            << "Total # of Students: " << count
                            << "Overall Class Average: " << overallAvg;
                }

                infile.close();
                outfile.close();
            };

            void gradeCalc(int studentID, int count, double midterm, double finalExam, double researchPaper, double groupProject, double participation, double studentAvg, double& overallAvg, double gradeSum)
            {

                //midterm = .25
                //finalExam = .25
                //researchPaper = .20
                //groupProject = .20
                //participation = .10

                ifstream infile;
                ofstream outfile;

                infile.open("grades.dat");

                while (!infile.eof()) {

                    infile >> studentID >> midterm >> finalExam >> researchPaper >> groupProject >> participation;
                    studentAvg = ((midterm * .25) + (finalExam * .25) + (researchPaper * .20) + (groupProject * .20) + (participation * .10));
                    overallAvg = gradeSum / count;

                    if (studentID > 1 && studentID <= 999) {
                        count++;
                        gradeSum += studentAvg;

                        cout << endl
                             << fixed << showpoint << setprecision(2) << "Student ID: " << studentID << ' ' << "Average:  " << studentAvg;
                    }
                    cout << endl
                         << "Total # of Students: " << count
                         << "Overall Class Average: " << overallAvg;
                }

                infile.close();
            };

Upvotes: 0

Views: 288

Answers (3)

Barmar
Barmar

Reputation: 780723

printGrade takes overallAvg as an argument. But it never uses the argument, instead it reassigns the variable with:

overallAvg = gradeSum / count;

gradeCalc() has the same problem. In fact, these functions seem to do all the same calculations.

You're also calling the functions with uninitialized variables.

If overallAvg is supposed to be an output argument, you need to make it a reference parameter.

void printGrade(int studentID, int count, double midterm, double finalExam, double researchPaper, double groupProject, double participation, double studentAvg, double &overallAvg, double gradeSum)

The expression error is due to these lines:

                cout << endl
                     << "Total # of Students: " << count;
                << "Overall Class Average: " << overallAvg;

The semicolon at the end of the second line is ending the cout, so the third line is starting with an invalid operator. Get rid of that semicolon.

                cout << endl
                     << "Total # of Students: " << count
                     << "Overall Class Average: " << overallAvg;

This might also fix the warning about the unused variable, since you're using the variable in this output statement.

Upvotes: 1

veda
veda

Reputation: 6584

It depends on the compiler. Compile with -Wno-unused-parameter flag. It should do the trick.

Upvotes: -1

Gillespie
Gillespie

Reputation: 6561

You have some syntax issues you need to resolve:

printGrade():

    outfile << endl
            << "Total # of Students: " << count; //<-- bad semicolon
    << "Overall Class Average: " << overallAvg;

gradeCalc():

    cout << endl
            << "Total # of Students: " << count; //<-- bad semicolon
    << "Overall Class Average: " << overallAvg;

Upvotes: 2

Related Questions