My program works the only problem I am having is that when it reads the end of file it adds the last score to the total again. here is my program what have I done wrong with.eof?
#include <iostream>
#include <fstream> // File Stream Library
using namespace std;
int main()
{
char team;
int numb, k=0, n=0;
ifstream gamefile;// ifstream variable name declaration
// open file
gamefile.open("/home1/c/a/acsi201/data/game");
//if the file fails to open it will return this statement
if(gamefile.fail())
{
cout << "Input file opening failed.\n";
exit(1);
}
/*while loop keeps reading file till the end of file with if else
statements to compile the scores the winning team would be displayed
first.*/
while(!gamefile.eof()) // while loop
{
gamefile >> team >> numb; //reading the info provided by the file
/* begining of if statement the character K stand for the Knicks
and if team equals K than it will return knicks points scored
otherwise it will return the Nets score.*/
if(team == 'K')
k = k + numb;
else
n = n + numb;
if(k > n)
/* Another if statement for winning team to go first.If the Knicks
score is greater than the Nets than the Knicks score will be
read first otherwise the Nets score will be read first.*/
cout << "Knicks " << k << " Nets " << n << endl;
else
cout << "Nets " << n << " Knicks " << k << endl;
}
// end of whileloop
gamefile.close();// closing of file previousely opened
cout << "Good Game! \n";
/* end of program since a output file was not required, to see output
after compilation run file hw5 < result than at prompt type more
result*/
}