Reverse terminal output

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mschenkelberg
    New Member
    • Jun 2007
    • 44

    Reverse terminal output

    I was wondering if it is easy to direct output in a scenario where the program gets user input from stdin and instead of outputting result on next line after use hits enter, it will output on same line user entered input.

    Normally this happens because user must press enter to submit text
    ./test
    Please enter a number: 32
    You entered 32.

    I would like to do this:
    ./test
    Please enter a number: 32 You entered 32.

    An example of a program that does this is gforth for the forth programming language. I was wondering if anyone knew how to do it with simple code.
  • Sick0Fant
    New Member
    • Feb 2008
    • 121

    #2
    It's a guess-- but try using the backspace escape sequence (?)

    Something like

    Code:
    cout<<"Enter a number: ";
    cin>> number;
    cout<<"\bYou entered << number
    Again, it's a guess.

    Comment

    • mschenkelberg
      New Member
      • Jun 2007
      • 44

      #3
      Originally posted by Sick0Fant
      It's a guess-- but try using the backspace escape sequence (?)

      Something like

      Code:
      cout<<"Enter a number: ";
      cin>> number;
      cout<<"\bYou entered << number
      Again, it's a guess.
      Nope \b only delete up to beginning of current line. Thanks for the response though.

      Comment

      • MACKTEK
        New Member
        • Mar 2008
        • 40

        #4
        You can do what you want to do using _getch() and _putch()
        you will need #include <conio.h>

        The _getch() will grab a keypress without echo to the screen.

        So, you will have to write a program to handle what you want printed to the screen... using _putch()

        So, for example you can write a loop using _getch and print out using _putch but not print when they press a space or an "enter" key.

        You will also have to convert your input to valid numbers and throw away invalid data.

        Also, there is no "backspace" or other line editing features "built in" to that, so you would have to either add them in or require the user to enter data according to the format you specify.
        For example: In a small program I wrote, using the backspace did actually "backspace" but remember that "backspace" will not erase your previous input characters from any array you are building unless you add it in.

        Comment

        • mschenkelberg
          New Member
          • Jun 2007
          • 44

          #5
          Thanks, but this program needs to run on linux. Are there equivalent linux functions?

          Comment

          • MACKTEK
            New Member
            • Mar 2008
            • 40

            #6
            There seems to be a possible fix.
            Using something called non-canonical mode.
            I am not a linux programmer, so good luck.

            Comment

            Working...