Header files needed: iostream, fstream, string, iomanip, climits For this projec
ID: 3871198 • Letter: H
Question
Header files needed: iostream, fstream, string, iomanip, climits
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!!!!!!!!!!!!!!!
NO SSTREAM ALLOWED!!!!!!!
NO GLOBAL VARIABLE!!!!
When reading the input,
– Use the getline function to r
ead the first name and last name
– Use the extraction operator to r
ead in the scores (float values)
– Don’t forget about the new line
character on the input stream
• Write a segment of code that will
– Read in information for one student
– Calculate the average for that student
– Output the information for that student
• Output the information as shown in the output file
created by the sample solution
• Use two digits of precision for the numbers
This what was given so far but I can't have Sstream
I was told to use a while loop maybe
#include
#include
#include
#include
#include
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();
}
Explanation / Answer
Given below is the fixed code with comments. Output is also shown. Make sure the 1st line in the input file has number of scores as mentioned in the question. See the input file format and output at the end. Post a comment in case of any issues, I will help. If the answer worked well for you, please do rate it. Thank you.
#include <iostream>
#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;
}
//write column heading in output file
fout << setw(20) << std::left << "Name";
fout << setw(20) << std::left << "Average";
fout << setw(20) << std::left << "Grade" << endl;
fout << fixed << setprecision(2); //write floating numbers with 2 decimal places in output file
string line;
string first, last;
char grade;
float score;
int numScores;
fin >> numScores; // the 1st line in the file is the number of scores for each student
fin.ignore(); //get rid of newline before using getline()
getline(fin, first); //read the first name of 1st student
//eof is detected only after a reading some data from file, so read first name before
while (!fin.eof()){
getline(fin, last);
float sum = 0;
int count =0;
while (count < numScores){
fin >> score;
sum = sum +score;
count++;
}
double avg = sum/count;
grade = ' ';
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';
//write to output file
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;
//read the next student's first name
fin.ignore(); //get rid of newline after the last score of previous student
getline(fin, first);
}
fin.close();
fout.close();
}
input file: marks.txt
3
John
Smith
93
99
90
Michael
Jackson
67
85
79
Robert
Williams
45
80
79
output file grades.txt
Name Average Grade
Smith John 94.00 A
Jackson Michael 77.00 C
Williams Robert 68.00 D
program run:
Enter input file name:marks.txt
marks.txt
Enter output file name:grades.txt
grades.txt
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.