fstream Newbie Help!

Pages: 12
Nov 8, 2016 at 2:35am
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; .
Nov 8, 2016 at 2:36am
No, reading will be very very difficult without the endl. Are you completely sure you are going your way without endl?
Nov 8, 2016 at 2:45am
Yes, the professor requires it, unfortunately. He specifically wants us to learn how to read just portions of a line.
Nov 8, 2016 at 2:47am
Josh Donaldson 
36 15 3 0 0

Can you explain the numbers 36, 15, 3, 0 and 0?
Last edited on Nov 8, 2016 at 2:47am
Nov 8, 2016 at 2:50am
They're baseball statistics. At-bat, hits, walks, hit by pitch, and sacrifice fly, in that order. From the 2016 MLB playoffs.
Nov 8, 2016 at 2:54am
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();


Nov 8, 2016 at 3:09am
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!

Nov 8, 2016 at 3:10am
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.
Nov 8, 2016 at 3:12am
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 Nov 8, 2016 at 3:14am
Nov 8, 2016 at 3:16am
float obp = (hits + walks + hitByPitch) / (atBats + walks + hitByPitch + sacrificeFly);
It should be :
float obp = (float)(hits + walks + hitByPitch) / (float)(atBats + walks + hitByPitch + sacrificeFly);
Nov 8, 2016 at 3:33am
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