1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
//Global Infile and Outfile
ifstream fin;
ofstream fout;
//Function Prototypes
void eofwhileloopfn(ifstream &fin, ofstream &fout,
string name[2],float score[6],float avg[3]);
void inputfn(ifstream &fin,string name[2], float score[6]);
void calcfn(float score[6],float avg[3]);
void outputfn(ifstream &fin, ofstream &fout,
string name[2],float score[6], float avg[3]);
int main ()
{
system ("color f0");
string name[2];
float score[6], avg[3];
char inputfile[51];//stores name of the input file
char outputfile[51];//variable to store the name of the output file
cout<<left<<fixed<<showpoint<<setprecision(2);
cout<<"Enter the input file name: ";
cin>>inputfile;
cout<<endl;
fin.open(inputfile);
if (!fin)
{
cout<<"Cannot open the input file. "<<endl;
system("pause");
return 1;
}
cout<<"Enter the output file name: ";
cin>>outputfile;
cout<<endl;
fout.open(outputfile);
// The End-of-File While Loop Function
eofwhileloopfn (fin,fout,name,score,avg);
fin.close();
fout.close();
system ("pause");
return 0;
}//end of main fn
//***********************************************************************
void eofwhileloopfn(ifstream &fin, ofstream &fout,
string name[2],float score[6], float avg[3])
//float sum
{
//End-Of-File Controlled While Loop
//Call Other Functions Inside of Loop
while (!fin.eof())
{
inputfn(fin,name,score);
calcfn (score,avg);
outputfn(fin,fout,name,score,avg);
}
}//End of Loop Function
//************************************************************************
void inputfn(ifstream &fin, string name[2], float score[6])
{
//File Input
fin>>name[0]>>name[1];
for (int col=0;col<6;col++)
{fin>>score[col];}//col represents index
}
//***********************************************************************
void calcfn(float score[6],float avg[3])
//int sum [2]
{
cout<<left<<fixed<<showpoint<<setprecision(2);
//Program Average
//Test Average
for (int col=0;col<6;col++)
{
while (col<3)
//for (int col=0;col<3;col++)
{
avg[0]=float(score[col]+score[col]+score[col]);
}
while (col>3||col<6)
//for (int col=0;col<6;col++)
{
avg[1]=float(score[col]+score[col]+score[col]);
}
}
//Course Average
avg[0]=avg[0]/3.0;
avg[1]=avg[1]/3.0;
avg[2]=(avg[0]+avg[1])/2.0;
}//end of calculation function
//***********************************************************************
void outputfn(ifstream &fin, ofstream &fout,
string name[2],float score[6],float avg[3])
{
cout<<left<<setw(20)<<name[0]+" "+name[1];
for (int col=0;col<3;col++)
{cout<<left<<setw(5)<<avg[0]<<setw(5)<<avg[1]<<setw(5)<<avg[2];}
cout<<endl;
}//End of Output Function
//***********************************************************************
|