Question: How I fix this source code that make can read everything from txt file /*Yihsiang Cheng LAB 7 Fill Rates*/ #include #include #include #include #define
How I fix this source code that make can read everything from txt file
/*Yihsiang Cheng
LAB 7 Fill Rates*/
#include
#include
#include
#include
#define MAX 100
using namespace std;
void outputHead();
int getdata(ifstream& infile,string crn[],string classname[],int cenrollment[],int maxenrollment[],int waitlist[]);
void output(string crn[],string classname[],int cenrollment[],int maxenrollment[],int waitlist[],double fillrate[],int size);
double calculateAverageFillRate(double fillrate[],int totalCount);
int main()
{
ifstream infile;
string classname[MAX];
string crn[MAX];
int cenrollment[MAX],maxenrollment[MAX],waitlist[MAX];
double fillrate[MAX];
int size,totalCount;
infile.open("Lab7FillRates.txt");
if(infile.fail())
{
cout << "No such file!" << endl;
}
outputHead();
size=getdata(infile,crn,classname,cenrollment,maxenrollment,waitlist);
output(crn,classname,cenrollment,maxenrollment,waitlist,fillrate,size);
calculateAverageFillRate(fillrate, totalCount);
return 0;
}
/////////////////////////////////////////
void outputHead()
{
ofstream f;
f.open("Lab 7 output.txt");
f << "CRN" << setw(15) << "Course" << setw(25) << "CurrentEnrollment" << setw(25) << "MaxEnrollment" << setw(25) << "Number of WaitList" << setw(15) << "FillRate" << endl;
f.close();
cout << "CRN" << setw(15) << "Course" << setw(25) << "CurrentEnrollment" << setw(25) << "MaxEnrollment" << setw(25) << "Number of WaitList" << setw(15) << "FillRate" << endl;
}
/////////////////////////////////////////
int getdata(ifstream& infile,string crn[],string classname[],int cenrollment[],int maxenrollment[],int waitlist[])
{
/*
Pre: infile - reference to the datafile
crn[] - array of CRN
classname[] - array of ClassName
cenrollment[] - array of CurrentEnrollment for the class
maxenrollment[] - array of MaxEnrollment for the class
waitlist[] - array of Number of students on waiting list for the class
Post: Fill Rate on CIS department in DE ANZA
Purpose: To input the information from data file
*/
int count = 0;
while ( count < MAX && !infile.eof())
{
infile >> crn[count] >> classname[count] >> cenrollment[count] >> maxenrollment[count] >> waitlist[count];
count++;
}
return count;
}
////////////////////////////////////////////////
double calculateAverageFillRate(double fillrate[],int totalCount)
{
double a=0;
for(int i=0;i //compute sum of all fill rate a+=fillrate[i]; //return average return a/totalCount; } //////////////////////////////////////////////// void output(string crn[],string classname[],int cenrollment[],int maxenrollment[],int waitlist[],double fillrate[],int size) { /* Pre: infile - reference to the datafile crn[] - array of CRN classname[] - array of ClassName cenrollment[] - array of CurrentEnrollment for the class maxenrollment[] - array of MaxEnrollment for the class waitlist[] - array of Number of students on waiting list for the class fillrate[] - array of FillRate about the class size - size of our class Post: nothing Purpose: To input the information to screen */ ofstream f; f.open("Lab 7 output.txt",ios::app); for (int i=0; i < size; i++) { f << crn[i] << " " << classname[i] << " " << cenrollment[i] << " " << maxenrollment[i] << " " << waitlist[i] << " " << fillrate[i] << endl; } f.close(); for (int i=0; i < size; i++) { cout << crn[i] << " " << classname[i] << " " << cenrollment[i] << " " << maxenrollment[i] << " " << waitlist[i] << " " << fillrate[i] << endl; } return; } this is txt file 00469 CIS D002.-62Z 27 40 0 00471 CIS D003.-02Y 22 40 0 00472 CIS D003.-03Y 14 40 0 02638 CIS D003.-04Y 32 40 0 20371 CIS D003.-62Z 40 40 12 22477 CIS D003.-61Y 8 40 0 22326 CIS D004.-62Y 7 40 0 22333 CIS D004.-62Z 40 40 2 22582 CIS D004.-65Z 23 40 0 21790 CIS D014A-62Z 24 40 0 00479 CIS D018A-62Y 20 40 0 02639 CIS D018A-01Y 40 40 7 21713 CIS D022A-02Y 39 40 0 21714 CIS D022A-03Y 22 40 0 21715 CIS D022A-04Y 31 40 0 21716 CIS D022A-05Y 40 40 2 21717 CIS D022A-61Y 6 40 0 21718 CIS D022A-62Y 7 40 0 22006 CIS D022A-06Y 40 40 10 22027 CIS D022A-63Y 5 40 0 22329 CIS D022A-07Y 40 40 8 22569 CIS D022A-08Y 16 40 0 22584 CIS D022A-09Y 40 40 7 23868 CIS D022A-62Z 35 40 0 21719 CIS D022B-62Z 40 40 15 21720 CIS D022B-02Y 40 40 1 21721 CIS D022B-61Y 25 40 0 22325 CIS D022B-03Y 39 40 3 22583 CIS D022B-04Y 40 40 15 22320 CIS D022C-61Y 35 40 0 22327 CIS D022C-01Y 40 40 15 22855 CIS D022C-02Y 40 40 0 23078 CIS D022C-62Z 40 40 15 23076 CIS D028.-61Y 39 40 0 22958 CIS D029.-61Y 30 40 0 00484 CIS D035A-01Y 26 40 0 00485 CIS D035A-61Y 40 40 5 23382 CIS D035A-62Z 40 40 15 00487 CIS D035B-61Y 28 40 0 22856 CIS D036A-01Y 40 40 15 23412 CIS D036A-61Y 17 40 0 22957 CIS D036B-61Y 34 40 0 23383 CIS D040.-01Y 40 40 8 24578 CIS D040.-61Y 40 40 7 23384 CIS D041A-01Y 33 40 0 24090 CIS D041A-02Y 40 40 15 24666 CIS D041B-02Y 40 40 13 24539 CIS D056.-62Z 33 60 0 24101 CIS D064A-62Z 25 40 0 22859 CIS D064C-62Z 28 40 0 24683 CIS D064F-62Z 27 40 0 00491 CIS D066.-62Z 40 40 0 24920 CIS D074.-62Y 5 40 0 00494 CIS D089A-62Z 31 40 0 00495 CIS D089C-62Z 14 40 0 24684 CIS D095C-55L 8 40 0 24685 CIS D095D-55L 8 40 0 25067 CIS D095E-55L 5 40 0 21723 CIS D098.-62Z 13 40 0 23077 CIS D099.-62Z 26 40 0 24252 CIS D102.-62Z 27 40 0 21722 CIS D108.-62Z 17 60 0 02644 CIS D21JA-01Y 40 40 5 22853 CIS D21JA-02Y 40 40 12 23989 CIS D21JA-62Y 36 40 0
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
