Proliges
Proliges

Reputation: 361

.NET Console.Readline doesn't read the first line entered

I wrote a console application but the first input line is always ignored.

static void Main(string[] args)
{
        try
        {
            string line;

            while ((line = Console.ReadLine()) != "exit")
            {
                string input = Console.ReadLine().ToString();
                Console.WriteLine("I wrote: " + input);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
}

When I run this, the result is as follows:

enter image description here

Any ideas why this is happening? I already tried writing a line before the first line, but the same problem occurs.

Upvotes: 0

Views: 401

Answers (1)

Marco Salerno
Marco Salerno

Reputation: 5203

This way:

static void Main(string[] args)
{
    try
    {
        string line;
        while ((line = Console.ReadLine()) != "exit")
        {
            Console.WriteLine("I writed: " + line);
            string input = Console.ReadLine().ToString();
            Console.WriteLine("I writed: " + input);
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

You were printing only input and never line


Maybe you wanted to do it this way:

static void Main(string[] args)
{
    try
    {
        string input;
        while ((input = Console.ReadLine()) != "exit")
        {
            Console.WriteLine("I writed: " + input);
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

Upvotes: 2

Related Questions