Trouble with sorting loop, all zeros

i have this program that i was given the majority of the code and tasked with finding the batting avg of the players and displaying who has the highest batting avg. i was able to find the BA and display it with it's respective player. My problem comes with my function i used to sort and display the highest one, it displays all zeros. also i am having problems with displaying the proper name which is something i feel like i am close to figuring out but i am lost on the all zeros. Thank you in advance for any help you can provide.
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
/*
COSC 1437 Activity 3
Baseball Statistics
editing in code done by Jacob Pitoniak
*/

#include <iostream>
#include <string>
#include <fstream>

using namespace std;





// Structure to hold batting statistics
struct  Player
{
	string name;
	int atBats;
	int hits;
	double AVG;
};

// func to find and display the highest BA.
void Max(Player * baseballArray[], string &playerName)

{
	
	int maxHit = -1;

	for (int index = 0; index < 5; index++)
	{
		if ((maxHit < baseballArray[index]->AVG))
		{
			maxHit = baseballArray[index]->AVG;
			cout << playerName << " Has the highest BA =" << maxHit << endl;
		}
		
	}
	
}


int main()
{
	ifstream statFile;
	string playerName;
	double bats, hits, AVG;
	Player * baseballArray[100];
	Player * aPlayer;
	int numPlayers = 0;
	
	


	statFile.open("stats.txt");

	// Continue reading until the end of file
	while (statFile >> playerName)
	{
		statFile >> bats >> hits;

		// Dynamically create a new Player
		aPlayer = new Player;
		aPlayer->name = playerName;
		aPlayer->atBats = bats;
		aPlayer->hits = hits;
		aPlayer->AVG = (hits / bats);


		// Store the pointer to the Player struct in the array
		baseballArray[numPlayers++] = aPlayer;

		// Display this information
		cout  << aPlayer->name << endl << "========" << endl << "AB = " << aPlayer->atBats << endl
			<< "hits = " << aPlayer->hits << endl <<  "BA = " << (hits/bats) << endl << endl;


	}



	// For Activity 3: Display which player has the highest batting average.
	// Display the player's name and average

	Max(baseballArray, playerName);

}
 
A number of problems:

Line 88: You're passing in the name of the last player read from the file.

Line 38: You're displaying whatever name was passed in, which happens to be the last player read from the file.

Line 38: you want to move the baseballarray[index]->name to player name. You don;t want to display the name of the player with the highest BA until you've completed the loop.

Line 70: You're doing integer arithmetic.

Line 89: You have a memory leak. Every thing that was allocated via new should be released via delete.

edit: Removed comment regarding integer arithmetic on line 70.
Last edited on
I thought that too but 70 is in main, and main says
double bats, hits, AVG;

nvm
Last edited on
so for line 70 since i have it in main and all those are set to double that shouldn't pose a problem right?

this the text file i am using for my numbers
Andrus 574 148
Beltre 543 151
Choo 531 146
Fielder 592 182
Odor 405 108

and this is my output

Andrus
========
AB = 574
hits = 148
BA = 0.25784

Beltre
========
AB = 543
hits = 151
BA = 0.278085

Choo
========
AB = 531
hits = 146
BA = 0.274953

Fielder
========
AB = 592
hits = 182
BA = 0.307432

Odor
========
AB = 405
hits = 108
BA = 0.266667

Odor Has the highest BA =0
Press any key to continue . . .



so i am getting the right results up until the last line
Odor Has the highest BA =0



I am still confused as to what i am doing wrong to end up with 0 because if i change
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void Max(Player * bA[], string &playerName)

{

	int maxHit = -1;

	for (int index = 0; index < 5; index++)
	{
		if ((maxHit < bA[index]->AVG))
		{
			maxHit = bA[index]->AVG;
			
		}

	}
	cout << playerName << " Has the highest BA =" << maxHit << endl;

}
to
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void Max(Player * bA[], string &playerName)

{

	int maxHit = -1;

	for (int index = 0; index < 5; index++)
	{
		if ((maxHit < bA[index]->hits))
		{
			maxHit = bA[index]->hits;
			
		}

	}
	cout << playerName << " Has the highest BA =" << maxHit << endl;

}

I get the right display for the highest amount of hits which is
182
i am sure it is something simple but I am just not processing it in my mind.

Also about the right name could you please elaborate?

below is how i've modified my code since the OP


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
#include <iostream>
#include <string>
#include <fstream>

using namespace std;





// Structure to hold batting statistics
struct  Player
{
	string name;
	int atBats;
	int hits;
	double AVG;
};

// func to find and display the highest BA.
void Max(Player * bA[], string &playerName)

{

	int maxHit = -1;

	for (int index = 0; index < 5; index++)
	{
		if ((maxHit < bA[index]->AVG))
		{
			maxHit = bA[index]->AVG;
			
		}

	}
	cout << playerName << " Has the highest BA =" << maxHit << endl;

}


int main()
{
	ifstream statFile;
	string playerName;
	double bats, hits,AVG;
	Player * baseballArray[100];
	Player * aPlayer;
	int numPlayers = 0;



	statFile.open("stats.txt");

	// Continue reading until the end of file
	while (statFile >> playerName)
	{
		statFile >> bats >> hits;

		// Dynamically create a new Player
		aPlayer = new Player;
		aPlayer->name = playerName;
		aPlayer->atBats = bats;
		aPlayer->hits = hits;
		AVG = (hits / bats);
		aPlayer->AVG = AVG;


		// Store the pointer to the Player struct in the array
		baseballArray[numPlayers++] = aPlayer;

		// Display this information
		cout << aPlayer->name << endl << "========" << endl << "AB = " << aPlayer->atBats << endl
			<< "hits = " << aPlayer->hits << endl << "BA = " << (hits / bats) << endl << endl;

	}

	// For Activity 3: Display which player has the highest batting average.
	// Display the player's name and average
	Max(baseballArray, playerName);
	
}
why is the index in your Max() function only going up to 5? and why do you have a cout after every next larger batting average?


i did that because i was counting through 5 players and the cout i tried to fix ^^^ to display after the loop but i still am running into the same problem with the wrong name and it displaying 0
idk but make a constructor for your class to just take in those values because a lot of them are named the same so it's a bit confusing to read through

also don't increment the index inside of accessing the array, it could cause problems depending on the machine
basballArray[numPlayers++]

EDIT: I think I found your problem

line 79: 'playerName' is just whoever was the last player read in from the file. There's nowhere that you checked for playerName to be the name of the player that had the highest batting average.

line 25&31: 'maxHit' is an int while 'AVG' is a double that will always fall between 0 and 1 so when casted to an int it will just drop the decimal part, setting it to 0
Last edited on
so i was able to figure that one out (line 25&31) last night and was able to fix it with double maxHit = -1; i know understand my issue with line 79 but i am kind of lost on how to fix it and I'am still trying to figure that out.
At line 32, you need to set the playerName argument to the player that has the highest BA so far as you iterate through the loop.

1
2
  //  At line 32: 
  playerName = bA[index]->name;  // Set argument to name of player with highest BA so far 

Thank you for you help that helped explain it to me and fixed my problem.
Topic archived. No new replies allowed.