fstream Newbie Help!

Pages: 12
Well, I didn't put the endl; . My professor wants us to keep the names and stats on the same lines. But yes, that is what it would have been had I used the endl; .
No, reading will be very very difficult without the endl. Are you completely sure you are going your way without endl?
Yes, the professor requires it, unfortunately. He specifically wants us to learn how to read just portions of a line.
Josh Donaldson 
36 15 3 0 0

Can you explain the numbers 36, 15, 3, 0 and 0?
Last edited on
They're baseball statistics. At-bat, hits, walks, hit by pitch, and sacrifice fly, in that order. From the 2016 MLB playoffs.
You can use the following code to read the file.
1
2
3
4
5
6
7
8
9
cout << "Reading the file : " << endl;
string firstName, lastName;
int atBat, hits, walks, hitByPitch, sacrificeFly;
while(inFile >> firstName >> lastName >> atBat >> hits >> walks >> hitByPitch >> sacrificeFly)
{
    cout << firstName << ' ' << lastName << ' ' << atBat << ' ' << hits << ' ' << walks << ' ' << hitByPitch << ' ' << sacrificeFly << endl;
}

inFile.close();


Thank you, this is EXACTLY what I needed! I guess I had the right idea, I just used the incorrect syntax, but this helped A LOT!

Thank you, this is EXACTLY what I needed! I guess I had the right idea, I just used the incorrect syntax, but this helped A LOT!

You can mark the thread as solved.
Now, I also have one more question:

After all of that info is displayed, I added this to the bottom of the loop:

float obp = (hits + walks + hitByPitch) / (atBats + walks + hitByPitch + sacrificeFly)

I basically wanted to put the on-base percentage at the end of each line. When I use that formula, they all come out to "0". When, for example, the first player's on-base percentage should be what 18/39 comes out to. Even if I set obp to (18) / (39), it comes out to 0.
Last edited on
float obp = (hits + walks + hitByPitch) / (atBats + walks + hitByPitch + sacrificeFly);
It should be :
float obp = (float)(hits + walks + hitByPitch) / (float)(atBats + walks + hitByPitch + sacrificeFly);
Thanks for sticking with my question despite initial confusion due to wording on my part. This has helped me a bunch! I now have enough information to finish the project myself, thanks!
Topic archived. No new replies allowed.
Pages: 12