c++ format please help me You will write a program that reads and stores student
ID: 653690 • Letter: C
Question
c++ format please help me
You will write a program that reads and stores student data from an input file, computes and stores each student's test average, then prints a report to an output file, along with class averages.
Input File Format
The first line of the input file will be a single integer number -- this value tells how many records are in the file.
After this, each line represents one student record. Each record consists of the following fields (in this order), separated by commas:
last name: a character string (assume no more than 20 characters)
first name: a character string (assume no more than 20 characters)
course: a single character. 'M' for math, 'H' for history, 'E' for English. Represents the course that the student is taking.
Test 1 grade: an integer
Test 2 grade: an integer
Final exam grade: an integer
You may assume that a file is in the correct format, and that the types and limits will be as described above
A sample input file
Program Details
Create a structure type called Student. A variable of type Student should contain fields for the following:
last name
first name
course
test 1 grade
test 2 grade
final exam grade
test average -- (use type double)
The other fields match the data described in the input file format section. Use C-strings for the last name and first name.
Your program should start by asking the user to type the names of the input and output files (in that order). Whenever a bad filename is entered, print an error message and prompt the user to re-enter the filename.
Open the input file and read the records into an array of Student structures. You'll need to create this array dynamically, because you won't know how many records there are until you start reading the file itself. The data from each record in the file should be stored in one Student structure (i.e. one array slot per student record)
For each student, compute the test average and store it in the test average member variable for that student struct. The test average should be computed according to the following weights for the tests:
Print a grade summary report to the output file, as specified below in the "Output File Format" section
Close the files and clean up any dynamically allocated space before ending the program
Output File Format
The summary report is to be sorted by course, with English first, then History, then Math. Each course's section in the summary should have a header, with the student detail records printed below
The summary output of a student record should consist of:
The student's name, in the format firstName lastName -- with no extra punctuation between the names.
Test Average, printed to 2 decimal places
Letter Grade of the test average, based on standard 10-point scale (i.e. 90-100 = A, 80-89.99 = B, etc.)
After each course section, print the test average for the class. (This means there will be three class averages -- one for English, one for History, one for Math)
See sample output file below to see the formatting
Sample Input File
Output File Contents (after the program run)
Explanation / Answer
#include<iostream>
#include<cstring>
#include<fstream>
using namespace std;
struct Student{
string lastName,firstName;
char subject;
int marks1,marks2,marks3;
};
char calcGrade(double avg){
char grade;
if(avg>=90.0&&avg<=100.0)
grade='A';
else if(avg>=80&&avg<=89.99)
grade='B';
else if(avg>=70&&avg<=79.99)
grade='C';
else if(avg>=60&&avg<=69.99)
grade='D';
else
grade='E';
return grade;
}
int main(){
char inFilename[20],outFilename[20];
int noOfRec,i,sum,count;
double avg,classAvg;
char grade;
cout<<"Please enter the name of the input file: ";
cin>>inFilename;
cout<<"Please enter the name of the output file: ";
cin>>outFilename;
ifstream ifile;
ifile.open(inFilename);
ofstream ofile;
ofile.open(outFilename);
if(!ifile){
cout<<inFilename<<" cannot be opened";
return -1;
}else{
ifile>>noOfRec;
struct Student s[noOfRec];
for(i=0;i<noOfRec;i++){
ifile>>s[i].lastName,s[i].firstName,s[i].subject,s[i].marks1,s[i].marks2,s[i].marks3;
}
if(!ofile){
cout<<outFilename<<" cannot be opened";
}else{
ofile<<"Student Grade Summary ";
ofile<<"--------------------- ";
ofile<<"ENGLISH CLASS ";
ofile<<"Student name Test Avg ";
sum=0;
count=0;
classAvg=0.0;
for(i=0;i<noOfRec;i++){
if(s[i].subject=='E'){
avg=(s[i].marks1+s[i].marks2+s[i].marks3)/3;
grade=calcGrade(avg);
ofile<<s[i].firstName<<" "<<s[i].lastName<<" "<<avg<<" "<<grade<<" ";
sum+=avg;
count+=1;
}
}
classAvg=sum/count;
ofile<<" Class Average "<<classAvg<<" ";
ofile<<"--------------------- ";
ofile<<"HISTORY CLASS ";
ofile<<"Student name Test Avg ";
sum=0;
count=0;
classAvg=0.0;
for(i=0;i<noOfRec;i++){
if(s[i].subject=='H'){
avg=(s[i].marks1+s[i].marks2+s[i].marks3)/3;
grade=calcGrade(avg);
ofile<<s[i].firstName<<" "<<s[i].lastName<<" "<<avg<<" "<<grade<<" ";
sum+=avg;
count+=1;
}
}
classAvg=sum/count;
ofile<<" Class Average "<<classAvg<<" ";
ofile<<"--------------------- ";
ofile<<"MATH CLASS ";
ofile<<"Student name Test Avg ";
sum=0;
count=0;
classAvg=0.0;
for(i=0;i<noOfRec;i++){
if(s[i].subject=='M'){
avg=(s[i].marks1+s[i].marks2+s[i].marks3)/3;
grade=calcGrade(avg);
ofile<<s[i].firstName<<" "<<s[i].lastName<<" "<<avg<<" "<<grade<<" ";
sum+=avg;
count+=1;
}
}
classAvg=sum/count;
ofile<<" Class Average "<<classAvg<<" ";
ofile<<"--------------------- ";
ifile.close();
ofile.close();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.