Write a program to compute numeric grades for a course. The course records are i
ID: 3688291 • Letter: W
Question
Write a program to compute numeric grades for a course. The course records are in a file that will serve as the input file. The input file is in exactly the following format: Each line contains a student’s last name, then one space, then the student’s first name, then one space, then ten quiz scores all on one line. The quiz scores are whole numbers and are separated by one space. Your program will take its input from this file and send its output to a second file. The data in the output file will be the same as the data in the input file except that there will be one additional number (of type double ) at the end of each line. This number will be the average of the student’s ten quiz scores. If this is being done as a class assignment, obtain the file names from your instructor. Use at least one function that has file streams as all or some of its arguments.
Explanation / Answer
main.cpp
#include <iostream>
#include <fstream>
#include <cstdlib>
//#include <conio.h>
int q9();
//void corrector(std::ifstream& file1, std::ofstream& file2);
void processgrade(std::ifstream& file1, std::ofstream& file2);
int main()
{
std::ifstream inputFile1;
std::ofstream outputFile1;
inputFile1.open("q9in.txt");
if (inputFile1.fail())
{
std::cout << "Input file opening failed. ";
//_getch();
exit(1);
}
outputFile1.open("q9out.txt");
if (outputFile1.fail())
{
std::cout << "Output file opening failed. ";
//getch();
exit(1);
}
processgrade(inputFile1, outputFile1);
inputFile1.close();
outputFile1.close();
//_getch();
return 0;
}
int q9()
{
std::ifstream inputFile1;
std::ofstream outputFile1;
// Camelcase/Camelcaps naming convention
inputFile1.open("q9in.txt");
if (inputFile1.fail())
{
std::cout << "Input file opening failed. ";
//_getch();
exit(1);
}
outputFile1.open("q9out.txt");
if (outputFile1.fail())
{
std::cout << "Output file opening failed. ";
// _getch();
exit(1);
}
while (!inputFile1.eof())
{
processgrade(inputFile1, outputFile1);
}
inputFile1.close();
outputFile1.close();
//_getch();
return 0;
}
void processgrade(std::ifstream& file1, std::ofstream& file2)
{
char nextChar;
int count = 0;
double nextNum, sum = 0, avg;
while (count < 2)
{
file1.get(nextChar);
file2 << nextChar;
std::cout << nextChar;
if (nextChar == ' ')
{
count++;
}
}
count = 0;
std::cout << std::endl;
while ((count < 10) && (file1 >> nextNum))
{
file2 << nextNum << ' ';
sum = sum + nextNum;
count++;
}
avg = sum/count;
file2 << avg;
}
//_getch();
// return 0;
//}
q9in.txt
Smith Reed 95 93 88 72 86 78 90 82 86 94
Ingersoll Buchannan 70 77 80 83 88 84 89 90 93 99
Lockhart Kirkpatrick 80 88 90 91 92 84 85 83 89 93
Day Jones 83 85 87 88 90 92 94 96 89 93
Seamans Eckert 71 80 88 83 88 88 90 88 91 95
sample output:
Smith Reed
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.