#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;
}
LADY187 0 Newbie Poster
chococrack 74 Junior Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
chococrack 74 Junior Poster
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.