Hogwarts needs a program to calculate their students’ grades. Wizardry can only
ID: 3886318 • Letter: H
Question
Hogwarts needs a program to calculate their students’ grades. Wizardry can only do so much, and the supernatural task of grading is beyond their magical abilities.
I want you to create a program that will grade the students’ test scores and give a final average. You will read the data from an input file and write the results to an output file.
Copy the following three lines and paste them into your input file named student_data.txt. Your program will read in this data. The format for each line of the file is as follows : (student’s name, year, grade 1, grade 2, grade 3)
Harry 1 80 70 60
Draco 2 95 90 100
Luna 3 60 75 55
Calculate the average (your average should be a floating point value) and print everything to a file named averages.txt. Print “Data written to averages.txt.” to the console.
Correct Console Output:
Data written to averages.txt
Explanation / Answer
//main.cpp
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
ifstream fin;
fin.open("student_data.txt");
ofstream fout;
fout.open("averages.txt");
fout << "Name Year Average";
string name;
float year, gr1, gr2, gr3, avg;
for (int x=0; x<3; x++){
fin >> name >> year >> gr1 >> gr2 >> gr3;
avg = (gr1+gr2+gr3)/3.0;
fout << " " << name << " " << year << " " << avg;
}
cout << "Data written to averages.txt";
fin.close();
fout.close();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.