Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C# (http://www.programmingforums.org/forum16.html)
-   -   How to clear console buffers (http://www.programmingforums.org/showthread.php?t=12859)

lucifer Mar 23rd, 2007 2:16 AM

How to clear console buffers
 
Hi
i have to take input from user in console using Console.Read() and Console.Readline() functions but whenever i use Console.Readline() before Console.Read() , the Console.Read() funtion does not execute
plz help
eg

:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            int x=Console.Read();
            string str = Console.ReadLine();
          int z=  Console.Read();
        }
    }
}


Ooble Mar 23rd, 2007 6:22 AM

First of all, Console.Read is used for reading single characters, not tokens. Read the int in using int x = int.Parse(Console.ReadLine()); .

The reason the second Read didn't seem to be executing was that you were entering one on each line: when you typed an integer, the first function read the character... then the next read the rest of the line. The string you typed was read partially by the third, and then dropped when your program finished.

lucifer Mar 24th, 2007 5:56 AM

Quote:

Originally Posted by Ooble (Post 125610)
First of all, Console.Read is used for reading single characters, not tokens. Read the int in using int x = int.Parse(Console.ReadLine()); .

The reason the second Read didn't seem to be executing was that you were entering one on each line: when you typed an integer, the first function read the character... then the next read the rest of the line. The string you typed was read partially by the third, and then dropped when your program finished.

even if i enter single characters still the problem persists
i have also tried to do this
:

char x=(char)Console.Read();
            string str = Console.ReadLine();
            char z = (char)Console.Read();


Ooble Mar 25th, 2007 2:55 PM

Try typing:
:

x Something
m

Bet it'll work.


All times are GMT -5. The time now is 2:13 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC