Thread: How to put a Char into the Input buffer of a console?

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    162

    How to put a Char into the Input buffer of a console?

    Hi, me again

    I am trying to write a program that loops an input until the escape key is pressed. But how do you put a single char into the input buffer so when the program requests a input the char will be displayed and removable by backspace key? Below is the basic what I have come so far but the problem is that “cin.putback(c);” does neither display the char “c” nor is it removable for the user, when pressing backspace key. I could add “cout << c;” but that only solves one problem, what do I need to do to make the char editable/removable for user when inputing?

    Code:
    char c;
    while (1) {
    	c = getch();
    	switch (c) {
    		case 27:  // Escape key is pressed
    			return 0;
    		case 13: // Enter key is pressed
    			//...
    		break; 
    		default: // Some other key is pressed
    			cin.putback(c);
    // Place char c into the input buffer,
    // but it won't be displayed nor editable when input requested.
    			cin.getline(Text, 256); 
    // request input from user,
    // the char c will be included in Text when user has finished input.
    			//...
    	}
    }
    Last edited by Aidman; 03-09-2003 at 08:50 AM.
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    As u are already using a nonstandard function getch() then you can put that char back where it came from with another nonstandard function ungetch()
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    162
    I dodn't see how this will help me place the char c into the input buffer. Please demonstrate how this should work in code please...
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You may also use getche() if you want the keystroke to be echoed to the screen. ungetch() may echo the keystroke as well when its put back to the console. Play around with these to get the desired effect.

    gg

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    162
    Hey getche() works perfect! Thanks Codeplug, again
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    If all you want to do is monitor for escape then try something like:
    Code:
    DO
       use non-standard getch() for next user keystroke
       IF keystroke is ESC
          return
       ENDIF
       put keystroke back to console, ungetch(), so cin can read it later
    WHILE user has not hit ENTER
    cin.getline()
    [EDIT]
    This don't work - read below...

    gg
    Last edited by Codeplug; 03-09-2003 at 09:31 AM.

  7. #7
    Registered User
    Join Date
    Dec 2002
    Posts
    162

    NO WAIT!

    no wait

    The char c is still not removable... it is displayed but not editable when input is requested. Please help...

    (this was a respons to MY last reply)
    Last edited by Aidman; 03-09-2003 at 09:19 AM.
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

  8. #8
    Registered User
    Join Date
    Dec 2002
    Posts
    162
    I realised that I need to flush/clear the input buffer when looping... how do I flush/clear the input buffer, is there a function for that?
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Nevermind.....that algorithm will infinite loop. When you ungetch() a char, the next call to getch() will return what you just put back. You cannot use cin.putback(c) unless it was the last char you extracted (using cin).
    You will be better off not mixing standard and non-standard I/O. Therefore, you will have to fill your input buffer yourself use getch[e]() and kbhit().

    gg

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Normally I wouldn't do this, but I haven't got my <conio.h> in a LONG time
    I'm sure something like this has been posted a 100 times too...

    Code:
        const int ESC = 27;
        const int BACKSPACE = 8;
        const int BUFFER_SIZE = 255;
    
        char buffer[BUFFER_SIZE];
        int c,i=0;
        do
        {
            c = getch();
    
            //accept this char if it's printable
            if (isprint(c))
            {
                buffer[i++] = (char)c;
                putch(c);
            }
    
            //support for backspace functionality
            if ((c == BACKSPACE) && (i != 0))
            {
                putch(c);   //backspace
                putch(' '); //overwrite existing char
                putch(c);   //backspace back into position
                i--;
            }
            
        } while((c != '\r') && (c != ESC) && (i < BUFFER_SIZE));
    
        buffer[i] = '\0';
    
        //did it work?
        cout << endl << buffer << endl;
    gg

  11. #11
    Registered User
    Join Date
    Dec 2002
    Posts
    162

    Talking

    Really thanks Codeplug! almost all the funtions in your code are new to me, but it works great... I was about to give up... when you came thanks!
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  2. writing a pack-style function, any advices?
    By isaac_s in forum C Programming
    Replies: 10
    Last Post: 07-08-2006, 08:09 PM
  3. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  4. Writing past ...
    By Ana Val sazi in forum C++ Programming
    Replies: 8
    Last Post: 06-29-2002, 08:43 AM
  5. getline problem
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 10-06-2001, 09:28 AM