Click here to Skip to main content
15,917,320 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I created a player class and I am trying to use my player class to update a player for my menu driven player program. I want to be able to have the user enter a player number and ask if they want to update the player assists and goals then display the updated player information and if the player doesn't exist then it would display the message player does not exist. For some reason when I enter a player number of one of the players I created when it gets to the edit goals Y/N and I chose Y it says the player doesn't exist instead of letting me edit the goals.it still says that the player does not exist even though it was just created. I am not sure on how to fix this.

Any help would be appreciated.

C#
static void ProcessUpdate(Int32 number, String firstName, String lastName, Int32 goals,
            Int32 assists, Player[] players, ref Int32 playerCount, Int32 MAXPLAYERS)
        {
           
            int playerindex;//index of the player number in Array
            string answer, answer2;
            

            if (playerCount < MAXPLAYERS )
            {
                Console.WriteLine("\nUpdate Player: please enter the player's number");
                number = Int32.Parse(Console.ReadLine());
                playerindex = GetPlayerIndex(number, firstName, lastName, goals, assists, players, ref playerCount);
                if (playerindex != -1)
                {
                    
                    Console.WriteLine("\nUpdate Player: Player {0} currently has {1} goals and {3} assists", players[playerindex].Number,
                            players[playerindex].Goals, players[playerindex].Assists, players[playerindex].Points());
                    Console.ReadLine();
                 
                    Console.WriteLine("\nEdit Goals?: Y/N");
                    answer = Console.ReadLine();
                    if (answer.Equals('Y'))
                    {
                        Console.WriteLine("\nUpdate Player: please enter the player's new Goal total");
                        goals = Int32.Parse(Console.ReadLine());
                        Console.WriteLine("\nEdit Assists?: Y/N");
                        answer2 = Console.ReadLine();
                        if (answer2.Equals('Y'))
                        {

                            Console.WriteLine("\nUpdate Player: please enter the player's new Assists total");
                            assists = Int32.Parse(Console.ReadLine());
                            /*
                            players[playerindex].LastName = lastName;
                            players[playerindex].FirstName = firstName;
                            players[playerindex].Goals = goals;
                            players[playerindex].Assists = assists;*/
                            Console.WriteLine("\n{0,7}   {1,-20}{2, -20}{3,8}{4,8}{5,8}\n", "Number", "First Name", "Last Name", "Goals", " Assists", "Points");
                            Console.WriteLine("{0,7}   {1,-20}{2, -20}{3,8}{4,8}{5,8}",
                             players[playerindex].Number, players[playerindex].FirstName, players[playerindex].LastName,
                             players[playerindex].Goals, players[playerindex].Assists, players[playerindex].Points());
                            Console.WriteLine("Sucessfully Updated!");
                            Console.WriteLine();
                        }
                    }

                    else
                        Console.WriteLine("\nUpdate Player: the player number does not exists");
                }
                else
                    Console.WriteLine("\nUpdate Player: the player does not exist in the roster");
            }
        }
Posted
Comments
PIEBALDconsult 17-Nov-14 20:58pm    
answer.Equals('Y')

As Nayan said, you'll need a string literal , not a character.
Plus, this is C#, you'll rarely need to use Equals.
Having said that, you might want to try System.StringComparer.InvariantCultureIgnoreCase.Equals

1 solution

You are comparing a string value to a char value (you declared answer as string and then equalling it with a char value)
Also, this comparison is case-sensitive. Please make sure you are typing 'Y' and not 'y' (or whatever you are expecting)

The below should work;
answer.Equals("Y")


Please mark the answer as correct if it helps you.

Thanks.
 
Share this answer
 

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