Baseball Structs Program

Hey Everyone!!

I am having some problems with my structures program for school. The problem comes after inputting the data in the struct and then trying to display the information in the struct in a chart format. I get some weird hex symbols while for the chart output. If anyone can help me that would be great! Thanks!

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
#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;
};

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

int main()
{
	Baseball stats[MAXPLAYERS];
	
	getData(stats);
	showData(stats);

}

void 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 calcBA, calcSA, calcOBA;
	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);
	calcBA = sum / (stats[i].AB);
	calcSA = (stats[i].singles + (2*stats[i].doubles) + (3*stats[i].triples) + (4*stats[i].HR)) / (stats[i].AB);
	calcOBA = (sum + stats[i].walks) / (stats[i].AB + stats[i].walks);
	}
	
}
	
}

void showData(Baseball stats[])
{ 
	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<MAXPLAYERS; i++)
	{
		cout << stats[i].FirstName;
		cout << setw(14) << stats[i].LastName;
		cout << setw(6) << stats[i].AB;
		cout << setw(6) << stats[i].singles;
		cout << setw(6) << stats[i].doubles;
		cout << setw(6) << stats[i].triples;
		cout << setw(6) << stats[i].HR;
		cout << setw(6) << stats[i].walks;
		cout << 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;
	}
}
Check your for loops in function 'getData'. I have a feeling your braces are misplaced....

Also you're iterating over too many players, you're bound to get garbage data.

Here you go:
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
#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;
};

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 calcBA, calcSA, calcOBA;
	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);
	calcBA = sum / (stats[i].AB);
	calcSA = (stats[i].singles + (2*stats[i].doubles) + (3*stats[i].triples) + (4*stats[i].HR)) / (stats[i].AB);
	calcOBA = (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(14) << stats[i].LastName
		     << setw(6) << stats[i].AB
		     << setw(6) << stats[i].singles
		     << setw(6) << stats[i].doubles
		     << setw(6) << stats[i].triples
		     << setw(6) << stats[i].HR
		     << setw(6) << stats[i].walks
		     << 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;
	}
}

How many players would you like to enter data for(1-20): 2

Please enter player #1's first name: 1
Please enter player #1's last name: 1
Please enter the number of at bats for player #1: 1
Please enter the number of singles for player #1: 1
Please enter the number of doubles for player #1: 1
Please enter the number of triples for player #1: 1
Please enter the number of home runs for player #1: 1
Please enter the number of walks for player #1: 1
Please enter player #2's first name: 2
Please enter player #2's last name: 2
Please enter the number of at bats for player #2: 2
Please enter the number of singles for player #2: 2
Please enter the number of doubles for player #2: 2
Please enter the number of triples for player #2: 2
Please enter the number of home runs for player #2: 2
Please enter the number of walks for player #2: 2


Here is the data that you entered, in a nice chart format!

First Name     Last Name    AB    1B    2B    3B    HR    BB    BA    SA   OBA
----------     ---------    --    --    --    --    --    --    --    --   ---
1             1 1.000 1.000 1.000 1.000 1.000 1.000
2             2 2.000 2.000 2.000 2.000 2.000 2.000

Process returned 0 (0x0)   execution time : 10.093 s
Press any key to continue.


You can mess around with the interface to get the formatting fixed...
Last edited on
Thanks chrisname! I will post other problems with this program if they arise.
No problem.
Topic archived. No new replies allowed.