Lab 08 – File I/O Objective To practice reading from and writing to files. Tasks
ID: 3600991 • Letter: L
Question
Lab 08 – File I/O Objective
To practice reading from and writing to files.
Tasks
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 spaces. 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. At the same time as you output to the file, also output the student’s full name an average quiz score to the console.
Use a function for computing the average of each student’s quiz scores. Pass the stream objects for the input and output file. Make sure to pass them by reference so that any changes made to their streams (advancing the stream) will be present when you get back to int main. This function should read in only the quiz scores and then output the quiz scores and average score to the output file. You may use the following prototype for the function:
void calcAvg(ifstream& in, ofstream& out);
Your program should be able to handle any number of students in the input file. A sample input file has been provided. There is no guarantee that this exact file will be used for grading so make sure your program is robust. Make sure your program can handle missing and un- openable files.
Sample Output: Console (given provided input file)
Sample Output: “lab08_out.txt” (given provided input file)
Hess Damian 5 10 7 5 9 2 8 5 6 8 6.5 Nielsen Andrew 7 9 8 7 4 7 3 4 9 4 6.2 Yankovic Alfred 6 5 8 10 9 6 5 7 3 10 6.9 Scharf Inga 9 2 7 10 7 8 8 10 6 9 7.6
Procedure
In CodeBlocks, create a new project on a flashdrive.
Download the file lab08.cpp from blackboard to your project folder.
Rename lab08.cpp to lab08_yourlastname.cpp.
In CodeBlocks, remove main.cpp from the project and add lab08_yourlastname.cpp to the project.
Perform the tasks outlined above. Make sure you can generate the sample output.
When you are done working on the lab, upload lab08_yourlastname.cpp to blackboard
(from the assignment page, click Browse My Computer) and submit the assignment.
Explanation / Answer
here is your program : ------------------------------>>>>>>>>>>>>>>>>>>>
#include<iostream>
#include<fstream>
using namespace std;
struct Student{
double grade[6];
string firstName;
string lastName;
};
class ExtractData{
Student data[50];
ifstream f;
ofstream of;
int noOfStudent;
public:
ExtractData(char *inName,char *oName){
f.open(inName);
of.open(oName);
noOfStudent = 0;
for(int i = 0;!f.eof();i++){
if(i >= 50){
break;
}
if(!f.is_open()){
cout<<" Input File Is not Opened ";
break;
}
if(!of.is_open()){
cout<<" output File Is not Opened or not created ";
break;
}
//extracting data from file
f>>data[i].lastName;
f>>data[i].firstName;
f>>data[i].grade[0];
f>>data[i].grade[1];
f>>data[i].grade[2];
f>>data[i].grade[3];
f>>data[i].grade[4];
//writing data to output file
of<<data[i].lastName<<" ";
of<<data[i].firstName<<" ";
of<<data[i].grade[0]<<" ";
of<<data[i].grade[1]<<" ";
of<<data[i].grade[2]<<" ";
of<<data[i].grade[3]<<" ";
of<<data[i].grade[4]<<" ";
//writting to the console
cout<<" "<<data[i].firstName;
cout<<" "<<data[i].lastName;
noOfStudent++;
//calculating average grades
data[i].grade[5] = (data[i].grade[0]+data[i].grade[1]+data[i].grade[2]+data[i].grade[3]+data[i].grade[4])/5;
//writing average to the output file
of<<data[i].grade[5]<<" ";
//writting average to the console
cout<<" "<<data[i].grade[5]<<" ";
}
f.close();
of.close();
}
};
int main(){
//variable declaration to store filename
char inputFile[50],outputFile[50];
//prompting to user to enter the file names
cout<<" Enter The Input Filename ";
cin>>inputFile;
cout<<" Enter The output Filename ";
cin>>outputFile;
//calling constructor which has all the logic of your requirement
ExtractData e(inputFile,outputFile);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.