Help with Baseball Program

Hey all I'm having some real trouble wrapping my head around how to go about this assignment. I have to create a baseball program that shows a baseball players stats(that are pulled from a file) and put their stats in parallel arrays. Here is what I have so far and I've been working on this program for well over 8 hours. I tried asking my teacher for help through email but she just says that the problem I'm trying to get over is the "biggest hurdle" and doesn't help beyond that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/****************************
Program 3 Baseball
Programmer: 
****************************/

#include <fstream>
#include <iostream>
#include <string>
using namespace std;
const int MAXPLAYER=30;
//prototype statements

void main()
{
	string names[30];
	int numbers[30];
	int hits[30];
	ifstream input;
	ofstream output;

	//open the files
	input.open("Bin.txt");
	output.open("Bout.txt");

	//verify file was opened
	//int i=0;
	if (!input.fail())
	{
		for(int index=0;index<MAXPLAYER; index++)
				{
					getline(input,names[index],',');
					input.ignore(20,'\n');
					output<<names[index];
					output<<endl;
				}
		for(int index=0;index<MAXPLAYER; index++)
				{
					input>>numbers[index];
					output<<numbers[index];

				}
		for(int index=0;index<MAXPLAYER; index++)
				{
					input>>hits[index];
					output<<hits[index];
					output<<endl;
				}
	}


	else cout<< "File was not opened."<< endl;
	//close files
	input.close();
	output.close();
	system("pause");
}
/*************************************
		Player Names
*************************************/

Here is my file im supposed to pull the information from.

Borland C++, 	82 430 120 682
John Doe, 	22 249 125 374
Some body,	25 896 171 1067
Mario, 		45 485 133 618
Luigi,		46 743 156 899
Princess peach,	69 654 153 807
Donkey Kong,	90 494 163 657
Wario, 		65 238 123 500
Waluigi, 	44 475 111 586
Billy Mays,	99 685 432 1117
Chuck Norris,	0  999 0   999
Rayden,		77 394 151 545
Cinder,		44 130 123 477
Lappy,		10 947 44  991
Compy,		1  495 123 618
MidiSoft,	6  480 40  520
Music Man,	4  444 99  543
1337 H4x0r,	13 999 0   1337
Cool Dude,	66 549 101 650
Hot chick,	89 696 169 865
Kirby,		32 293 190 492
Electrolyte,	33 928 58  986
Dusty,		14 198 59  297
Sparky,		19 968 39  1257
Halogen, 	87 405 34  467

I got the program to open the file and pull the names into a string and place that string into the output file. But i can't seem to figure out how to get the player numbers etc. to be placed in parallel arrays. Every time I run this program it spits out the names and then puts the numbers below all the names and the numbers are just random numbers that make no sense. Any help is much appreciated.
It looks like in your read loop (27-48) you try to read all the names, then all the first numbers, then all the second numbers. You need to read a name, a number, a number, a number, a number, then loop and start reading a name again.
Are you supposed to change the format of the output in the output file? If so you might want to pull the output and work on that separately from the input loops. But like Zhuge said, you should do each line on it's own then move to the next. This way you'll only need one for loop to read in each of the 4 pieces of data. Then you can write another loop to output it however you want. If the format has to remain the same, including spaces to pad the columns a bit, that's a slightly different problem though.
Last edited on
I need the output to look like this
______________________________________________________________________
		Player & Team Statistics Report				
______________________________________________________________________
Name			Number	Hits	Outs	AtBats Walks	BatAvg

Terry Steinbach		 1	124	330	491	37	0.273
Mark McGuire		 2	113	377	587	97	0.231
Tony Phillips		 3	118	333	524	73	0.262
Mike Gallego		 4	 90	267	409	52	0.252
Carnie Lansford		 5	185	366	616	65	0.336
Ricky Henderson		 6	 90	216	381	75	0.294
Dave Henderson		 7	145	434	643	64	0.250
Stan Javier		 8	 77	233	348	38	0.248
Dave Parker		 9	146	407	600	47	0.264
Ron Hassey		10	 61	207	298	30	0.228
Walt Weiss		11	 55	181	263	27	0.233
Jose Conseco		12	 61	166	258	31	0.269
Luis Polonia		13	 59	147	218	12	0.286
Glenn Hubbard		14	 26	105	153	22	0.198
Lance Blanken		15	 29	96	137	12	0.232
Billy Beane		16	 19	60	 82	 3	0.241
Felix Jose		17	 11	46	 61	 4	0.193
Ken Phelps		18	  1	8	 13	 4	0.111
Jamie Quirk		19	  2	8	 10	 0	0.200
Larry Arndt		20	  1	5	  7	 1	0.167
Chris Bando		24	  1	1	  2	 0	0.500
Mike Mills		25	113	216	491	162	0.343
______________________________________________________________________
Team Statistics			61	168	264	34	0.224


Where the names are one array all the way down, the numbers are another array all the way down, etc.
Number doesn't necessarily need to be an array, there's way to simply use a variable that's incremented in a loop to output the number of each record as you output it. But ya, I'd handle the input in a single for loop, then move on to another loop to output everything (well, except for the lines that aren't players... those can be outputted outside of the loop obviously.)
Those numbers are just examples the numbers will be something can be changed in the input file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
if (!input.fail())
	{
		for(int index=0;index<MAXPLAYER; index++)
				{
					getline(input,names[index],',');
					input>>numbers[index]>>hits[index]>>outs[index]>>atbat[index];
					input.ignore(20,'\n');
				}

		output<<"______________________________________________________________________"<<endl;
		output<<"                   Player & Team Statistics Report"<<endl;
		output<<"______________________________________________________________________"<<endl;
		output<<"Name			Number	Hits	Outs	AtBats Walks	BatAvg"<<endl;
		for(int index=0;index<MAXPLAYER; index++)
				{
					output<<names[index]<<"		 "<<numbers[index]<<"	"<<hits[index]<<"	"<<outs[index]<<"	"<<atbat[index];
					output<<endl;
				}

is what i put in there now and its starting to look better but
______________________________________________________________________
                   Player & Team Statistics Report
______________________________________________________________________
Name			Number	Hits	Outs	AtBats Walks	BatAvg
Borland C++		 82	430	120	682
John Doe		 22	249	125	374
Some body		 25	896	171	1067
Mario		 45	485	133	618
Luigi		 46	743	156	899
Princess peach		 69	654	153	807
Donkey Kong		 90	494	163	657
Wario		 65	238	123	500
Waluigi		 44	475	111	586
Billy Mays		 99	685	432	1117
Chuck Norris		 0	999	0	999
Rayden		 77	394	151	545
Cinder		 44	130	123	477
Lappy		 10	947	44	991
Compy		 1	495	123	618
MidiSoft		 6	480	40	520
Music Man		 4	444	99	543
1337 H4x0r		 13	999	0	1337
Cool Dude		 66	549	101	650
Hot chick		 89	696	169	865
Kirby		 32	293	190	492
Electrolyte		 33	928	58	986
Dusty		 14	198	59	297
Sparky		 19	968	39	1257
Halogen		 87	405	34	467
		 1637440	44	4281803	1976965344
		 4304277	30421916	4336852	5273856
		 30408706	1637324	1637176	1637100
		 30419896	4215188	0	1976830629
		 1637616	44	1637208	1976965344


the out put is still kind of wonky
Last edited on
Topic archived. No new replies allowed.