//*****************************************************************
// R. A. Hillyard
// fileAvg1.cpp
// October 2001
//
// This program reads in test scores from a file named scores.dat
// and computes each student's weighted average. Each line of
// the file consists of the student's id number, followed by her
// scores on the two midterm exams and the final. For example,
// a typical line of the file might look like
// 134256 82 79 91
// The weighted average is calculated by counting the final
// to be the same as two exams.
// The output is printed to the screen
//*****************************************************************
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
void setDecimalPoint(ostream& myStream, int palces);
int main()
{
long int idnum; //variable for id number
int mid1, mid2, final; //variables for test scores
double avg; //variable for average score
ifstream scoreFile; //declare an input file stream variable
setDecimalPoint(cout,2);
scoreFile.open("scores.dat"); //open the file
if(scoreFile.fail()) //check for success and exit if open failed
{
cout << "unable to open file scores.dat\n";
exit(1);
}
//read data from file and process
scoreFile >> idnum >> mid1 >> mid2 >> final;
while (!scoreFile.eof())
{
avg = (mid1 + mid2 + 2*final) / 4.0; //calculate average
cout << idnum << setw(10) << avg << endl; //output result to screen
scoreFile >> idnum >> mid1 >> mid2 >> final;
}//end while
scoreFile.close(); //close file
cin >> idnum;
return 0;
}
void setDecimalPoint(ostream& myStream, int palces)
{
myStream.setf(ios::showpoint); //set percision for decimal numbers
myStream.setf(ios::fixed);
myStream.precision(palces);
}
/*
//*********************contents of scores.dat**********************
123456 82 79 91
234564 88 78 87
976523 91 92 90
782346 77 85 82
987656 62 92 84
345876 77 74 85
//************************program output***************************
123456 85.75
234564 85.00
976523 90.75
782346 81.50
987656 80.50
345876 80.25
*/