Hi I'm creating a program that holds three arrays one for the persons last name, one for the points scored and one for the player number, now I've got all the arrays and everything done but when I try to delete a player it deletes the entire list instead of just the information of the player number entered. I am not sure on how to fix this.
Some guidance in the right direction would really help please and thank you
static void ProcessDelete(Int32[] playerNumbers, ref Int32 playerCount, String[] playerLastName, Int32[] playerPoints, Int32 playerIndex)
{
playerNumbers[playerIndex] = 0;
playerLastName[playerIndex] = " ";
playerPoints[playerIndex] = 0;
}
static void DeletePlayer(Int32[] playerNumbers, String[] playerLastName, Int32[] playerPoints, ref Int32 playerCount, Int32 MAXPLAYERS)
{
int player;// Player number to delete
int playerindex;//index of the player number in Array
if (playerCount < MAXPLAYERS)
{
player = GetPositiveInteger("\nDelete Player: please enter the player's number");
playerindex = GetPlayerIndex(player, playerNumbers, playerCount);
if (playerindex != -1)
{
for (playerCount = playerindex; playerCount > 0; playerCount--)
{
Console.WriteLine("\nDelete Player: Number - {0}, Name - {1}, Points - {2}", playerNumbers[playerindex], playerLastName[playerindex], playerPoints[playerindex]);
Console.WriteLine("Succesfully Deleted");
Console.WriteLine();
ProcessDelete(playerNumbers, ref playerCount, playerLastName, playerPoints, playerindex);
}
}
else
Console.WriteLine("\nDelete Player: player not found");
}
else
Console.WriteLine("\nDelete Player: the roster is empty");
}
}
}
I would strongly suggest that you revisit your design entirely:
Player
, where Player
is a new class consisting of a player number, a last name, and a scoreList<T>
instead of arrays - the fact that arrays have a fixed size makes a lot of operations with them much harder. If you want to "delete an entry" you need to create a new array, and copy all the elements you still want from the old array into the new array. That's a pain. In your code you're not really deleting an element at all - you're just setting the score, name and number to default values. Using a List<T>
you can actually remove the element.Now your current code is also quite confusing here:
for (playerCount = playerindex; playerCount > 0; playerCount--)
{
...
ProcessDelete(...);
}
Why are you looping at all? You've worked out the index you want to "delete" - so why aren't you just calling ProcessDelete
once?
// Removed the playerCount parameter. Why is it there at all?
ProcessDelete(playerNumbers, playerLastName, playerPoints, playerindex);
Also, if playerIndex
is 0, you won't loop at all - I suspect you meant the loop condition to be playerCount >= 0
rather than playerCount > 0
.
However, I can't see why the code you've presented would delete the information for more than one player - instead, it's deleting the information for one player multiple times. Either the code you've presented isn't your actual code, or you're not actually getting the result you think you are. (Or I've misread something, of course.)
See more on this question at Stackoverflow