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

  1. #16
    Registered User
    Join Date
    May 2010
    Posts
    4,452
    You seem to be confusing yourself by referring to getch() and _getch() in the content of Turbid-C++. There is no _getch() in Turbid-C++, but there is a getch(). The _getch() you're talking about is in Visual C++ and it seems to be broken as was pointed out in one of your other threads.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,183
    Quote Originally Posted by Nwb
    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.
    I think you can turn that off or something. I don't know. I don't deal with <conio.h> functions; the last time I wrote a program involving it (specifically kbhit, I think), it was back in 2002 and I had a few weeks of experience with C++, and in fact at the time my teacher taught me to #include <iostream.h>
    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

  3. #18
    Registered User
    Join Date
    Sep 2018
    Posts
    194
    Let's consider this code:
    Code:
        char foo;
        cin >> foo;
        cout << foo;
    
        for (int i = 1; i <= 10; i++) {
            foo = _getch();
            cout << "\n" << (int)foo; 
        }
    
    
        string bar;
        cin >> bar;
        cout << bar;
    Suppose you entered "hello"
    foo is reading h, and bar is reading ello.

    _getch() is not reading from the stream at all!

    But from what I saw Turbo C's getche() was reading from the stream because I had a getche() which detected for say a key "k", when the key is pressed it plays sound() for say 1 second. I noticed that if I kept pressing k when the sound is being played, then even if I stopped pressing k the program will continue to be play k because getche() was taking k from the stream from when I had been pressing k when the sound was being played.

    Now I'm not sure if getche() returns 0 like _getch().

    edit: I don't know why that makes a difference but I'm just hoping it does.. lol. So we can use the getche() to read from input buffer, but is there a way to read from the buffer while there is something in fact to read from the buffer?
    Last edited by Nwb; 11-04-2018 at 10:54 AM.

  4. #19
    Registered User
    Join Date
    May 2010
    Posts
    4,452
    So if you're trying to use Turbo-C++ why are you posting and complaining about Visual C++ code?

  5. #20
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,498
    I feel sorry for you learning something so half-assed and so badly for 2 years.

    Perhaps
    kbhit in c | Programming Simplified
    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.

  6. #21
    Registered User
    Join Date
    Sep 2018
    Posts
    194
    It's fine Salem honestly. We learn other concepts like algorithms & flowcharts (I don't see myself using such stuff but it's cool), binary stuffs, history and etc. Now obviously like every other student, I HATE anything related to history but it's really simple. It's only the C++ part which needs to be updated. The only outdated thing even in that is that we use 0 and 1 for flag instead of bool and we have clrscr() and getch(). Other than that what we learn in the textbooks do not collide with modern programming except that we don't learn new concepts like namespaces (but they're easy to grasp).

    It's sort of a relief because compared to the other subjects we could have taken computers is very easy. I could study computers an evening before the test and all questions are direct but other subjects are REALLY REALLY competitive. The questions we get are really twisted they don't have one question that doesn't have you to convert every single given unit to SI unit. And it gets worse when you have to take up an exam to get into a GOOD university. Although how much you get in computers won't help you get into any university because they consider only Physics, Chemistry and Maths and Biology if you took biology.

    Can you give an example on how I could use kbhit?

  7. #22
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,498
    Read the link.
    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.

  8. #23
    Registered User
    Join Date
    Sep 2018
    Posts
    194
    Quote Originally Posted by Salem View Post
    Read the link.
    I understand how it works but I just amn't able to come up with how I would be using that..

  9. #24
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,498
    > I understand how it works but I just amn't able to come up with how I would be using that..
    So you don't understand it then.

    Or at least not enough to at least post an attempt at using it.
    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.

  10. #25
    Registered User
    Join Date
    Sep 2018
    Posts
    194
    Sorry, Salem we can use khbit for checking if a key is pressed before playing the sound. But what if '1' is on the stream and the user is pressing on '2'? The program executes for 1 and then verifies that the user is indeed pressing a key and so plays the note for '1', but the user is pressing '2' key.

    Is there a way to know if a 'specific' key is being pressed?

    Like is there a function that reads input but not from the buffer like '_getch() in VS'? But for Turbo C? getche() from what I have observed getche() reads from the buffer so we can't use that because it would be reading previously pressed keys instead of real time pressed key and if we wanted to know the real time key being pressed then again the problem of clearing the buffer comes into picture.

    What do you reckon? By the way thanks for baring with me ;p

    Do we need to disable buffering? That is the only thing that came into my mind. By the way how do I do that in Turbo C++ and does Turbo C++ support that?
    Last edited by Nwb; 11-05-2018 at 07:38 AM.

  11. #26
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,183
    I mentioned ncurses in a previous post, but perhaps you won't be able to get that to work on Windows. You might be able to get PDCurses to work, and from what I see from the docs, besides providing its own getch with delay mode or nodelay mode, it also provides a flushinp function that "throws away any type-ahead that has been typed by the user and has not yet been read by the program".
    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. #27
    Registered User
    Join Date
    Sep 2018
    Posts
    194
    Quote Originally Posted by laserlight View Post
    I mentioned ncurses in a previous post, but perhaps you won't be able to get that to work on Windows. You might be able to get PDCurses to work, and from what I see from the docs, besides providing its own getch with delay mode or nodelay mode, it also provides a flushinp function that "throws away any type-ahead that has been typed by the user and has not yet been read by the program".
    I appreciate the response. I want to keep having to use a library as a last priority. Is there anyway we can disable input buffering on Turbo C++? Or can we come up with some other approach?

    I know I meet seem paranoid but I really want to pull this off. If we're asked to progam something, we wouldn't be able to bring in drives or be able to use the internet.. we have to do everything on the spot. Does that make sense? Idk. So if I have to implement something external then I'm willing to as long as I can remember what to type or have it written on my book.


    edit
    I was thinking:
    Maybe khbit like Salem said is the solution. So I have the program wait for a key and when the key is detected by getche() it plays the corresponding note for just '10 milliseconds'. Now before playing the note it checks whether any key is held down. So because of the small time it might work! Oh Salem you're a genius!

    I think the only flaw is that we do not know if more than one key is being held, but I guess I might be biting off more than I can chew if I want to counter that as well.. hmm do you reckon Turbo C++ might have pthread?

    One more thing though I have to confirm this: I think I remember that the beeps were not continuous they had a pause.. so instead of beeeeep it went beep beep beep.

    I thought this was because getche() worked like _getch() in VS in that it also returns a 0. Just thought I'd mention if any of you guys happened to know the reason for this. But wait now that I think about it that doesn't make sense because the default case is to exit.. Oh lord why is programming so hard..


    Thank you guys for baring with me feel free to tell me if I have missed something or if there is a better way. I'll test this whenever we have lab. For now I have vacation so umgh my hands are tied. (maybe my next lab will be 15th)
    Last edited by Nwb; 11-05-2018 at 08:23 AM.

  13. #28
    Registered User
    Join Date
    May 2010
    Posts
    4,452
    I was thinking:
    What a novel thing to do.

    Maybe khbit like Salem said is the solution.
    kbhit() is part of the solution, but not the whole solution.

    Is there a way to know if a 'specific' key is being pressed?
    This is where getch() comes into play.

    So I have the program wait for a key and when the key is detected by getche()
    This is where kbhit() comes into play, detecting a keyboard hit.

    it plays the corresponding note for just '10 milliseconds'.
    This is the real problem. How are you trying to play the note for just 10 milliseconds? You want to make a piano type instrument, then why the delay? Doesn't the piano just play one note at a time? If the player pushes another key doesn't that key immediately play? Note if you're "delaying" you need to "ignore" keyboard changes until the note finishes.

    Now before playing the note it checks whether any key is held down.
    Again kbhit() to the rescue.

    I think the only flaw is that we do not know if more than one key is being held,
    Don't over think things. Just get it to work with single keys.

    hmm do you reckon Turbo C++ might have pthread?
    No, threading doesn't exist in Turbo C++. Remember Turbo C++ was designed for a single threaded OS. How would a single threaded OS even begin to be able to handle multiple threads.

    One more thing though I have to confirm this: I think I remember that the beeps were not continuous they had a pause.. so instead of beeeeep it went beep beep beep.
    So once you confirm or dispel this rumour worry about this then.

    I thought this was because getche() worked like _getch() in VS in that it also returns a 0.
    Again don't confuse the implementation from VS with the implementation from Turbo-C++, they appear to be different. One might even say that Visual C++ is broken when it comes to the conio functions.

    By the way getch(), getche(), and kbhit() all work on the same stream and are all unbuffered.

  14. #29
    Registered User
    Join Date
    Sep 2018
    Posts
    194
    jim if it's unbuffered then what would explain the fact that when the program is executing sound() and I am pressing a key say 10 times, then after executing sound() it starts executing sound() 10 more times because it is reading the keys that I had pressed?

    This is the real problem. How are you trying to play the note for just 10 milliseconds? You want to make a piano type instrument, then why the delay? Doesn't the piano just play one note at a time? If the player pushes another key doesn't that key immediately play? Note if you're "delaying" you need to "ignore" keyboard changes until the note finishes.
    Let me elaborate.. I think I was too brief.

    So the program is an infinite while loop which gets broken when a non piano key is pressed (the default case sets the while to break).
    And inside the while loop is getche(), right below getche() is switch case.

    Inside each case is a sound() for 10 milliseconds of different frequencies.
    After the program is done executing the sound() it goes back to the getche().

    But, I am still holding the key, so the getche() reads the same key again and executes the same sound() frequency again, hence by using this I could make it so that the program plays the sound 'until' and 'only' while I'm holding the key. Makes sense?

  15. #30
    Registered User
    Join Date
    May 2010
    Posts
    4,452
    jim if it's unbuffered then what would explain the fact that when the program is executing sound() and I am pressing a key say 10 times, then after executing sound() it starts executing sound() 10 more times because it is reading the keys that I had pressed?
    So why would you expect anything different. Those keypresses need to be dealt with, you can't just ignore them. Just because the stream is not buffered doesn't mean that the events disappear if your program happens to be doing something else.

    So the program is an infinite while loop which gets broken when a non piano key is pressed (the default case sets the while to break).
    And inside the while loop is getche(), right below getche() is switch case.
    Show your code, it's too late to paly a guessing game.

    Inside each case is a sound() for 10 milliseconds of different frequencies.
    After the program is done executing the sound() it goes back to the getche().
    So? This is why I pointed out that this is the hard part.

    You need to either "ignore" keyboard changes during this time or stop the sound() and play another sound() when a new keypress occurs.

    But, I am still holding the key, so the getche() reads the same key again and executes the same sound() frequency again,
    Are you sure? I don't think getch() or getche() works this way. Have you verified your assumptions? If I remember correctly getch() and getche() only respond to single key down events (no automatic repeat) so it would take multiple keystrokes to keep things going. Again you need to show your code because it is possible you have a logic mistake that is causing problems.

    By the way this not how a piano would operate, even if the pianist holds a key down the sound "stops" (becomes inaudible) after a given time.

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