Results 1 to 7 of 7

Thread: How to terminate a ReadLine()?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    How to terminate a ReadLine()?

    I'm new to C# and learning to write a Console app. I'm catching on fairly quickly, however, I'm trying to create a little login app where the user is prompted to enter "Username:". When I enter a value for Username and press the enter key, the cursor just goes to the beginning of the line. What am I doing wrong?

    Thanks,

    Code:
            static void NewUser()
            {
                Console.WriteLine("\n\n");
                Console.Write("Enter Username: ");
    
                do
                {
    
                    username = Console.ReadLine();
    
                } while (username != null);
    
                Console.Write("Enter Password: ");
    
                do
                {
    
                    password = Console.ReadLine();
    
                } while (password != null);
            }
    Blake

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: How to terminate a ReadLine()?

    Your conditions are the wrong. You want to keep prompting the user as long as there is no value, so these:
    Code:
            static void NewUser()
            {
                Console.WriteLine("\n\n");
                Console.Write("Enter Username: ");
    
                do
                {
    
                    username = Console.ReadLine();
    
                } while (username != null);
    
                Console.Write("Enter Password: ");
    
                do
                {
    
                    password = Console.ReadLine();
    
                } while (password != null);
            }
    should be like this:
    Code:
            static void NewUser()
            {
                Console.WriteLine("\n\n");
                Console.Write("Enter Username: ");
    
                do
                {
    
                    username = Console.ReadLine();
    
                } while (username == null);
    
                Console.Write("Enter Password: ");
    
                do
                {
    
                    password = Console.ReadLine();
    
                } while (password == null);
            }
    Actually, comparing to null is useless regardless, because ReadLine will never return null. If the user enters no text then it will return an empty String, so that code should really be:
    Code:
            static void NewUser()
            {
                Console.WriteLine("\n\n");
                Console.Write("Enter Username: ");
    
                do
                {
    
                    username = Console.ReadLine();
    
                } while (username == string.Empty);
    
                Console.Write("Enter Password: ");
    
                do
                {
    
                    password = Console.ReadLine();
    
                } while (password == string.Empty);
            }

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: How to terminate a ReadLine()?

    jmc,

    Thanks for your answer. It works, but I find that when I enter a value for the Username...I have to hit the <enter> key twice in order to get prompted to enter the Password.
    Blake

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: How to terminate a ReadLine()?

    I just used this exact code and it worked fine for me:
    csharp Code:
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6.  
    7. namespace ConsoleApp1
    8. {
    9.     class Program
    10.     {
    11.         static void Main(string[] args)
    12.         {
    13.             NewUser();
    14.             Console.ReadLine();
    15.         }
    16.  
    17.         private static string username;
    18.         private static string password;
    19.  
    20.         static void NewUser()
    21.         {
    22.             Console.WriteLine("\n\n");
    23.             Console.Write("Enter Username: ");
    24.  
    25.             do
    26.             {
    27.  
    28.                 username = Console.ReadLine();
    29.  
    30.             } while (username == string.Empty);
    31.  
    32.             Console.Write("Enter Password: ");
    33.  
    34.             do
    35.             {
    36.  
    37.                 password = Console.ReadLine();
    38.  
    39.             } while (password == string.Empty);
    40.         }
    41.     }
    42. }

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: How to terminate a ReadLine()?

    I copied that same code into my app and it still didn't work. I have included all of the code (which is pretty little). Perhaps there is something that changing the ReadLine in how it acts...just a guess.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
    
            {
                ConsoleKeyInfo keyInput;
                // Prevent example from ending if CTL+C is pressed.
                Console.TreatControlCAsInput = true;
    
                Console.WriteLine("\n");
                Console.WriteLine("                             Select an Option\n");
                Console.Write("   (N)ew User or (L)ogin or (T)ransaction or Transaction (H)istory or Esc: ");
    
                keyInput = Console.ReadKey(true);
    
                switch (keyInput.Key)
                {
                    case ConsoleKey.L:
                        LogIn();
                        break;
                    case ConsoleKey.N:
                        EnterUserInfo();
                        break;
                    default:
                        break;
                }
            }
    
            static string username;
            static string password;
    
            //
            //=============================================================================================
            //
             static void EnterUserInfo()
            {
                Console.Write("\n\n" + "   Enter Username: ");
    
                do
                {
                    username = Console.ReadLine();
    
                } while (username == string.Empty);
    
                Console.Write("\n" + "   Enter Password: ");
    
                do
                {
                    password = Console.ReadLine();
    
                } while (password == string.Empty);
            }
            //
            //=============================================================================================
            //
            static void LogIn()
            {
    
            }
            //
            //=============================================================================================
            //
            static void TranslateInput()
            {
    
            }
        }
    }
    Blake

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: How to terminate a ReadLine()?

    I tried your code and it behaved as you said. I commented out this line:
    csharp Code:
    1. Console.TreatControlCAsInput = true;
    and it worked as expected.

    https://stackoverflow.com/questions/...-is-this-a-bug

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: How to terminate a ReadLine()?

    Thanks jmc...appreciate the help!
    Blake

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width