Okay so I figured out whats wrong in my program. It doesn't seem to read the next name that follows. here is my code:
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;
getline (fin, Name);
while( !fin.eof())
{
getline(fin,Name);
fin >> Score;
So this is the while statement. I tried to work with the getline because the input file looks like this:
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
I see how after ever -1 I want it to stop the while loop and continue to the next line. But I don't think I programed it to actually read the next line. How am I suppose to fix my getline?
GRRR! I tried checking it and changed to regular fin instead of getline...but still when I try to run it it doesn't seem to find the info to calculate. i do get the first set of fout before the first while loop but thats all.
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()) //Checks if file will open
{
cout << "Incorrect output file.\n Program terminated.\n";
return 0;
}
else
{
fin >> Class_Name; //Reads first line and saves it as class name
fout <<setw(70)<<endl;
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 << "90\t < average\t\t A"<<endl;
fout << "80\t < average <=\t 90\t B"<<endl;
fout << "67.5\t < average <=\t 80\t C"<<endl;
fout << "55\t < average <= 67.5\t D"<<endl;
fout << "0\t <= average <= 55\t F"<<endl;
while( !fin.eof()) // Initaites while statement, while not the end of file
{
fin >> Name; //Reads Name and saves it as Name
fin >> Score; //Reads score and saves it as Score
while ( Score != -1 ) // Initiates while looping statement, while score does not equal -1
{
NumTs++; //Increases number of tests by 1
SumTs+=Score;
NumSs++;
if ( NumTs == 0 )
{
fout <<left<<endl;
fout <<Name<<setw(25)<<" Error there were no scores for this student."<<endl;
}
else
{
fout <<setprecision(3)<<endl;
fout <<Name<<setw(15)<<NumTs<<setw(15)<<AvgS<<setw(15)<<Grade<<setw(10)<<endl;
}
NumTs = 0;
fin >> Name;
fin >> Score;
} //End of while looping statement, returns to top with new Name and Score
Class_Avg = Sum_Avg/NumSs;
fout <<left<<"Class average for "<<NumSs<<" students:\t"<<Class_Avg<<endl;
}
fin.close();
fout.close();
Is it my If statements? I think the brackets are in place right....mmm is it something small? or did I do a huge coding mistake that should never be done!? It is how i tried to set up all the other if statements with my while statements? I can't see my error...someone please point me in the right direction I feel blind! DX