Click here to Skip to main content
14,699,089 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I created a program that holds three arrays one for the persons last name, one for the points scored and one for the player number. I made a method to delete a player and my for loop is suppose to shift the elements up 1 and delete 1 from playerCount but when I enter a players number to delete all the information for that player it clears the entire list when I just want the player entered to be removed. I also need to use arrays to do this.

Any help would be appreciated please and thank you.


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 = MAXPLAYERS - 1; 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");
        }
        
    }
}
Posted
Comments
Sergey Alexandrovich Kryukov 25-Nov-14 15:34pm
   
Why tells you that you need to use arrays?
—SA
Maciej Los 25-Nov-14 15:37pm
   
Why arrays? Wouldn't be better to use List of custom class?
PIEBALDconsult 25-Nov-14 15:42pm
   
Yes, he would. As has been pointed out weeks ago.
PIEBALDconsult 25-Nov-14 15:40pm
   
You're _still_ stuck on that after two weeks? The problem is that you should not use arrays to do that!

Please read all comments to the question.

Strongly recommend you to forget about separate array for each object: persons, points, etc.
Have a look at example. I write it just for fun and to show you more elegant solution:
void Main()
{
    //initial list of players
    List<Player> players = new List<Player>{
            new Player("Dorothy", 59),
            new Player("Anthony", 52),
            new Player("Frederic", 25),
            new Player("Timothy", 15),
            new Player("Rebeca", 55),};
    Console.WriteLine("Initial count of players: {0}", players.Count());
    //add another user
    Player p = new Player("Maciej", 0);
    players.Add(p);
    Console.WriteLine("After addition: {0}", players.Count());
    //remove newly added
    players.Remove(p);
    Console.WriteLine("After deletion : {0}", players.Count());
    Console.WriteLine();

    //Linq query to get the list of players and...
    var qry = from pl in players
        select new{Name=pl.PlayerName, Points=pl.Points, Total=(from pp in players select pp.Points).Sum()};
    foreach(var pp in qry)
    {
        Console.WriteLine("{0} reached {1} points; total: {2}", pp.Name, pp.Points, pp.Total);
    }

}

// Definition of class
public class Player
{
    private string playername = string.Empty;
    private int ppoints = 0;

    public Player(string sName, int iPoints)
    {
        playername = sName;
        ppoints = iPoints;
    }

    public string PlayerName
    {
        get
        {
            return playername;
        }
        set
        {
            playername = value;
        }
    }

    public int Points
    {
        get
        {
            return ppoints;
        }
        set
        {
            ppoints = value;
        }
    }

}


Result:
Initial count of players: 5
After addition: 6
After deletion : 5

Dorothy reached 59 points; total: 206
Anthony reached 52 points; total: 206
Frederic reached 25 points; total: 206
Timothy reached 15 points; total: 206
Rebeca reached 55 points; total: 206


Note: it's not complete sample, but an idea!

As you can see, it's much, much easier to manage one list rather than 3 arrays.
   
First of all, such design of the code is really ugly. You should not have separate arrays for username, number and scores. Isn't that obvious that those data elements make an integral object. So create a use class or structure, and use a list or a collection of instances of such class or structure.

Furthermore, if you want to remove any elements, it makes arrays prohibitively bad. Your "I also need to use arrays" is perfectly baseless. If you had to remove an element from an array, all methods doing that essentially create a brand-new instance of an array and copy all data from the source array onto the new one. This is a big no-go thing.

Instead, you will need some collection with removal functionality, a list, a dictionary, or something else. Please see what you can use: http://msdn.microsoft.com/en-us/library/system.collections.generic.aspx[^].

Note that I don't say "forget the arrays". In particular, you can use methods returning arrays from your collections, such as this one for the generic list: http://msdn.microsoft.com/en-us/library/x303t819.aspx[^].

—SA
   
Comments
Maciej Los 25-Nov-14 16:13pm
   
Agree, +5!
Sergey Alexandrovich Kryukov 25-Nov-14 19:31pm
   
Thank you, Maciej.
—SA
As others have mentions, unless this is a school assignment you likely should be putting all player information into one class or structure then using one of the existing C# data structures, eg List, to store and manipulate the players.

You are on the right track when you say "method to delete a player and my for loop is suppose to shift the elements up". You do want to shift the elements down and decrement your player counter, but avoid actually resizing the array.

class PlayerTest
       {
       static int maxPlayer = 20;
       public int currentPlayers;
       int[] playerNumbers = new int[maxPlayer];
       int[] playerScores = new int[maxPlayer];
       string[] playerNames = new string[maxPlayer];



       public void InitPlayers()
           {
           int i;

           currentPlayers = maxPlayer;
           // init the data
           for (i = 0; i < maxPlayer; i++)
               {
               playerNumbers[i] = i;
               playerScores[i] = i * 100; ;
               playerNames[i] = "Player " + i.ToString();
               }
           }

       public void ShowPlayers()
           {
           int i;

           for (i = 0; i < currentPlayers; i++)
               {
               Console.WriteLine(playerNames[i] + "  Score: " + playerScores[i].ToString() + "   " + playerNumbers[i].ToString());
               }
           }

       public void DeletePlayer(int index)
           {
           int i;

           for (i = index; i < (currentPlayers - 1); i++)
               {
               playerNames[i] = playerNames[i + 1];
               playerScores[i] = playerScores[i + 1];
               playerNumbers[i] = playerNumbers[i + 1];
               }

           currentPlayers--;
           }

       public void Wait()
           {
           Console.WriteLine("Press a key.");
           Console.ReadKey();
           }
       }
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900