9 Replies - 661 Views - Last Post: 15 February 2017 - 12:26 PM Rate Topic: -----

#1 jjpit   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-February 17

trouble with a sorting loop and displaying the result

Posted 14 February 2017 - 06:11 PM

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 and the wrong name. here is my code

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



and here is my display

Quote

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 . . .


and this is the text file contents i am using

Quote

Andrus 574 148
Beltre 543 151
Choo 531 146
Fielder 592 182
Odor 405 108


Is This A Good Question/Topic? 0
  • +

Replies To: trouble with a sorting loop and displaying the result

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3826
  • View blog
  • Posts: 13,947
  • Joined: 08-August 08

Re: trouble with a sorting loop and displaying the result

Posted 14 February 2017 - 06:43 PM

You don't have an array (or better yet, a vector) of players, so you can't sort one! What you do have is a memory leak. You create a new object, and when you do, you lose track of the last one you've created!

Read up on vectors.
Was This Post Helpful? 0
  • +
  • -

#3 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: trouble with a sorting loop and displaying the result

Posted 14 February 2017 - 08:40 PM

View PostCTphpnwb, on 14 February 2017 - 09:43 PM, said:

You don't have an array (or better yet, a vector) of players, so you can't sort one! What you do have is a memory leak. You create a new object, and when you do, you lose track of the last one you've created!

Why ?

@jjpit:
Think about what result you should expect when you assign a decimal fraction to an int (in your Max function).

Then, why are you sending a name to Max? Which name should you be printing there?
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3826
  • View blog
  • Posts: 13,947
  • Joined: 08-August 08

Re: trouble with a sorting loop and displaying the result

Posted 14 February 2017 - 08:54 PM

Wow. I shouldn't try to answer when tired. I actually entered that code into my IDE, and still didn't see the array!
Was This Post Helpful? 0
  • +
  • -

#5 jjpit   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-February 17

Re: trouble with a sorting loop and displaying the result

Posted 14 February 2017 - 09:03 PM

View Postr.stiltskin, on 14 February 2017 - 08:40 PM, said:

View PostCTphpnwb, on 14 February 2017 - 09:43 PM, said:

You don't have an array (or better yet, a vector) of players, so you can't sort one! What you do have is a memory leak. You create a new object, and when you do, you lose track of the last one you've created!

Why ?

@jjpit:
Think about what result you should expect when you assign a decimal fraction to an int (in your Max function).

Then, why are you sending a name to Max? Which name should you be printing there?


thank you for your response that first part cleared up my train of thought.
I was able to get the correct highest BA output with this
double maxHit = -1;
.

About the name i sort of understand what you are talking about but i am still trying to fix that.


View PostCTphpnwb, on 14 February 2017 - 08:54 PM, said:

Wow. I shouldn't try to answer when tired. I actually entered that code into my IDE, and still didn't see the array!


no worries.
Was This Post Helpful? 0
  • +
  • -

#6 jjpit   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-February 17

Re: trouble with a sorting loop and displaying the result

Posted 15 February 2017 - 11:10 AM

so with help i was able to solve my issue with the name and understand why i was wrong. i put this in the loop
			playerName = bA[index]->name;

which helped me output the proper "playerName"

void Max(Player * bA[], string &playerName)

{
	
	double maxHit = -1;

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

}


Was This Post Helpful? 0
  • +
  • -

#7 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: trouble with a sorting loop and displaying the result

Posted 15 February 2017 - 11:47 AM

Good, but you still don't seem to fully understand the point I was making.

In your main function you declared a string called playerName which you were using only to input data from the input file. When you send that string by reference to your Max function, you're enabling the other function to make changes to the main function's string. (You can test that by adding a cout statement at the end of main() after the call to Max to see what happened to playerName there.)

That's not necessarily bad if it's what you intended, but in this case there's no reason for Max to be making changes to a variable that "belongs" to main(). If a function doesn't need access to another function's variables, it shouldn't have access. You can eliminate the second parameter from Max and simply declare another string in Max to temporarily store the name of the player with the highest average while the function is in scope.

[Actually, a similar issue also exists with the array, in that the Max function can not only read but also modify its data. There is a way to avoid that using something called const but you don't have to worry about that now -- you'll learn about it later in your programming studies.]
Was This Post Helpful? 1
  • +
  • -

#8 jjpit   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-February 17

Re: trouble with a sorting loop and displaying the result

Posted 15 February 2017 - 12:06 PM

View Postr.stiltskin, on 15 February 2017 - 11:47 AM, said:

Good, but you still don't seem to fully understand the point I was making.

In your main function you declared a string called playerName which you were using only to input data from the input file. When you send that string by reference to your Max function, you're enabling the other function to make changes to the main function's string. (You can test that by adding a cout statement at the end of main() after the call to Max to see what happened to playerName there.)

That's not necessarily bad if it's what you intended, but in this case there's no reason for Max to be making changes to a variable that "belongs" to main(). If a function doesn't need access to another function's variables, it shouldn't have access. You can eliminate the second parameter from Max and simply declare another string in Max to temporarily store the name of the player with the highest average while the function is in scope.

[Actually, a similar issue also exists with the array, in that the Max function can not only read but also modify its data. There is a way to avoid that using something called const but you don't have to worry about that now -- you'll learn about it later in your programming studies.]




so if i understand right. I need to make a string separate from main() for Max so that I am not changing the contents of the string from main(). which would be a good programming practice correct?


Thank you for taking the time to explain this.
Was This Post Helpful? 0
  • +
  • -

#9 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: trouble with a sorting loop and displaying the result

Posted 15 February 2017 - 12:16 PM

As a general rule, yes. But I just noticed this:

Quote

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.


So if your teacher gave you this line:
void Max(Player * bA[], string &playerName)

and expects you to use it as-is, then leave it the way you have it and just file my suggestion away for the future.

Otherwise, you can change max to
void Max(Player * bA[])
{
    //  your code ...

}

and declare a separate string inside Max which will exist only within that function.
Was This Post Helpful? 0
  • +
  • -

#10 jjpit   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-February 17

Re: trouble with a sorting loop and displaying the result

Posted 15 February 2017 - 12:26 PM

the Max line of code
void Max(Player * bA[], string &playerName) 


was written by me, so now i know what to do in the future and use good programming practice. Thank you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1