Using xCode, FWIW.
Basically, didn't really get this concept in class, and cannot figure it out by searching online so far. Here's my assignment:
"You are to write a program that will process a text file that contains the following information for a group of professional baseball players (one line of data per player): Name (at most 20 characters, padded with blanks on the right), At Bats, Hits, Walks, Hit by Pitches, and Sacrifice Flies. Sample data for one player: Francisco Cervelli 326 86 56 6 5 This means that in the year this data was gathered (2016), Francisco Cervelli had 326 at bats, 86 hits, 56 walks, 6 hit by pitches, and 5 sacrifice flies. Thus, his on-base percentage for the season was: = 0.3766 . You should prompt the user for the name of an input data file, make sure the requested file exists, prompt the user for the name of an output file, make sure it does not already exist, and then, for each player: -- read in the data -- calculate the on-base percentage -- display the on-base percentage along with the player’s name -- store the player’s name, on-base percentage, and number of at bats in the output file (one line per player). After all players have been processed, your program should display the following and also store this information in the output file: -- Player with the highest on-base percentage and this OBP. -- Player with the lowest on-base percentage and this OBP. -- The average on-base percentage for all players, calculated in each of the two ways discussed in class. Use at least a few functions (with complete, proper argument lists -- do NOT use global variables) in this program. Your code should be fully documented. A portion of your grade on this project will depend on your program structure and organization."
I'm stuck on trying to get the data from a text file, because I can't seem to reference a pre-created one, or create one with an ofstream. Here's the code I've tried. Not really sure where I'm going wrong, but I know it's probably a simple fix. Thanks, guys!
// // main.cpp // Baseball Statistics //
include <iostream>
They're supposed to be the same as what is commented out, since the commented out code didn't work. But that didn't work either. We're supposed to use the ofstream file anyways.
When I get rid of the comments, and reference Stats.text, like below, I get a bunch of errors. I'm just trying to figure out how to create that output file, read it, and put the info into variables.
The ofstream should create a new file called "Stats.txt". I want to be able to "get" (.get()) the info from this new file, and put it in variables in the cpp file.
Basically, when I type out this:
char name [21];
Stats.get(name, 21);
Why does that not either output "Josh Donaldson", or at least assign "Josh Donaldson" to "name"?
I basically don't know how to read text files in C++.
That gave me the contents of the file. Now, how would I do it one-by-one? I want to extract the first player's name, then their OBP, then AB, Hits, Walks, etc. Then do the same for the second/third/fourth/etc. player.
I don't want to get the whole line at once, but I want to get the first string (the name), assign that to a variable, and do the same for the numbers.
Anyways, here's what that got me (and I don't know why it worked, or how):