0

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();
            };
  • What command are you using to compile? Certain compiler flags will throw an error if you don't use arguments passed to a function. – bhzag Oct 25 '17 at 22:09
  • 1
    The error messages aren't as helpful as they could be, but the indentation of the code lines you added is a clue: you have semicolons that you need to remove. – 1201ProgramAlarm Oct 25 '17 at 22:10
  • Note your question title describes the warnings, not the errors. So which lines are 56 and 91? – aschepler Oct 25 '17 at 22:10
  • Unrelated: `cout << endl << "Total # of Students: " << count; << "Overall Class Average: " << overallAvg;` Got an extra semi colon in there. – user4581301 Oct 25 '17 at 22:11
  • Unrelated: Recommended reading: [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – user4581301 Oct 25 '17 at 22:13
  • 1
    You try to modify values which are not passed by reference so the modification will not be accessible in main – Gustavo Oct 25 '17 at 22:14

3 Answers3

2

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;
Gillespie
  • 4,319
  • 2
  • 24
  • 42
  • i have made those changes, but now it just crashes. here is the updated code: https://pastebin.com/60haZ7eV – Charles Carrington Oct 25 '17 at 22:36
  • @CharlesCarrington sounds like you have a new question. Take a few kicks at solving it, Gustavo 's comment under the question may help, and ask a new question if you get stuck. – user4581301 Oct 25 '17 at 22:55
1

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.

Barmar
  • 596,455
  • 48
  • 393
  • 495
  • i changed what you suggested, but now it just crashes. here is the updated code: https://pastebin.com/60haZ7eV – Charles Carrington Oct 25 '17 at 22:26
  • Why are you taking all those parameters that you never use? The function reads the values from the file, not from the parameters. – Barmar Oct 25 '17 at 22:41
  • do i not have to pass the values between the different functions? – Charles Carrington Oct 25 '17 at 22:42
  • You would if the different functions didn't read them from the file. But both functions get the variables from the file. I think you need to go back and study how function arguments are used. – Barmar Oct 25 '17 at 22:43
-1

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

veda
  • 5,488
  • 14
  • 53
  • 76