473,699 Members | 2,907 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 6021
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
6124
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
1400
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
5191
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
4564
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
4931
by: Joachim | last post by:
Is there a way to set a timeout for the Console.ReadLine method?
0
1420
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
14377
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
1012
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
1703
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
4880
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
8704
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
8623
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9054
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
6544
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
4390
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...
0
4637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3071
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
2
2361
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.