Rewriting old program using functions!

Hey guys, my assignment is to rewrite this old program that I wrote by using various types of functions. The old program worked perfectly. it read the following file, and produced the table below it. The new program though, does not exit the loop, and surprisingly does not print anything on the output file. So its not even reading the tableheader function apparently.
I would appreciate some help. Thanks!

INPUT FILE:

Introduction to Computer Programming I
Toots Sweet
87 76 90 -1
Willy Nilly
73 63 64 70 -1
Phil O'Sophy
-1
Jill Quirk
90 80 70 -1
John Smith
100 90 87 100 -1
Al Bondy
50 56 30 60 -1
Barb Nelson
90 89 78 100 80 70 -1
Bob Brown
80 76 82 66 50 -1
Nancy Johnsen
90 90 -1
John Derikson
100 78 99 89 77 97 -1


ORIGINAL OUTPUT FILE:

======================================================================
Name Number of Tests Average Grade
----------------------------------------------------------------------
Toots Sweet 3 84.3 B
Willy Nilly 4 67.5 D
Phil O'Sophy 0 Error there were no scores for this student.
Jill Quirk 3 80.0 C
John Smith 4 94.2 A
Al Bondy 4 49.0 F
Barb Nelson 6 84.5 B
Bob Brown 5 70.8 C
Nancy Johnsen 2 90.0 B
John Derikson 6 90.0 B
----------------------------------------------------------------------
Class Average for 9 students is: 78.9




NEW PROGRAM:

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;

void Tableheader(ofstream&);
void bigloop(string &student1, string &student2, ifstream& Ifile, ofstream& Ofile, float& Tsum, int&Tcount);
char lettergrade (float Avg);
int endtable(float Tsum, int Tcount, ofstream&Ofile);

/*1. Main Function to open and close files. Declare some variables.
-calls func 2
-calls func 3
-calls func 5

2. Header function

3. Big Do-while loop function
-calls func 4

4. letter Grade determination function

5. Endtable function */


int main()

{
int Tcount=0;
float Tsum=0, Tavg=0;
ifstream Ifile;
ofstream Ofile;
string ifilename,ofilename, random, student1, student2;

cout<<"Please enter input file name: ";
cin>>ifilename;
Ifile.open(ifilename.c_str());

if (Ifile.fail())
{cout<<"could not open input file, error program terminated. \n";
return 0;}

cout<<"Please enter output file name: ";
cin>>ofilename;
Ofile.open(ofilename.c_str());

if (Ofile.fail())
{cout<<"could not open output file, error program terminated. \n";
return 0;}

Tableheader(Ofile);
bigloop (student1, student2, Ifile, Ofile, Tsum, Tcount);
endtable(Tsum, Tcount,Ofile);

}


void Tableheader(ofstream& Ofile)
{Ofile<<"======================================================================"<<
"\nName Number of Tests Average Grade";
Ofile<<"\n----------------------------------------------------------------------";}



void bigloop(string &student1, string &student2, ifstream& Ifile, ofstream& Ofile, float& Tsum, int&Tcount)
{

do
{


int count=0, Score=0;
char letter=0;
float Sum=0,Avg=0;

Ifile>>Score;

while (Score>=0)
{
Sum+=Score;
count++;
Ifile>>Score;
}

if (count!=0)
{
Avg=Sum/count;
Tsum+=Avg;
Tcount++;
}

if (count==0)
Ofile<<" "<<endl<<fixed<<left<<setw(20)<<student1+""+student2 <<"0"<<" Error there were no scores for this student.";
else
Ofile<<" "<<endl<<fixed<<setprecision(1)<<left<<setw(20)<<student1+" "+student2<<setw(15)<<count<<setw(15)<<Avg<<lettergrade(Avg);



Ifile>>student1;
Ifile>>student2;
}while (!Ifile.eof());

}


char lettergrade (float Avg)
{
char letter;

if (Avg<=55)
letter='F';
else if (Avg<=67.5)
letter='D';
else if (Avg<=80)
letter='C';
else if (Avg<=90)
letter='B';
else
letter='A';
return (letter);

}


int endtable(float Tsum, int Tcount, ofstream& Ofile)
{

float Tavg=0;

if (Tcount==0)
{Ofile<<"Table cannot be completed, no data was provided in input file";
return 0;}


Tavg=Tsum/Tcount;



Ofile<<endl<<"Class Average for "<<Tcount<<" students is: "<<Tavg;}


Last edited on
Topic archived. No new replies allowed.