Stats Program

Hello everyone! I am currently working on a World Series Statistics Program for my Comp Science Class at school. I have a problem with my program though. The calculations for Batting Average, Slugging Average, and On Base Average, are all wrong and I can't figure out why. Here is my code so far....

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <iostream>
#include <iomanip>

using namespace std;

const int MAXLASTNAME = 20;
const int MAXFIRSTNAME = 10;
const int MAXPLAYERS = 20;

struct Baseball {
	char FirstName[MAXFIRSTNAME+1];
	char LastName[MAXLASTNAME+1];
	float AB;
	float singles;
	float doubles;
	float triples;
	float HR;
	float walks;
	double BA;
	double SA;
	double OBA;
};

void checkPlayers(int &players);
int getData(Baseball[]);
void showData(Baseball[], int);

int main()
{
	Baseball stats[MAXPLAYERS];

	int players = getData(stats);
	showData(stats, players);

}

int getData(Baseball stats[])
{
	int i, players;

	cout << "How many players would you like to enter data for(1-20): ";
	cin >> players;
	cout << endl;

	checkPlayers(players);

	for(i=0;i<players;i++) {
		cout <<	"Please enter player #" << i+1 << "'s first name: ";
		cin >> stats[i].FirstName;

		cout << "Please enter player #" << i+1 << "'s last name: ";
		cin >> stats[i].LastName;

		cout << "Please enter the number of at bats for player #" << i+1 << ": ";
		cin >> stats[i].AB;

		cout << "Please enter the number of singles for player #" << i+1 << ": ";
		cin >> stats[i].singles;

		cout << "Please enter the number of doubles for player #" << i+1 << ": ";
		cin >> stats[i].doubles;

		cout << "Please enter the number of triples for player #" << i+1 << ": ";
		cin >> stats[i].triples;

		cout << "Please enter the number of home runs for player #" << i+1 << ": ";
		cin >> stats[i].HR;

		cout << "Please enter the number of walks for player #" << i+1 << ": ";
		cin >> stats[i].walks;
	}

	
	double sum = 0;

	for(i=0; i<players; i++) {
    	sum += (stats[i].singles + stats[i].doubles + stats[i].triples + stats[i].HR);

		cout.precision(3);
		cout.setf(ios::fixed);
		stats[i].BA = sum / (stats[i].AB);
		stats[i].SA = (stats[i].singles + (2*stats[i].doubles) + (3*stats[i].triples) + (4*stats[i].HR)) / (stats[i].AB);
		stats[i].OBA = (sum + stats[i].walks) / (stats[i].AB + stats[i].walks);
	}

    return players;
}

void showData(Baseball stats[], int players)
{
	int i;
	cout << endl << endl;
	cout << "Here is the data that you entered, in a nice chart format!\n\n";


	cout << "First Name" << setw(14) << "Last Name" << setw(6) << "AB" << setw(6) << "1B" << setw(6) << "2B";
	cout << setw(6) << "3B" << setw(6) << "HR" << setw(6) << "BB" << setw(6) << "BA" << setw(6) << "SA";
	cout << setw(6) << "OBA" << "\n";


	cout << "----------" << setw(14) << "---------" << setw(6) << "--" << setw(6) << "--" << setw(6) << "--";
	cout << setw(6) << "--" << setw(6) << "--" << setw(6) << "--" << setw(6) << "--" << setw(6) << "--";
	cout << setw(6) << "---" << "\n";


	for(i=0; i<players; i++)
	{
		cout << stats[i].FirstName
		     << setw(15) << stats[i].LastName
		     << setw(8) << stats[i].AB
		     << setw(8) << stats[i].singles
		     << setw(8) << stats[i].doubles
		     << setw(8) << stats[i].triples
		     << setw(8) << stats[i].HR
		     << setw(8) << stats[i].walks
			 << setw(8) << stats[i].BA
			 << setw(8) << stats[i].SA
			 << setw(8) << stats[i].OBA
		     << endl;
	}


}





void checkPlayers(int &players)
{
	while((players < 0) || (players > 20)) {
		cout << "Invalid value entered!!!\n";
		cout << "A valid value is between 0 and 20 inclusive!";
		cout << "Please reenter your answer: ";
		cin >> players;
	}
}



Topic archived. No new replies allowed.