Click here to Skip to main content
15,891,597 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I created a player class and made a array from that class for my menu driven player system I am trying to use my GetChar method to keep displaying the prompt and reading whatever the user typed on the keyboard until Char.TryParse can convert the input to an char But I keep getting the error Cannot complicity convert type char to string when I call my GetChar method.

Any help would be appreciated

C#
//Creates a player in the tables if the array is not already full and the name is not a duplicate
      static void ProcessCreate(Int32 number, String firstName, String lastName, Int32 goals,
          Int32 assists, Player[] players, ref Int32 playerCount, Int32 MAXPLAYERS)
      {
          //Int32 player = 0;
          if (playerCount < MAXPLAYERS)
          {
            number = IOConsole.GetInt32("\nCreate Player: please enter the player's number");
                         //Console.ReadLine();
              if (GetPlayerIndex(number, firstName, lastName, goals, assists, players, ref playerCount) == -1)
              {
                  firstName = IOConsole.GetChar("\nCreate Player: please enter the player's First Name");
                    //Console.ReadLine();
                    lastName = IOConsole.GetChar("\nCreate Player: please enter the player's Last  Name");
                   //Console.ReadLine();
                   goals = IOConsole.GetInt32("\nCreate Player: please enter the player's goals");
                 Int32.Parse(Console.ReadLine());
                 assists = IOConsole.GetInt32("\nCreate Player: please enter the player's assists");
                   //Console.ReadLine();
                  InsertPlayer(number, firstName, lastName, goals, assists, players, ref playerCount);
                  Console.WriteLine("\n{0,7}   {1,-20}{2, -20}{3,8}{4,8}{5,8}\n", "Number", "First Name", "Last Name", "Goals", " Assists", "Points");
                  for (Int32 player = 0; player < playerCount; player++)
                  Console.WriteLine("{0,7}   {1,-20}{2, -20}{3,8}{4,8}{5,8}",
                players[player].Number, players[player].FirstName, players[player].LastName,
                players[player].Goals, players[player].Assists, players[player].Points());


                  Console.WriteLine();
              }
              else
                  Console.WriteLine("\nCreate Player: the player number already exists");
          }
          else
              Console.WriteLine("\nCreate Player: the player roster is already full");

      }


Here is my get char method
C#
public static char GetChar(String prompt)
        {

            // declare variables
            char validChar;
            while (!Char.TryParse(Console.ReadLine(), out validChar))
            {
                Console.WriteLine("Invalid entery - try again.");
                Console.Write(prompt);
            }
            return validChar;
        }
Posted
Comments
gggustafson 22-Nov-14 16:43pm    
Char.TryParse operates on a string of exactly one character or null. Can you assure yourself that Console.ReadLine isn't returning a string of length greater than 1?

1 solution

Why don't you use Console.ReadKey[^] instead of Console.Read?

C#
public static char GetChar(string prompt)
{

    // declare variables
    char validChar;

    var input = Console.ReadLine();

    while (!Char.TryParse(input.Key.ToString(), out validChar))
    {
        Console.WriteLine("Invalid entry - try again.");

        Console.Write(prompt);
    }

    return validChar;
}
 
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