I can’t get the pointer back to zero so i can go through the data again after the echo.
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
ifstream infile;
infile.open ("scores.txt" , ifstream::in);
int ch = infile.get();
string lastname;
char gender= ' ';
string collegetype;
float score=0;
float scoreMALE=0;
float scoreFEMALE=0;
int countMALE=0;
int countFEMALE=0;
while ( !infile.eof())
{
cout << (char) ch;
ch = infile.get();
}
infile.seekg(0,ios::beg);
while ( !infile.eof())
{
infile>>lastname>>gender>>collegetype>>score;
if(gender == 'm')
{
scoreMALE = scoreMALE + score;
countMALE++;
}
else if (gender == 'f')
{
scoreFEMALE = score;
countFEMALE++;
}
}
cout<<"\n\n\n The total Female Scores is"<<countFEMALE<<"\n\n\n The total of the male scores is"<<countMALE; // Checking to see if file works
infile.close();
cout<<"Press <Enter> to Exit";
cin.ignore();
cin.get();
return 0;
}
here’s the input file:
Bailey M CC 68
Harrison F CC 71
Grant M UN 75
Peterson F UN 69
Hsu M UN 79
Bowles M CC 75
Anderson F UN 64
Nguyen F CC 68
Sharp F CC 75
Jones M UN 75
McMillan F UN 80
Gabriel F UN 62
Adding the infile.clear() above the infile.seekg(0,ios::beg), causes the code to iterate through the second loop here (as Kerreck-SB suggested) .
The reason your scores would still be 0 after doing this is due to the character comparison used being of the incorrect case. E.g. ‘f’ and ‘m’ instead of the ‘F’ and ‘M’ in your input file.