user4202953
user4202953

Reputation: 49

Why do I need 2 Console.ReadLine(); to pause the console?

I'm just learning c#, and I like to understand everything before I move on.

The problem I'm having is I need 2 Console.ReadLine(); to pause the console. If I just use 1, the program ends after the input. So why does it need 2 readline methods instead of? Any ideas?

Please note in my code, I've commented out 1 of the readline methods, the way I want my program to work but it doesn't. However removing the comments allows the program to work, but I don't understand why.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CoinFlip
{
    class Program
    {
        static void Main(string[] args)
        {

            Random rng = new Random();
            Console.WriteLine(@"

This program will allow you to guess heads or tails on a coin flip.

Please enter h for heads, or t for tails and press Enter: ");

            char userGuess = (char)Console.Read();
            int coin = rng.Next(0,2);

            Console.WriteLine("Coin is {0}\n\n", coin);


            if (coin == 0 && (userGuess == 'h' || userGuess == 'H'))
            {

                Console.WriteLine("It's heads! You win!");

            }
            else if (coin == 1 && (userGuess == 't' || userGuess == 'T'))
            {
                Console.WriteLine("It's tails! You win!");

            }
            else if (userGuess != 't' && userGuess != 'T' && userGuess != 'h' && userGuess != 'H') 
            { 
                Console.WriteLine("You didn't enter a valid letter"); 
            }

            else
            {

                if (coin == 0) { Console.WriteLine("You lose mofo. The coin was heads!"); }
                if (coin == 1) { Console.WriteLine("You lose mofo. The coin was tails!"); }

            }
            Console.ReadLine();
            //Console.ReadLine();
        }
    }
}

Upvotes: 2

Views: 1471

Answers (3)

Debopam Chanda
Debopam Chanda

Reputation: 161

The first Console.ReadLine() is consumed by the Enter key and hence the program ends.
Try this instead of Console.Read()

    var consoleKeyInfo = Console.ReadKey();
    var userGuess = consoleKeyInfo.KeyChar;

Upvotes: 0

Servy
Servy

Reputation: 203820

The short answer, don't use Console.Read. It can't read anything until you submit a line of text, but it only reads the first character of that line of text, leaving the rest of that line for further console input, such as , say a call to Console.ReadLine. Use Console.ReadKey instead of Console.Read to read a single character.

Upvotes: 3

Jon Skeet
Jon Skeet

Reputation: 1500514

You're using Console.Read(), which reads a single character after the user has hit return. However, it only consumes that single character - which means the rest of the line (even if it's empty) is still waiting to be consumed... which Console.ReadLine() is doing.

The simplest fix to this is to use Console.ReadLine() earlier too:

string userGuess = Console.ReadLine();

.. then maybe check that the guess was a single character, or just change all your character literals (e.g. 't') to string literals (e.g. "t").

(Or use Console.ReadKey() as Servy suggested. It depends on whether you want the user to hit return or not.)

Upvotes: 4

Related Questions