Results 1 to 3 of 3

Thread: Console.Read() - Please help C# newbie

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    Console.Read() - Please help C# newbie

    Hi,
    I want to create menu driven condole application
    (1. Do this, 2. Do that, 3. Exit)

    I have a stranger problem with Console.Read()
    I'm reading a character in a loop using Console.Read()
    and displaying it using Console.Write()

    VB Code:
    1. public static void Test()
    2. {
    3.   int i;
    4.   char c;
    5.  
    6.   while (true)
    7.   {
    8.     i = Console.Read ();
    9.  
    10.     c = (char) i;
    11.     Console.WriteLine ("Echo: {0}", c);
    12.   }
    13. }
    When I press 5<Enter>, then 9<Enter> I should be getting this:
    VB Code:
    1. 5
    2. Echo: 5
    3. 9
    4. Echo: 9
    Instead, I'm getting this:
    VB Code:
    1. 5
    2. Echo: 5
    3. Echo:        // remembers char 13 - line feed
    4. Echo:        // remembers char 10 - new line
    5.  
    6. 9
    7. Echo: 9
    8. Echo:        // remembers char 13 - line feed
    9. Echo:        // remembers char 10 - new line

    After stepping through the code I realized that
    the Console.Read() remembers the enter key as well (char 13 and 10)
    that's why the two empty echos after the actual number.

    How can I solve this? Flash the buffer?
    I need to use Console.Read() for reading single digits.

    Any help appreciated.
    Thanks

    Tomexx.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Console.Read() - Please help C# newbie

    Try Console.ReadLine()

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    Re: Console.Read() - Please help C# newbie

    Yeah,
    I wanted to do this with the .Read() but I give up (spend too much time on it although I learned few things doing so).

    I did my menu using ReadLine() instead and it works well.
    Thanks

    Tomexx.

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