0

Consider below code:

#include<iostream>

int main() {
    char line[256];
    std::cin.getline(line, 256);
    std::cout << line << std::endl;
}

Upon execution, whatever text we enter, appears twice on the console. Is there a way to overwrite the user entered text with the one couted later.

Sajal
  • 1,783
  • 1
  • 17
  • 21
  • 3
    Try using `cout<<"\b\r";` after `cin`, before the actual text that you want to print. – Susmit Agrawal Jul 27 '18 at 11:42
  • 3
    You could use a library like NCurses. – Eljay Jul 27 '18 at 11:42
  • 6
    How about not outputting the line in the first place? Overwriting text with identical content is pointless. – eerorika Jul 27 '18 at 12:24
  • 2
    This requires platform specific code (there's currently one Unix terminal specific answer) or terminal specific control codes (google for example "ANSI escape codes"), as standard C++ does not support this kind of "visual" control of input and output. So you should specify the platform (add relevant tag and possibly edit the question text). – hyde Jul 27 '18 at 12:48
  • Why don't you use `std::string` instead of C-style strings? They're much easier to use. – eesiraed Jul 27 '18 at 18:04
  • @user2079303: In actual requirement, I need to output a formatted version of the inputted text. – Sajal Jul 30 '18 at 05:34
  • @hyde: Actually I wanted to know if there is a platform independent way of achieving the desired behavior, thanks for letting me know that this requires platform-specific code. – Sajal Jul 30 '18 at 05:39
  • @Sajal Well, if using standard C++ without platform specific part is hard requirement, there's the option of clearing the screen by printing enough newlines, then printing entire screen contents again. For this to work really properly, you need to know the height of the terminal, though. Or you can just print "enough" newlines, then print the screen, which will be at the bottom of terminal window. Or you can just print some clear separator (two lines of ===== or something) and not worry about previous screen contents being visible at the top of the screen. – hyde Jul 30 '18 at 07:44

1 Answers1

0

You can suppress the output of getline

#include <termios.h>
#include <unistd.h>
#include <iostream>
#include <string>


void SetStdinEcho(bool enable = true) {
    struct termios tty;
    tcgetattr(STDIN_FILENO, &tty);
    if( !enable )
        tty.c_lflag &= ~ECHO;
    else
        tty.c_lflag |= ECHO;

    tcsetattr(STDIN_FILENO, TCSANOW, &tty);
}


int main()
{
    SetStdinEcho(false);

    char line[256];
    std::cin.getline(line, 256);

    SetStdinEcho(true);

    std::cout << intput << std::endl;

    return 0;
}

This works with linux.

My answer is taken from this post Reading a password from std::cin

There also a windows version is shown.

schorsch312
  • 5,553
  • 5
  • 28
  • 57
  • 1
    Would be good to mention that this code would only work on Linux and not Windows. – Rosme Jul 27 '18 at 12:35
  • @schorsch312: Thanks for your answer, I was looking for some platform independent code but it seems, it's not possible to do this platform independently. – Sajal Jul 30 '18 at 06:17
  • Of course a platform independent would be better, but I dont know any. – schorsch312 Jul 30 '18 at 07:10