473,748 Members | 2,658 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can I clear the buffer between Console.ReadLin e() inputs (do-while input)

2 New Member
I am making a game that requires input to be taken until the user inputs "exit" to the console. That part is taken care of with a simple do-while loop where i ask for user input:

Expand|Select|Wrap|Line Numbers
  1. do
  2. {
  3.     string input = Console.ReadLine();
  4.     game.getInput(input); //input validation (disregard)
  5. }
  6. while (Console.ReadLine().ToLower() != "exit");
  7.  
Now here is the problem: When I run the Console application, I input a string such as "walk north" and the program responds accordingly. The second time I input anything, the program does not respond and I must input a value again for the value to be read in. Every other input is disregarded as no value being input. I suspect the buffer isn't being cleared between inputs, but I have never dealt with that in C#. Here is an example of what happens:

Expand|Select|Wrap|Line Numbers
  1. (Console Starts)
  2. Console has started! //introduction to program
  3. >hello     //what the user inputs: prints value on next line
  4. hello      
  5. >hello2     //second input (supposed to print to next line)
  6. >hello2     //third input is taken and printed normally
  7. hello2
  8. >hello3     //fourth input (supposed to print to next line)
  9.  
All even numbered inputs do nothing.

It seems that every other input is completely disregarded and tossed out even though the user has input something new. Any idea how to clear the buffer between or set up the loop in a better way to accept user input without this "every other input doesn't work" business?

EDIT: In case it matters, the game.getInput() function returns in all cases.

Thanks,

Themantimes8

FINAL EDIT:
After reviewing Rabbit's tip, I reconstructed the loop to look like this:

Expand|Select|Wrap|Line Numbers
  1. do
  2.             {
  3.                 string input = Console.ReadLine();
  4.                 if (String.IsNullOrEmpty(input))
  5.                 {
  6.                     Console.Out.WriteLine("Invalid Input");
  7.                 }
  8.                 else if (input == "exit")
  9.                 {
  10.                     exit = true;
  11.                 }
  12.                 else
  13.                 {
  14.                     game.getInput(input);
  15.                 }
  16.             }
  17.             while (!exit);
This seems to have solved all problems I had before. Thanks Rabbit!
Sep 20 '13 #1
2 6039
Rabbit
12,516 Recognized Expert Moderator MVP
That's because you're reading 2 lines for each loop iteration. You're reading one line at 3 and one line at 6.
Sep 20 '13 #2
Themantimes8
2 New Member
Ohh. Thanks a lot man! I didn't even realize that it would have any affect on the line. Great Job! Would give best answer, but there was no recommendation on how to fix. Thanks anyways!

EDIT: Using Rabbit's tip I could find a way to fix (was tricky though) so might as well give best answer honestly. I guess not all best tips have to explicitly explain the solution, but have some hint that allows the user to come up with their own solution.
Sep 20 '13 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
6130
by: talljames | last post by:
Hi all, Does anyone know how to end a Console.ReadLine() programmically rather than typing the enter key in C#? I have tried just setting the class object that contains this line to null, but unfortunately that doesn't clear it up because it is waiting for a signal to reactivate itself. I thought that maybe a could write the equivelant of an Enter key to the console? Not sure how to do that ie "\r\n"? :?:
0
1402
by: Vampier | last post by:
Hello, I don't know if my professor pulled a late Aprils fools joke on us or not. Here is my 'problem': I am supposed to enter 3 numbers without entering the return key, I can only enter the space key. Supposable I must have a value returned when I entered a spacekey. Since all the numbers a re 4 digits I thought it would be a good idea to look into the limiting the length of a Console::Read(); or Console::Readline(); is there a...
1
5198
by: Kevin | last post by:
In a newsgroup thread from Jan 8, 2003 between Barry Holsinger and the VBDotNet Team, please review this excerpt: "You understood my problem completely. Your sample code provides a really elegant way to inject CrLf into the input stream, which effectively unblocks the ReadLine method. Last night, I had finally got the WriteConsoleInput
8
4568
by: nadeem_far | last post by:
Hello All, These days I am working on a console application that will register for certain events and do some processing based on the event notification. Now, all the objects are being created in main(). Once the main exists the program ends. Here is what I want to do.
1
4938
by: Joachim | last post by:
Is there a way to set a timeout for the Console.ReadLine method?
0
1425
by: alokranjan26 | last post by:
HI all, I just want to know how can i clear a console screen in using c# , can some one also tell in it can be done using API. Regards, Alok Ranjan
2
14385
by: SriBhargav | last post by:
Hi, I've a question on setting timeout on console.readline() I would like the user to input something through Console.readline() in 5 secs. If there is no input in that time, I would like to proceed further with the program logic. I had a difficulty in implementing this, as console.readline() indefinitely waits for the user input.
0
1017
by: needhelpnow | last post by:
Hi, I have a webservice client (win app) which talks to a server (console app) via a webservice server, things work fine until I decided to add a Console.ReadLine() to get some input interactively at the server. A TargetInvocationException is thrown at the webservice client. Any help will be very much appreciated!
1
1708
by: Allen Maki | last post by:
Hi everybody, I need your help. I am using Visual C++ .NET How can I make Console::ReadLine accepts int? In other words. I know I can do this:
2
4884
by: elvan | last post by:
using System; namespace namespace1 { class class1 { static void Main(String args) { Console.WriteLine("What is your initial?"); int name1 = Console.Read(); char ch = Convert.ToChar(name1);
0
8995
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9561
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9381
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9332
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8252
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6799
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3316
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.