Arrays and functions trouble...

what am i doing wrong here? I've commented out some tests and somethings that just aren't working the way that i need them too. this is a calculating baseball statistics program for a homework assignment. I haven't had beginners c++ for two semesters thanks to scheduling issues and now i just seem to have lost all memory as to how to program correctly.
any help will be greatly appreciated.

ps the baseballstats.txt file reads as:
10 36 25 2 5
2 12 5 0 1
34 18 7 1 0
63 41 20 4 2
12 10 3 1 0
14 2 1 1 1
27 55 27 10 8
8 27 12 3 4
42 32 8 2 1
33 19 4 1 0



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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
  #include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;

int loadArrays(int[],int[],int[],int[],int[]);
void calcBatAvg(int[], int[], int[], int[]);
void printStats(int[], int[], int[], int[], int[], int[], int);

int main()
{
	const int SIZE = 20;
	int playerNum[SIZE], atBats[SIZE], hits[SIZE],
		runs[SIZE], rbis[SIZE], batAvg[SIZE],numberOfPlayers;
	
	numberOfPlayers = loadArrays(playerNum, atBats, hits, runs, rbis);

	//cout << numberOfPlayers << endl;
	
	//calcBatAvg(batAvg, hits, atBats, numberOfPlayers);
	printStats(playerNum, atBats, hits, runs, rbis, batAvg, numberOfPlayers);

	system("pause");
	return (0);
}

int loadArrays(int playerNum[],int atBats[],int hits[],int runs[],int rbis[])
{
	int i = 0, numberOfPlayers = 0;
	ifstream inFile;
	inFile.open("BaseballStats.txt");
	if (inFile.fail())
		cout << "There was an error opening the file.\n";
	while (inFile >> playerNum[i]) 
	{
		inFile >> atBats[i];
		inFile >> hits[i];
		inFile >> runs[i];
		inFile >> rbis[i];

		//cout << playerNum[i] << " " << atBats[i] << " " << hits[i]
			//<< " " << runs[i] << " " << rbis[i] << endl;
		
		i++;
	}

	inFile.close();
	return i;
}

void calcBatAvg(int batAvg[], int hits[], int atBats[], int numberOfPlayers)
{
	for (int i = 0; i < numberOfPlayers; i++)
	{
		batAvg[i] =  (hits[i] / atBats[i]) * 1000;

		//cout << batAvg << endl; //not working!
	}
}

void printStats(int player[], int bat[], int hit[], int run[], int rbi[], int bavg[], int count)
{
	cout << "Player Num" << "\t" << "At Bat" << "\t" << "Hits" 
		 << "\t" << "Runs" << "\t" << "Bat Avg" << endl;

	for (int i = 0; i <= count; i++) 
	{
		cout << player[i] << "\t" << bat[i] << "\t" 
			 << hit[i] << "\t" << run[i] << "\t" << rbi[i] 
			 << "\t" << bavg[i] << endl;
	}

}
Last edited on
Ok, on line 8, the last int[] should only be an int.
On line 56 remember you are doing integer division so if the hits are less that atBats then the batAvg is always going to be 0. Cast these into doubles or change their type.
On line 58 you don't want batAvg, you want batAvg[i].
On line 67, it should be i < count or you go out of bounds.
thanks much. after i got that sorted the rest of the programming was a cinch.
Topic archived. No new replies allowed.