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();
}
omg, c# console application...
Hi,
This is getting on my nerves:
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
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
Popular Topics
Advertisement