Write a program to read in the data from the Grades.txt file using an array of s
ID: 3570239 • Letter: W
Question
Write a program to read in the data from the Grades.txt file using an array of struct. All the grades for a particular student should be stored in a single element of the array. Display each student's name and average. Define your struct in a header file!
-Struct
-Student name
-Sum of all grades
-Number of grades
-Main
-Call function to read information from the file
-Call function to display information
-Function to read
-Read in a line from the file
-If the information read is for a student that already has information in the array, add the grade that was read to the existing record in the arr otherwise add information to new element of the array
-Function to display
-Output the name and average grade for each student
Grades.txt has the following info...
Fred,88
Charlie,95
Gus,100
Tina,92
Kate,75
Maria,83
Kelly,99
Kelly,87
Fred,70
Maria,89
Charlie,90
Tina,92
Kate,93
Gus,85
Output shoul be something like this...
Fred :
Average= 79.00
Charlie :
Average= 92.50
Gus :
Average= 92.50
Tina :
Average= 92.00
Kate :
Average= 84.00
Maria :
Average= 86.00
Kelly :
Average= 93.00
Thanks in Advance!
Explanation / Answer
#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
using namespace std;
int main()
{
string name="";
double testscore;
double avearge;
int sum=0;
int count=0;
char grade;
ifstream inFile;
ofstream outFile;
inFile.open("Grades.txt");
if(!inFile)
{
cout << "Cannot open input file"<<endl;
return 1;
}
inFile >> name;
while(inFile)
{
inFile >> testscore;
sum = sum + testscore;
count++;
sum=sum/10;
cout<<left<<setw(12) << name
<< setprecision(2)<<setw(12) << fixed << testscore <<endl;
inFile >> name;
}
system("pause");
return 0;
}
Sample output:
Fred, 88.00
Charlie, 95.00
Gus, 100.00
Tina, 92.00
Kate, 75.00
Maria, 83.00
Kelly, 99.00
Kelly, 87.00
Fred, 70.00
Maria, 89.00
Charlie, 90.00
Tina, 92.00
Kate, 93.00
Gus, 85.00
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.