Jump to content
  • Advertisement
Sign in to follow this  

omg, c# console application...

This topic is 3859 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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!



Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!