Odd results after Console.read() input
In the following little "learning" program of mine, I am making a simple class constructor program. The place I am having problems is with Console.Read(). If you run this program and go into debug, and once you come upon the conosle.read() the variable selection takes on some funky results. Selection is first initialized at 0, but as soon as you input for selection the number jumps way up. When I type in 1 at the menu, the variable takes on 49, 2 takes on 50, and 3 takes on 51. Obviously I am very new to this, and i'm guessing I am doing something wrong with Console.Read() but I don't know.
class Menu
{
public void MenuOptions()
{
int selection = 0;
while (selection != 3)
{
Console.WriteLine("Please select what you want to do....");
Console.WriteLine("\n1. Add new Employee\n2. List Employee's\n3. Quit");
selection = Console.Read();
if (selection == 1)
{
Console.WriteLine("Please enter int he following information");
Console.WriteLine("Enter the Name please\t\t");
string name = "hello";
Console.WriteLine("Please enter the Age");
int age = Console.Read();
int startingPay = Console.Read();
Employee newEmployee = new Employee(age, startingPay, name);
Console.WriteLine("Thank you. New Employee created");
}
}
}
}
class Employee
{
private int age;
private int startingPay;
private string name;
public Employee(int age, int startingPay, string name)
{
//Console.WriteLine();
this.age = age;
this.name = name;
this.startingPay = startingPay;
}
}
class Program
{
static void Main(string[] args)
{
Menu menu = new Menu();
menu.MenuOptions();
}
}
}
[1861 byte] By [
patio87] at [2007-12-25]
Console.Read returns the code of the character not the character itself. So when you press "1" you get the 49 which the code for the "1" character. You should use Console.ReadKey() method instead:
ConsoleKeyInfo keyInfo; // instead of int selection = 0;
while (keyInfo.KeyChar != '3') // instead of while (selection != 3)
{
Console.WriteLine("Please select what you want to do....");
Console.WriteLine("\n1. Add new Employee\n2. List Employee's\n3. Quit"); keyInfo = Console.ReadKey(); // instead of selecion = Console.Read();
if (keyInfo.KeyChar == '1') // instead of if (selection == 1)
{
...
}
}
You also have the same problem when reading the age and startingPay
int age = Console.Read();
int startingPay = Console.Read();
Console just returns characters codes (Console.Read) or strings (Console.ReadLine). If you want an int you will need to parse the entered text:
string ageText = Console.ReadLine();
int age;
if (! int.TryParse(ageText, out age))
{
Console.WriteLine("{0} is not a number", ageText);
}
Here is one solution:
selection = Convert.ToInt32(Convert.ToChar(Console.ReadLine()).ToString());
Numbers that you got are ascii values of characters that you enter, 1 = '49', because Read return char value.
Better from prevous is this:
if (int.TryParse(Console.ReadKey().KeyChar.ToString(), out selection) && selection == 1)
ReadKey reacts immediatelly after key is pressed
Please be aware of difference between Read and ReadLine. If in first soulution instead of ReadLine we put just Read then all other Read lines in if region will be jumped and execution will stop on next ReadLine if there is one.
Thanks Mike, so I guess I have two ways to do it, either with the consolekeyinfo, or readline().
Thanks boban.s, you're reply kinda went over my head though.