For this project, you will write a complete C++ program that performs all of the
ID: 3869684 • Letter: F
Question
For this project, you will write a complete C++ program that performs all of the following tasks.
Use variables of Data Type float to contain all test scores.
(1) Open a user specified (user enters in the name) input file. Verify that the file opened
successfully. If it did not open successfully, output an error message and terminate. –
Reference project 4 for information on this test.
(2) If the input file is successfully opened, prompt for and open a user specified output file. Verify
that the output file opened successfully. If it did not open successfully, output an error
message and terminate. Use the filename “Bad/file” to cause the open function to fail for the
output file.
(3) For steps 1 and 2 echo print out the name of the file entered by the user.
(4) First item in the input file is an integer value indicating the number of test scores. Read it from
the input file then test the file stream to see if the input file is empty.
(5) Write the column headings to the output file (see the sample solution output)
(6) Read a student’s first name and start an end of file loop
(7) For the first action in the loop, read the students last name. Then use a loop to read in and
sum up the test scores for the student. The number of scores to read is the first number read
from the input file as noted above in step 4. (check the Project 5 input file information on page
3). After reading the test scores, test the file stream to see if it is in the fail state. If it is, then a
file read error has occurred.
(8) Find the average of the test scores and determine the letter grade equivalent for that average.
(9) Write the information for the student to the output file. Output the first 9 characters of the last
name, the first 10 characters of the first name, the test average and the letter grade for the test
average (90,80,70,60 split for A, B, C, D and F). Test average is output with 2 decimal places.
(10)
(11)
(12)
Remove the newline character from the input file and read the first name of the next student
Repeat steps 7, 8, 9 and 10 for the next student.
Output of the program shall match that of the sample solution
NO ARRAYS ALLOWED!!!!!!!!!!!!!!!
Explanation / Answer
#include<iostream>
#include<string>
#include<sstream>
#include<fstream>
#include<iomanip>
using namespace std;
int main(){
string input,output;
ifstream fin;
ofstream fout;
cout << "Enter input file name:" ;
cin >> input;
cout << input << endl;
fin.open(input.c_str());
if (!fin){
cout << "Error opening file.Terminating program! ";
return 0;
}
cout << "Enter output file name:" ;
cin >> output;
cout << output << endl;
fout.open(output.c_str());
if (!fout){
cout << "Error opening file.Terminating program! ";
return 0;
}
fout << setw(20) << std::left << "Name";
fout << setw(20) << std::left << "Average";
fout << setw(20) << std::left << "Grade" << endl;
string line;
string first, last;
char grade;
int a;
while (getline(fin,line)){
istringstream str(line);
str >> first >> last;
int sum = 0;
int count =0;
while (str >> a){
sum = sum +a;
count++;
}
double avg = sum/count;
if (avg >=90 && avg <= 100)
grade = 'A';
if (avg >=80 && avg < 90)
grade = 'B';
if (avg >=70 && avg < 80)
grade = 'C';
if (avg >=60 && avg < 70)
grade = 'D';
if ( avg < 60)
grade = 'F';
fout << setw(20) << std::left << last.substr(0,9) + " " + first.substr(0,10) ;
fout << setw(20) << std::left << avg;
fout << setw(20) << std::left << grade << endl;
}
fin.close();
fout.close();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.