How to fix the errors with characters in C# Employee class I am making. I know how to handle string, int, and double. The char.Parse doesn’t cooperate.
public static string AskForLastName()
{
string inValue;
Console.Write("Enter last name: ");
inValue = Console.ReadLine();
return inValue; //works fine
}
public static char AskForGender()
{
char inValue;
Console.Write("Enter gender: ");
inValue = Console.ReadLine();//error here for some reason
return (char.Parse(inValue));//error here for some reason
}
public static int AskForNoDependendents()
{
string inValue;
Console.Write("Enter the dependents: ");
inValue = Console.ReadLine();
return (int.Parse(inValue));//works fine
}
public static double AskForAnnualSalary()
{
string inValue;
Console.Write("Enter annual salary: ");
inValue = Console.ReadLine();
return (double.Parse(inValue));//works fine
}
Description
Console.ReadLine returns a string. You can use
string[]to get the first character of your string. But you have to make sure that the user inserts at least one character.Sample
Update
My sample covers now that the user inputs
morw.Update
It is homework so…
If your Teacher ask "But what happens if the user inputs a
Minstead ofm? You should say "It asks the user again".If your Teacher asks then "How to make it possible to accept
Mandm? You should say i can make it case insensitive.Sample
You should use i can use .NET’s
.ToUpper()method.