I need to refactor the code below to include reasonable functions. I can not jus
ID: 3671788 • Letter: I
Question
I need to refactor the code below to include reasonable functions. I can not just copy whole blocks of code into the functions. Look at what goes where, what is the best way to handle data, what is the most efficient functions call?
This my original code which I got some help from someone on Chegg. Can you help me refactor and include functions in thhe coode below?
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
void printFileToScreen();
char grade(float marks){
if(marks >=90)
return 'A';
else if(marks >=80)
return 'B';
else if(marks >=70)
return 'C';
else if(marks >=60)
return 'D';
else
return 'F';
}
int main()
{
//close the input file here
printFileToScreen();
cout<<"Data Saved Successfully!!!";
return 0;
}
void printFileToScreen()
{
char data[100];
int n;
float average;
float total = 0;
int k = 0;
float marks;
float totalAll = 0;
int a=0,b=0,c=0,d=0,f=0;
// open a file in write mode.
ofstream outfile;
outfile.open("class_statistics.txt");
cout << "Writing to the file" << endl;
cout << "Enter Teacher name: ";
cin.getline(data, 100);
// write inputted data into the file.
outfile <<"Teacher: "<<data << endl;
cout << "Enter Class name: ";
cin.getline(data, 100);
outfile <<"Class: "<<data << endl;
cout<<"Enter number of students: ";
cin>>n;
for(int i=1;i<=n;i++){
k = 0;
total = 0;
cout<<"Enter name of "<<i<<" student: ";
cin.ignore();
cin.getline(data, 100);
cout<<"Enter at most 10 grades of "<<i<<" student:[999 to stop] ";
for(int j=0; j<10;j++){
cin>>marks;
if(marks == 999)
break;
total = total + marks;
k++;
}
average = double(total)/k;
totalAll = totalAll + average;
char g = grade(average);
if(g=='A')
a++;
else if(g=='B')
b++;
else if(g=='C')
c++;
else if(g=='D')
d++;
else
f++;
outfile <<"Student Name: "<<data<<" ";
outfile <<"Average: "<<average<<" ";
outfile <<"Grade: "<<g<<endl;
}
outfile <<"Student Count: "<<n<<endl;
outfile <<"Student Average: "<<double(totalAll)/n<<endl;
outfile <<"A's: "<<a<<endl;
outfile <<"B's: "<<b<<endl;
outfile <<"C's: "<<c<<endl;
outfile <<"D's: "<<d<<endl;
outfile <<"F's: "<<f<<endl;
outfile.close();
}
Explanation / Answer
Answer:
Note: modified code as per user requirements.
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
void readMarks();
char grade(float marks)
{
if(marks >=90)
return 'A';
else if(marks >=80)
return 'B';
else if(marks >=70)
return 'C';
else if(marks >=60)
return 'D';
else
return 'F';
}
//GET MARKS OF A STUDENT AND RETURN THE AVERAGE
float findAverage(int i)
{
float total = 0;
int k = 0;
float marks;
cout<<"Enter at most 10 grades of "<<i<<" student:[999 to stop] ";
for(int j=0; j<10;j++){
cin>>marks;
if(marks == 999)
break;
total = total + marks;
k++;
}
return double(total)/k;
}
//WRITE TEACHER AND CLASS NAME TO THE FILE
void writeTeacherAndClassName(char *data,char *data1)
{
ofstream outfile;
outfile.open("class_statistics.txt",ios::app);
outfile <<"Teacher: "<<data << endl;
outfile <<"Class: "<<data1 << endl;
outfile.close();
}
//WRITES THE STUDENT INFO TO OUTFILE
void writeStudentInfo(char *data,float average,char g)
{
ofstream outfile;
outfile.open("class_statistics.txt",ios::app);
outfile <<"Student Name: "<<data<<" ";
outfile <<"Average: "<<average<<" ";
outfile <<"Grade: "<<g<<endl;
outfile.close();
}
void writeClassInfo(int n, float totalAll,int a,int b,int c,int d,int f)
{
ofstream outfile;
outfile.open("class_statistics.txt",ios::app);
outfile <<"Student Count: "<<n<<endl;
outfile <<"Student Average: "<<double(totalAll)/n<<endl;
outfile <<"A's: "<<a<<endl;
outfile <<"B's: "<<b<<endl;
outfile <<"C's: "<<c<<endl;
outfile <<"D's: "<<d<<endl;
outfile <<"F's: "<<f<<endl;
outfile.close();
}
//READ THE TEACHERNAME AND CLASS NAME
void readInput()
{
char data[100];
char data1[100];
cout << "Enter Teacher name: ";
cin.getline(data, 100);
cout << "Enter Class name: ";
cin.getline(data, 100);
writeTeacherAndClassName(data,data1);
readMarks();
}
//READS THE STUDENT MARKS AND CALLS OTHER FUNCTIONS TOWRITE THE INFO
void readMarks()
{
char data[100];
int n;
float average;
float totalAll = 0;
int a=0,b=0,c=0,d=0,f=0;
cout<<"Enter number of students: ";
cin>>n;
for(int i=1;i<=n;i++)
{
cout<<"Enter name of "<<i<<" student: ";
cin.ignore();
cin.getline(data, 100);
average=findAverage(i);
totalAll = totalAll + average;
char g = grade(average);
writeStudentInfo(data,average,g);
if(g=='A')
a++;
else if(g=='B')
b++;
else if(g=='C')
c++;
else if(g=='D')
d++;
else
f++;
}
writeClassInfo(n, totalAll, a,b,c,d,f);
}
int main()
{
cout<<" C++ program FOR CLASS GRADES"<<endl;
readInput();
return 0;
}