C++ Only// The objective of this assignment is to fetch the contents of one file
ID: 3822829 • Letter: C
Question
C++ Only//
The objective of this assignment is to fetch the contents of one file (which is provided) and copy it to another, modified. The input file will contain the names and grades of a certain number of students. Your task is to read those values, calculate the average, figure out the letter grade corresponding, and write the information to another file. For instance, here is what your input and output files could look like.
input file
Patrick Stewart 11 20 30 40 50 60 70 80 90 100
Chris Pine 40 50 60 70 60 50 40 30 60 90
William Shatner 60 70 80 90 50 60 92 90 100 90
Leonard Nimoy 70 80 90 60 70 60 80 90 90 90
Zoe Saldana 70 70 80 90 70 80 90 80 70 60
output file Patrick Stewart: 055.10 (D)
Chris Pine: 055.00 (D)
William Shatner: 078.20 (B)
Leonard Nimoy: 078.00 (B)
Zoe Saldana: 076.00 (B)
Total students: 5
You are required to create the following functionality in the main() function
Read a given file line-by-line. o Each line is composed of a first name, a last name, and 10 grades (integer values).
o You must use c-strings. Each first and last name will have at most 20 characters each.
o You must use a loop to read the grades. You must accumulate the total of grades read for every student.
You must keep count of the number of students read
Output every student on a single line, including the first and last name, average grade and corresponding letter grade using the output format above.
Output the total number of student when you’re done reading all the values.
Each line should be read and outputted accordingly before reading the next line 2 Requirements
The input file is generic; therefore a new one, with new numbers (but using the same format) will be used by the TA for testing your code. There can be any number of lines in this file (including no lines).
You need to check that the input file is correctly open before proceeding.
Proper closure of the input and output files is required
The correspondence between number and letter grade should be done in a separate function called gradeToLetter, where the argument is a double containing the average, and the return value is a single character. Use the following number to letter correspondence: 0-50 = F, 50-60: D, 60-70: C, 70-80: B, 80-100: A.
Hints Control the format output using setw(), setfill(), fixed and setprecision(); It is advised that you use a while() loop to read the input file.
Other Requirements Apart from your main objective, you are required to create tidy code; therefore attention should be paid to format as well as just getting it to work. o Write in a comment at the top of the file information about the course number, the assignment, a small description of that the program does, your name, student number and the date. o Comment the different parts of your code. o You need to indent your code: the body of the functions (between the two curly brackets) needs to be indented by one tab. The body of the loops and control statements should also be indented by one tab. o Your variables and functions need to have meaningful names. o Add appropriate white space: blank lines between chunks of code and spaces between your variable names and operators e.g. int=4 instead of int=4; You need to include the fstream library to read and write to files. You should start your project with completely empty files, therefore your main function should be called main().
Explanation / Answer
Here is the code for you:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
//The correspondence between number and letter grade should be done in a separate function called gradeToLetter where the argument is a double containing the average, and the return value is a single character.
char gradeToLetter(double grade)
{
if(grade < 50)
return 'F';
else if(grade < 60)
return 'D';
else if(grade < 70)
return 'C';
else if(grade < 80)
return 'B';
else
return 'A';
}
int main()
{
ifstream fin;
ofstream fout;
string fileName;
//StudentGradesInput.txt
cout<<"Enter the name of the input file to read the names and grades: ";
cin>>fileName;
fin.open(fileName);
//You need to check that the input file is correctly open before proceeding.
if(!fin.is_open())
{
cout<<"Unable to open the input file."<<endl;
cout<<"Program quitting..."<<endl;
return 0;
}
cout<<"Enter the name of the output file to write the names and averages: ";
cin>>fileName;
fout.open(fileName);
/*if(!fout.is_open())
{
cout<<"Unable to open the output file."<<endl;
cout<<"Program quitting..."<<endl;
return 0;
}*/
string fName, lName;
int grade, sum, count = 0;;
char letterGrade;
//There can be any number of lines in this file (including no lines).
//It is advised that you use a while() loop to read the input file.
while(!fin.eof())
{
//Read a given file line-by-line. o Each line is composed of a first name, a last name, and 10 grades (integer values).
fin>>fName>>lName;
sum = 0;
//You must use a loop to read the grades.
for(int i = 0; i < 10; i++)
{
fin>>grade;
//You must accumulate the total of grades read for every student.
sum += grade;
}
double avg = sum / 10.0;
letterGrade = gradeToLetter(avg);
//Output every student on a single line, including the first and last name, average grade and corresponding letter grade using the output format above.
//Each line should be read and outputted accordingly before reading the next line
//Control the format output using setw(), setfill(), fixed and setprecision();
fout<<fName<<" "<<lName<<": "<<setw(6)<<setfill('0')<<fixed<<setprecision(2)<<avg<<" ("<<letterGrade<<")"<<endl;
//You must keep count of the number of students read
count++;
}
//Output the total number of student when you’re done reading all the values.
fout<<"Total students: "<<count<<endl;
//Proper closure of the input and output files is required
fin.close();
fout.close();
}
Nothing will be printed to the screen for this program. So, no screenshot. I used the same data as input, and it worked perfectly.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.