#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <iomanip>
using namespace std;

int main ()
{
     ifstream inFile;
     //declare variables
       int counter = 0, //row counter
           countErr = 0, //counter for error message
           id;
    double grade1,
           grade2,
           grade3,
           grade4,
           gpa,
           cTotal1 = 0,
           cTotal2 = 0,
           cTotal3 = 0,
           cTotal4 = 0,
           cAvg1,
           cAvg2,
           cAvg3,
           cAvg4;
    bool isFirstTime = true; //bool variable for print header

    //take input data from hw_5-1_ext1.txt
    inFile.open("C:\\hw_5-1_ext1.txt");


    //loop to calculate gpa and print ID, gpa, header and message
    while (inFile >> id >> grade1 >> grade2 >> grade3 >> grade4 )
        
       { if ( isFirstTime )
            { cout << setw(6) << " " << "Student" << setw(6) << " " << "GPA" << setw(8) << " " << "Special Note\n" << endl;
              isFirstTime = false; }

        //calculate GPA
        gpa = (grade1 + grade2 + grade3 + grade4)/4;

        //set decimals to 3 decimal pts
        cout << setprecision(3) << fixed << showpoint;

              //if statement for printing id, gpa and message.  If statement required for message.
              if (gpa >= 3.5)
                  cout << setw(7) << " " << id << setw(7) << " " << gpa << setw(7) << " " << "Honor Roll" << endl;
                      if (gpa >= 1.5 && gpa < 3.5)
                          cout << setw(7) << " " << id << setw(7) << " " << gpa << setw(7) << " " << " " << endl;
                              if (gpa < 1.5 )
                                   cout << setw(7) << " " << id << setw(7) << " " << gpa << setw(7) << " " << "Warning" << endl;

            //accumulaters add up course grades for each row into course grade total
            cTotal1 += grade1;
            cTotal2 += grade2;
            cTotal3 += grade3;
            cTotal4 += grade4;

            //counter is set to count rows in order to tell how many students there are
            counter++;

            //counter for error message
            countErr++;

       }  //end while loop
        if (countErr > 0 )
         {


        //calculate course average outside loop
        cAvg1 = cTotal1/counter;
        cAvg2 = cTotal2/counter;
        cAvg3 = cTotal3/counter;
        cAvg4 = cTotal4/counter;

       //print course averages
        cout << "\n";
        cout <<  setw(8) << " " << "Course 1 average: " << " " << setw(2) << cAvg1 << endl;
        cout <<  setw(8) << " " << "Course 2 average: " << " " << setw(2) << cAvg2 << endl;
        cout <<  setw(8) << " " << "Course 3 average: " << " " << setw(2) << cAvg3 << endl;
        cout <<  setw(8) << " " << "Course 4 average: " << " " << setw(2) << cAvg4 << endl;}
        else
            { cout << "**EMPTY FILE.  NO DATA TO PROCESS.  PLEASE CHECK FILE AND PROCESS AGAIN.**";}
    return 0;

}
inFile.open("C:\\hw_5-1_ext1.txt");

C:\hw_5-1_ext1.txt must exist and be in the correct format.

I ran this code with a file I created that looked like:

123456 100 77 88 55.1
654321 88 77 66 55

I saved the file to C:\hw_5-1_ext1.txt and compiled the program and it ran fine.

Student GPA Special Note

123456 80.025 Honor Roll
654321 71.500 Honor Roll

Course 1 average: 94.000
Course 2 average: 77.000
Course 3 average: 77.000
Course 4 average: 55.050

Meh.. are you sure your math is right in there? o.O Are you trying to find the GPA? Because it totaly not doing that. You're getting the average of the grades, but that's about it.

I have no idea why it can't run -- are you trying to run it on the same operating system that you compiled it on -- such as compile on MS-Windows and attempt to run on *nix :) If you expect an answer you need to ask a more detailed question.

It compiles and runs, you just have to supply the right input file.

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.