omg, c# console application...

Started by
1 comment, last by Rattenhirn 16 years, 5 months ago
Hi, This is getting on my nerves:

char op;

Console.WriteLine("1 - Create user");
Console.WriteLine("2 - Load user");
Console.Write("> ");
op = (char)Console.Read();

if (op.Equals('1'))
{
  string name
  Console.WriteLine();
  Console.WriteLine("Name -> ");
  name = Console.ReadLine();
}

It shows two options, if the user chooses the 1st, it should print "Name -> " and wait for user input... however, it prints "Name -> " and exits the application... why??? whats missing?? Its been a while since i've done console apps... Thanks
"Through me the road to the city of desolation,Through me the road to sorrows diuturnal,Through me the road among the lost creation."
Advertisement
char op;Console.WriteLine("1 - Create user");Console.WriteLine("2 - Load user");Console.Write("> ");op = (char)Console.Read();if (op.Equals('1')){  string name  Console.WriteLine();  Console.WriteLine("Name -> ");  while ( (name = Console.ReadLine()) != null)  {      Console.WriteLine(name);  }}


:D
Quote:Original post by FreJa
Hi,

This is getting on my nerves:

*** Source Snippet Removed ***

It shows two options, if the user chooses the 1st, it should print "Name -> " and wait for user input... however, it prints "Name -> " and exits the application... why??? whats missing??
Its been a while since i've done console apps...

Thanks


Change the line:
Console.Read()
to:
Console.ReadLine()
and make 'op' a string.

The reason for the behaviour you are seeing is this:
When a user enters text into the console, it is usually nor processed unless he presses enter (CR/LF). Console.Read() however, reads only one character and leaves the rest in the read buffer. The next ReadLine reads the rest of the buffer, which already contains a CR/LF and therefor does not wait for further input.

Hope that helps!



This topic is closed to new replies.

Advertisement