Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm learning C# for one of my classes and for my assignment I need to get user input from the console.

In my program I have:

choice = (char)System.Console.Read();

Later on in the program I use

if (System.Console.ReadLine() == "y")

to get input from the user.

The second statement gets skipped when I run the program. I'm guessing that the System.Console.Read() is leaving a newline in the stream. In C/C++, there's fflush() and cin.ignore(). What is the equivalent function in C#?

I know that it's probably easier for me to use ReadLine() or ReadKey(), but I'm just curious as to how to use Read() with newlines

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
388 views
Welcome To Ask or Share your Answers For Others

1 Answer

This is my equivalent for fflush:

while (Console.KeyAvailable)
    Console.ReadKey(true);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...