Thread: Clearing the input buffer which doesn't have a newline

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    194

    Clearing the input buffer which doesn't have a newline

    TL;DR part:
    Okay so I came across this problem while trying to make a sort of piano simulator like thing using only the STL and on console.

    I used _getch() because that's the only input option that immediately takes input (you can't expect a piano player to press the enter key after he has selected his key now, can you?).

    Problem now is though, that while the key note is being played for say like half a second or whatever, the player can still fill the input buffer with keys so that the program will play those keys even when the user's hands are off the keyboard. And that's not very neat.

    -> So I need a way to make it so that everything before the key note being played if flushed off.

    Now for the requirement:
    I need something that can either flush the input buffer without needing a '\n' (there are lot of ways to flush with '\n', but we won't have a '\n' at all because our user does not use the enter key)

    Or alternatively I need something that could temporarily disable and re-enable input if that's even possible. But remember I can only work with the STL (I would still like to know if it's possible using some library, though).

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,602
    Well you would need to use your OS dependent functions to either
    - count how many chars are currently available, then read that many.
    - use a function to empty the input buffer
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2018
    Posts
    194
    Quote Originally Posted by Salem View Post
    Well you would need to use your OS dependent functions to either
    - count how many chars are currently available, then read that many.
    - use a function to empty the input buffer
    I'm using Windows and I need something that is supported in Turbo C (that's what my school uses).

    Salem counting the character is a great idea, but how would this be implemented? Remember that the program itself is busy executing a piano note while the buffer is being filled up due to the user pressing and holding the key. I'm just clueless..

    Also Salem, about using a function, can you please suggest me something? The only thing I could find was fflush(stdin) and that is considered to have undefined behavior so certainly I cannot be relying on that..

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,602
    > I'm using Windows and I need something that is supported in Turbo C (that's what my school uses).
    I thought you were using windows, not DOS.

    TurboC was rendered obsolete around 1990 (way before you were born I suspect).

    You might want to ask your school how learning ancient Egyptian hieroglyphs prepares you for a career in the modern world.

    Seriously, the best thing you can do is drop the course and learn by yourself using some modern tools.

    You're just going to get fed a bunch of mind-rot that you'll have to forget when you try and get a job (well a job that isn't computing museum curator anyway).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2018
    Posts
    194
    Things work a bit differently in India, we learn only up till defining functions and structures this year (of two years) and besides that we have arrays, loops and other basic stuffs like declaring a variable (yes). Our textbooks have small mentions about C++11 stuffs but the programs have clrscr() and getch(). And because our textbooks have these two functions, most schools take Turbo C as the compiler.

    Next year we will have things like pointers and class I think, or so I have heard (but nothing more than that).

    This is what everybody in my state in India have to go through if they take up computer science for these two years. Basically, if you're taking a science field then you have to take Physics, Chemistry, Math and one other subject. I chose computer science because it's really easy and plus I want to become a programmer in the future anyway so it wouldn't make a difference taking some other subject (you can take up any field of science if you choose science, but it's just that you will have more knowledge in the subject by taking what you want).

    The computer subject which I took has been brutally hammered so that people who haven't had computer science in school but want to take up computer science can have a chance. Basically you should not expect to learn a lot or rely on the textbooks for programming, it's just like an introduction.

    Really I don't mind because we don't learn that much anyway and that doesn't bother me because I can just learn on my own. Also that how much you get in the elective subject, which is computer science in this case, won't play a role in getting into a university.

    According to my lab assistant, it doesn't make sense to download another compiler because it would confuse the children. And for having two compilers, it's just not necessary because most kids who are really interested in programming have their own computer at home to work with.

    I agree that our textbooks need to be changed but it seems the education system thinks it's not necessary for the time being.

  6. #6
    Registered User
    Join Date
    Sep 2018
    Posts
    194
    Or even if it wouldn't work on Turbo C, is there a solution for this?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,402
    Have you considered ditching the requirement of "using only the STL and on console", possibly ambitiously using a GUI? After all, _getch is a non-standard function, so according to your requirements you are not allowed to use it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,465
    Also posted here.

  9. #9
    Registered User
    Join Date
    Sep 2018
    Posts
    194
    Quote Originally Posted by laserlight View Post
    Have you considered ditching the requirement of "using only the STL and on console", possibly ambitiously using a GUI? After all, _getch is a non-standard function, so according to your requirements you are not allowed to use it.
    there is a function called getche() in conio.h in Turbo C. It's basically _getch(), I said _getch() so some of you might be able to relate.

    I came up with this:
    Code:
    while(c = getche());
    I guess that will be sufficient for my piano simulator at school.

    So yes could you suggest me if I were not restricted to STL? Because that might come useful if I happened to make a console game (in my own pc, not in school), so yes in that case I would still want the console.

    Do you think C++ is good for GUI? From people I have heard it's not that good and that's why I haven't bothered trying to learn a proper GUI library and hence why I'm sticking to console.

  10. #10
    Registered User
    Join Date
    Sep 2018
    Posts
    194
    Oh wait I was wrong one minute let me test what I said.. (I'm sure it worked last time just forgot how I did it then)

    edit: Okay now I'm really clueless what I said doesn't work either and I think what I had done was while trying to clear the stream when there was a '\n'.. my bad.. but what do I do now!

    would fflush(stdin) work? Technically would it work?
    Last edited by Nwb; 11-04-2018 at 10:07 AM.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,402
    Quote Originally Posted by Nwb
    there is a function called getche() in conio.h in Turbo C. It's basically _getch(), I said _getch() so some of you might be able to relate.
    No, _getch is equivalent to getch, not getche. The difference has to do with whether or not the character read is also echoed to the console. Either way, all three are non-standard and hence not part of the "STL". In fact, I believe the motivation behind the underscore prefix for _getch was precisely to note that it is non-standard yet provided by the compiler/standard library implementation.

    Quote Originally Posted by Nwb
    So yes could you suggest me if I were not restricted to STL?
    Actually, the "STL" is the "Standard Template Library", the (ironically) pre-standard precursor to the part of the C++ standard library that mainly has to do with containers and algorithms and components associated with them, and so it is often still used to refer to that part of the standard library even though the term is technically obsolete. You appear to be using it to mean the entire standard library, which is wrong usage, and frankly I'd just avoid using "STL" and say "standard library" if that is what you mean. If you're really strictly restricted to the STL, then you arguably cannot even do I/O since the I/O streams part of the standard library isn't even part of the STL, despite involving templates, and of course the I/O inherited from C is out of bounds as it is obviously not part of the STL

    Quote Originally Posted by Nwb
    Because that might come useful if I happened to make a console game (in my own pc, not in school), so yes in that case I would still want the console.
    Perhaps you would look into using a library like ncurses.

    Quote Originally Posted by Nwb
    Do you think C++ is good for GUI? From people I have heard it's not that good and that's why I haven't bothered trying to learn a proper GUI library and hence why I'm sticking to console.
    This probably just depends on who you ask: there are plenty of C++ GUI programs, and then C++ is used for game programming, but at the same time there's no GUI support in the standard, and other languages may have GUI libraries that are easier to develop polished GUI programs in.

    Quote Originally Posted by Nwb
    would fflush(stdin) work? Technically would it work?
    Go ahead and give it a try. You're doing non-standard stuff anyway, so who cares about standard compliance and portability versus undefined behaviour? If it works, use it and document it, and keep your fingers crossed that it doesn't break on you one day.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    4,465
    Technically would it work?
    No..

  13. #13
    Registered User
    Join Date
    Sep 2018
    Posts
    194
    Damn so I guess I can't make a piano simulator in Turbo C.. I really wanted this to work.

    getche() doesn't echo to console and getch() does?? I haven't used getch, I used getche() because it could take input while getch() according to the Turbo C's small little reference, couldn't.

    Does getch() read from the stream? Maybe just maybe? Then could we use that?
    Last edited by Nwb; 11-04-2018 at 10:26 AM.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,402
    Quote Originally Posted by Nwb
    getche() doesn't echo to console and getch() does?
    getche is supposed to echo to console, at least by default: that's presumably what the "e" stands for.

    Quote Originally Posted by Nwb
    Does getch() read from the stream? Maybe just maybe? Then could we use that?
    The notion of an input stream is part of the standard. getch is non-standard, so unless you can find authoritative documentation telling you that it does, it doesn't. But whatever, stop asking questions and just experiment! You're in non-standard, poorly documented territory, so just give things a try and see what happens. The normal problem with experimenting in that way is that you might find something appears to work, but actually it results in undefined behaviour... but that doesn't matter to you now. As I said, if it works, use it. It's not like you're going to actually create a product to sell that you're going to have to maintain, right? Do whatever works, and forget about this later.
    Last edited by laserlight; 11-04-2018 at 10:33 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Sep 2018
    Posts
    194
    Really because when I used getche() it didn't echo to the console.. huh? I might be mistaken but I will confirm that, or maybe Turbo C is just weird after all.

    So I have these two questions:
    1) Does getch() read from the stream? If it does then could we use that?
    2) When _getch() is called (I don't know about getche()) it returns the key pressed and then a 0 following that.

    So maybe we could filter out the second 0 and clear the input buffer by using something like
    while(non-zero = getche());

    And then would something like that work?

    edit: but then it would go into an infinite loop, can we make it not go into an infinite loop somehow, like taking the getche outside the while condition or something?

    or wait would it even return 0? Because it returns 0 because getch needs to be called twice..


    sorry for being all over the place
    Last edited by Nwb; 11-04-2018 at 10:37 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using getchar() to flush newline from input buffer
    By Valour549 in forum C Programming
    Replies: 15
    Last Post: 11-03-2018, 07:49 AM
  2. Clearing c++ input buffer
    By mramazing in forum C++ Programming
    Replies: 4
    Last Post: 11-08-2007, 11:05 AM
  3. clearing the buffer - but not the newline
    By Ash1981 in forum C Programming
    Replies: 13
    Last Post: 01-06-2006, 05:22 AM
  4. Clearing the input buffer
    By caduardo21 in forum C Programming
    Replies: 1
    Last Post: 05-14-2005, 08:40 PM
  5. Clearing the Input buffer
    By Brain Cell in forum C Programming
    Replies: 5
    Last Post: 03-21-2004, 12:08 PM

Tags for this Thread