Having Trouble Reading a text file and assigning data to different variables/arrays

I'm Sorry for the huge message, I figured It would make it easier to understand if you actually saw the assignment. I am only having difficulty with one specific area. That is, I'm Not exactly sure how to read a data file and assign different variables/arrays to so much data. The Text file would include a PItchers name, season, Innings pitched, hits allowed, Runs allowed, Earned runs allowed, walks, strikeouts... It will also have multiple pitchers...

QUESTION: How do I assign variable/arrays to so many different pieces of data?
(Since I cannot Highlight the Specifics, I have boldened where I need guidance. )

ASSIGNMENT:
COMP 1102 Project Three
Assigned: April 13, 2018
April 27, 2018
Due:
Note: Penalties for lateness will be assessed after this date.
Note: ABSOLUTELY NO PROJECTS WILL BE ACCEPTED AFTER MAY 2, 2018

It's baseball season again! Our next project centers around one way to statistically analyze
starting pitchers...

The Society of American Baseball Research (SABR) is a group of baseball fans, writers,
historians, and other researchers. The members of SABR are intensely interested in and
study all facets of the game. They have created Sabermetrics, which is the mathematical and
statistical analysis of baseball records. One of the many topics studied by sabermetricians is
pitching statistics and how to create ones that are more meaningful measures to use in the
comparison of pitchers, both past and present.

One statistic that is used in the evaluation of starting pitchers is Game Score, developed by
sabermetrics pioneer Bill James. (A modified version of Game Score was published by Tom
Tango in 2016.) The original method for calculating the Game Score for a particular game is:

• Begin with 50 points.
• Add one point for every out recorded; thus, three points for every complete inning pitched.
• Add two points for each full inning completed after the fourth (on top of the points above).
• Add one point for every strikeout (K).
• Subtract two points for every hit allowed.
• Subtract four points for every earned run allowed.
• Subtract two points for every unearned run allowed.
• Subtract one point for each walk (BB).

You have been asked to write a program that will, for a group of pitchers, calculate the
Game Score for every game of each pitcher's season, along with that pitcher's average Game
Score. Your program will be processing a text file that contains the following information for
each pitcher:

1st line: Name (25 characters, padded with blanks on the right), and Season (year) (an
integer in columns 26-29).
Subsequent lines (one line per game): Innings Pitched (IP), Hits Allowed (H), Runs Allowed
(R), Earned Runs Allowed (ER), Walks (BB), and Strikeouts (K).

You must create a data file (as described above and discussed in class) in order to test your
program. Sample data (first few lines for Max Scherzer's 2017 season):

Max Scherzer 2017
6.2 4 2 2 2 7
6.0 4 3 1 2 10
6.2 1 1 1 2 11
7.0 5 4 2 2 10
Note that a different data file will be used to test your program.


Write a program that will process the data in such a file and will display (on screen)
each pitcher's Name and the season, followed by the Game Score for each game
started by the pitcher (one Game Score per line). At the end of a pitcher's data,
the number of games started by that pitcher and the pitcher's Average Game Score
should be displayed. Show the Average Game Score to two decimal places (nearest
hundredth).

Sample output (for the sample data shown above):
Max Scherzer 2017
Game Scores :
63
Number of Games Started: 2
Average Game Score: 63.50


You should make efficient use of functions in this program.
Program structure and efficiency will be considered when this project is graded.
Additional specifications may be given later.


Your source file should be named GAME SCORE.CPP.

You may turn in your project by submitting your source file as an attachment to a Blackboard
message. You may also submit your data file, if you desire. DO NOT send your executable
file.

BONUS Opportunity: For 10 bonus points, have your program create an output file that will
contain the following information, sorted into descending order based on Average Game
Score: Pitcher Name, Season, # of Games Started, and Average Game Score.
Last edited on
Hello Mjimmie1,

I do not see any problem here.

The only answer I have is 42.

Andy
Lol, I am sorry, I accidentally posted before even starting to develop the question
Hello Mjimmie1,

OK. That happens, but I still do not see what you have done. Just what you need to do.

You need to decide if you will store this information in a struct or class before you read the file.

Start with small steps.

Andy
Hello Mjimmie1,

I came up with this that might help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
std::vector<Pitcher> pitchers;

while (inFile >> temp.s_fName)
{
	inFile >> temp.s_lName >> temp.s_year;

	while (inFile >> games.s_inningsPiched)
	{
		inFile >> games.s_hitsAllowed >> games.s_runsAllowed >> games.s_earnedRunsAllowed
			>> games.s_walks >> games.s_strickeouts;

		temp.s_games.emplace_back(games);
	}

	inFile.clear();

	pitchers.emplace_back(temp);

	temp.s_games.clear();
}


Hope that helps,

Andy
Thank You!
Handy Andy, I haven't started writing the code yet since the whole program would base off of the user's Text File. I was having a difficult time figuring out how to even start. If I should input all of the text files related info in Arrays, or if a single data type (int, float double, etc.) would suffice.
I also see you have used a vector. I have not learned the use for vectors yet, but if it is best suited for this application, I will get into it now.

The big question I haven't been able to answer pertains to any program...How do you begin developing the program? I'm sure you experienced developers can do these basic programs without any real thought at all. I just can't figure the right way to develop the program before actually writing the code( If that makes any sense). Any recommendations?
Last edited on
Hello Mjimmie1,

As you have summarized the first thing you need to do is read the file and collect the information from the file.

What I did not include in the above code is the header file that contains two structs to hold the information.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef _STRUCT_ 
#define _STRUCT_

struct Games
{
	double s_inningsPiched{};
	std::size_t s_hitsAllowed{};
	std::size_t s_runsAllowed{};
	std::size_t s_earnedRunsAllowed{};
	std::size_t s_walks{};
	std::size_t s_strickeouts{};
};

struct Pitcher
{
	std::string s_fName;
	std::string s_lName;
	std::size_t s_year{};
	std::vector<Games> s_games;
};

#endif // end !_STRUCT_ 

"Games" holds all the numbers for any line of numbers. And in "Pitcher" the last is a vector of "Games" so that one object of "Pitcher" holds all the numbers for each line of numbers.

I am not saying that you have to use what I have done, but it is a suggestion. I could have just as easily used arrays, but vectors are easier to use and a little better.

Vectors are not that hard to learn. I did it by just using them and have learned some of the finer points later. Start by including the header file <vector> and look at the last line of the "Pitcher" struct for how to define a vector. What you put between the <> is the type of the vector. This could be as simple as "int", "double", string or a class or struct.

The big question I haven't been able to answer pertains to any program...How do you begin developing the program?
Well for me and may others it starts in my head for the basic idea then goes into the code. For Others, like yourself, you may want to start with a piece of paper. Make notes of what variables you will need followed by an idea of how the program will flow. Sometimes a flow chart of the program flow is a good idea.

Back in the day when I was in my first C class out instructor made the statement that in the beginning it was 10% planning and 90% coding, but these days it is more like 90& planning and 10% coding. Point being thee more you plan the less time you spend correcting your code. Expect when you have someone changing the program in the middle.

This program is a good example. You could code other parts of the program, but until you can read the file and store the information there is not much else you can do. The example of small steps if the code to create the file stream followed by reading the file and storing the information. Even with my experience I put break points in the program to check that the file was being read correctly. It is always a good idea to write a little code then run the program to see how it is working.

I will work on something to give you an idea of how you might plan a program.

Hope that helps,

Andy
I cannot Thank You enough for your help. Learning Coding has been quite intimidating, but I'm getting there. With the help I have received from this great community, it is becoming easier.
Topic archived. No new replies allowed.