Help with while statement with reading and writing on input and output.

Okay so I'm guessing the problem here is a very very small one. Its a long program that I have but I'm sure its in the while statement where the problem lies. This program is suppose to read from an input file and then calculate with the numbers given and put the results in the output file. The calculations aren't done when I look at the output file after running it. You may spot alot of unorganized things here and there but that's because this is my first time ever trying to write a program from scratch.

So here it is, be more than welcome to skip the beginning and just look at the file statement(one about while it is not the end of the file):



#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <fstream> // Bring the function to deal with dta files

using namespace std ;

int main ()

{

string input_file, output_file, Name, Class_Name;

float Score, AvgS, SumTs, Class_Avg, Sum_Avg;
char Grade;
int NumTs, NumSs;

NumTs = 0;
SumTs = 0;
NumSs = 0;
Sum_Avg = 0;

ifstream fin;
ofstream fout;

cout << "Please enter Input file name: ";
cin >> input_file;
fin.open(input_file.c_str()); //opening the data file
if (fin.fail()) //Checks if file will open
cout << "Incorrect Input file.\n Program terminated.\n";
else
{
cout << "Please enter Output file name: ";
cin >> output_file;
fout.open(output_file.c_str());
}

if (fout.fail())
{
cout << "Incorrect output file.\n Program terminated.\n";
return 0;
}
else
{
getline (fin, Class_Name);

fout <<"Grade report for "<<Class_Name<<endl;
fout <<"This program will process test scores to provide individuals with letter"<<endl;
fout <<"grades according to the following scale:"<<endl;
fout <<setw(30)<< "90\t < average\t\t A"<<endl;
fout <<setw(30)<< "80\t < average <=\t 90\t B"<<endl;
fout <<setw(30)<< "67.5\t < average <=\t 80\t C"<<endl;
fout <<setw(30)<< "55\t < average <= 67.5\t D"<<endl;
fout <<setw(30)<< "0\t <= average <= 55\t F"<<endl;

fout <<left<<endl;
fout <<"====================================================================="<<endl;
fout <<"Name"<<setw(15)<<"Number of Tests"<<setw(15)<<"Average"<<setw(15)<<"Grade"<<setw(10)<<endl;
fout <<"---------------------------------------------------------------------"<<endl;

while( !fin.eof())
{
getline (fin, Name);
fin >> Score;

while ( Score != -1 )
{
NumTs++;
SumTs+=Score;
getline (fin, Name);
fin >> Score ;


AvgS = SumTs/NumTs;
Sum_Avg+=AvgS;

if ( AvgS > 90.0 )
Grade = 'A';
else if ( AvgS <= 90.0 && AvgS > 80.0)
Grade = 'B';
else if ( AvgS <= 80.0 && AvgS >= 67.5)
Grade = 'C';
else if ( AvgS <= 67.5 && AvgS > 55)
Grade = 'D';
else
Grade = 'F';

if ( NumTs == 0 )
{
fout <<left<<endl;
fout <<Name<<setw(25)<<" Error there were no scores for this student."<<endl;
getline(fin,Name);
fin >> Score;
}
else
{
fout <<setprecision(3)<<endl;
fout <<Name<<setw(15)<<NumTs<<setw(15)<<AvgS<<setw(15)<<Grade<<setw(10)<<endl;
}
fin.ignore(10,'\n');
getline(fin,Name);
}
Class_Avg = Sum_Avg/NumSs;
fout <<left<<"Class average for "<<NumSs<<" students:\t"<<Class_Avg<<endl;
}
fin.close();
fout.close();

}
return 0;
}






Okay so my main question, what is wrong with my while looping statement that prevents this :

File Input:

Introduction to 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



from turning out like something like this:

File Output:

(The first few fouts come out just fine so I erased them from here for now ^.^)

======================================================================
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
----------------------------------------------------------------------
Class average for 3 students: 72.3

Can I also get a small explanation as to why the problem is and how fixing it fixes it? (Does that question make sense? o.O )

Anyways any help will be very much appreciated! Thanks!
Topic archived. No new replies allowed.