217k views
1 vote
C++

Write a program that reads the student information from a tab separated values (tsv) file. The program then creates a text file that records the course grades of the students. Each row of the tsv file contains the Last Name, First Name, Midterm1 score, Midterm2 score, and the Final score of a student. A sample of the student information is provided in StudentInfo.tsv. Assume the number of students is at least 1 and at most 20. Assume also the last names and first names do not contain whitespaces. The program performs the following tasks: Read the file name of the tsv file from the user. Open the tsv file and read the student information. Compute the average exam score of each student. Assign a letter grade to each student based on the average exam score in the following scale: A: 90 =< x B: 80 =< x < 90 C: 70 =< x < 80 D: 60 =< x < 70 F: x < 60 Compute the average of each exam. Output the last names, first names, exam scores, and letter grades of the students into a text file named report.txt. Output one student per row and separate the values with a tab character. Output the average of each exam, with two digits after the decimal point, at the end of report.txt. Hint: Use the setprecision manipulator to format the output. Ex: If the input of the program is: StudentInfo.tsv and the contents of StudentInfo.tsv are: Barrett Edan 70 45 59 Bradshaw Reagan 96 97 88 Charlton Caius 73 94 80 Mayo Tyrese 88 61 36 Stern Brenda 90 86 45 the file report.txt should contain: Barrett Edan 70 45 59 F Bradshaw Reagan 96 97 88 A Charlton Caius 73 94 80 B Mayo Tyrese 88 61 36 D Stern Brenda 90 86 45 C Averages: midterm1 83.40, midterm2 76.60, final 61.60

User Shimizu
by
7.7k points

1 Answer

3 votes

// req. libraries

#include <fstream>

#include <iomanip>

#include <iostream>

// macros //

// letter grade macro

#define letterGrade(e) \

((e >= 90) \

? 'A' \

: ((e >= 80) ? 'B' : ((e >= 70) ? 'C' : ((e >= 60) ? 'D' : 'F'))))

// get .tsv filename and read.

std::ifstream &openTsv(const std::string &name) {

// create object

std::ifstream *tsv = new std::ifstream(name + ".tsv");

if (!(*tsv)) {

std::cerr << "File does not exist." << std::endl;

delete tsv;

tsv = nullptr;

}

return *tsv;

}

int main(int ac, char *av[]) {

// temp name

std::string tmp;

// get input from user

std::cout << "Enter the name of the tsv file: ";

std::cin >> tmp;

// call function

std::ifstream &obj = openTsv(tmp);

// arrays to store some stuff

double sums[4]; //[0] is exam1, [1] is exam2 and [2] is final. [3] stores the

// number of students

std::string last_name, first_name;

double exam1, exam2, finalexam;

std::ofstream output_file("report.txt");

while (obj >> last_name >> first_name >> exam1 >> exam2 >> finalexam) {

// Calculate the sum of each exam

sums[0] += exam1;

sums[1] += exam2;

sums[2] += finalexam;

// Write student information to the output file

output_file << last_name << "\t" << first_name << "\t" << exam1 << "\t"

<< exam2 << "\t" << finalexam << "\t"

<< letterGrade((exam1 + exam2 + finalexam) / 3.0) << std::endl;

sums[3]++;

}

double ex2sum = sums[1] / sums[3];

// Write the averages to the output file

output_file << "Averages: midterm1 " << std::fixed << std::setprecision(2)

<< sums[0] / sums[3] << ", midterm2 " << ex2sum << ", final "

<< sums[2] / sums[3] << std::endl;

// Close the files

obj.close();

output_file.close();

std::cout << "Grades have been recorded in report.txt." << std::endl;

return 0;

}

C++ Write a program that reads the student information from a tab separated values-example-1
C++ Write a program that reads the student information from a tab separated values-example-2
C++ Write a program that reads the student information from a tab separated values-example-3
User Bidby
by
9.0k points