Answer:
Here is the C++ program:
#include <fstream> //to create, write and read a file
#include <iostream> // to use input output functions
using namespace std; //to access objects like cin cout
const int MAXNAME = 20; //MAXNAME is set to 20 as a constant
int main(){ //start of main() function
ifstream inData; //creates an object of ifstream class
inData.open("grades.txt"); //uses that object to access and opens the text file using open() method
char name[MAXNAME + 1];
// holds the names
float average;
//stores the average
inData.get(name,MAXNAME+1); //Extracts names characters from the file and stores them as a c-string until MAXNAME+1 characters have been extracted
while (inData){ //iterates through the file
inData >> average; //reads averages from file using the stream extraction operator
cout << name << " has a(n) " << average << " average." << endl;
//prints the names along with their averages
inData.ignore(50,'\n'); //ignores 50 characters and resumes when new line character is reached. It is used to clear characters from input buffer
inData.get(name,MAXNAME+1);} //keeps extracting names from file
return 0; }
Explanation:
The program is well explained in the comments added to each line of the code. The program uses fstream object inData to access the grades.txt file. It gets and extracts the contents of the file using get() method, reads and extracts averages from file using the stream extraction operator. Then program displays the names along with their averages from grades.txt on output screen.
The grades.txt file, program and its output is attached.