Intro to Using Files in C++

So, I'm using infile for the first time, I'm confuse on if I'm doing it right.
The file open up. But, it asking to calculate the average exam score. and display the average to 2 decimal places.

****Directions:

Read the data line by line . You will need several variables to read each value into. (hint: fName, lName, exam1,
exam2, exam3 …..)
- calculate the average exam score for each student
- using cout, display the exam average to 2 decimal places for each computer scientist. (i.e. 83.12)
-close the file

*****Names and exam score *****
Grace Hopper 81.5 90.0 65.5 79.9 93.2
Alan Kay 75.5 81.5 70 90 98.2
John Backus 99.0 74 100 87.7 99.9
Sergey Brin 62 100 88.5 79.7 97.3
Dennis Ritchie 97 98 100 100 99.9
Brian Kernighan 100 99 100 89 99

**Program**

#include <fstream>
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <string>
using namespace std;
int main()
{



double average;
int name1, name2, name3, name4, name5, name6;
string name;

ifstream infile;

infile.open("scores.txt");

if (infile)
{
// using a while loop read in and display the data
infile >> name1;
cout << " Grace Hopper has an exam average of";
cin >> average;

infile >> name2;
cout << "Alan Kay has an exam average of ";
cin >> average;

infile >> name3;
cout << "Joh nBackus has an exam average of";
cin >> average;

infile >> name4;
cout << "Sergey Brin has an exam average of";
cin >> average;

infile >> name5;
cout << "Dennis Ritchie has an exam average of";
cin >> average;

infile >> name6;
cout << "Brian Kernighan has an exam average of";
cin >> average;



cout << fixed << setprecision(2);
cout << average << endl;

infile.close();
}
else
{
cout << "Error opening file." << endl;
}//end of else
return 0;
}
Topic archived. No new replies allowed.