0

I'm trying to create a very simple chat application where a user types something in (received by cin) and I echo their output manually, but cin naturally prints to console after the user hits enter on the keyboard.

Is there a way to suppress this echo back into cout after the user returns from cin so I can override it with my own message?

Ex:

int main() 
{
    string str;
    while(true) 
    {
         getline(cin, str)
         cout << "Person: " << str << endl;
    }
}

The output looks like this:

Some text I typed
Person: Some text I typed

The first line is automatically echoed back into the terminal when the user sends the new line. This is the line I would like to suppress.

Any ideas? I am trying to stay away from using any 3rd person library if possible.

Eugene Sh.
  • 17,802
  • 8
  • 40
  • 61
CompEng88
  • 1,336
  • 14
  • 25
  • Isn't the first line printed while you type the text ? (And not after pressing enter)? – Chelmy88 Sep 23 '19 at 21:15
  • Have you tried to use `\r` to overwrite that first line? – Calslock Sep 23 '19 at 21:16
  • 2
    The line is not "echoed back into the terminal", nor does "cin naturally prints to console". cin is not your tty. cout is not your tty. do not conflate the two. The issue you are seeing is that the terminal is in cooked mode and is displaying data that is being entered at the keyboard. (eg, your program has nothing to do with the display of those characters). If you want to change the behavior of the terminal, you'll want to put the tty in raw mode. And you really do want to use a 3rd party library to do that. – William Pursell Sep 23 '19 at 21:17
  • Related: [Reading a password from std::cin](https://stackoverflow.com/q/1413445/2602718). I'd also look into something like [ncurses](http://www.cs.ukzn.ac.za/~hughm/os/notes/ncurses.html) – scohe001 Sep 23 '19 at 21:21
  • "cin naturally prints to console after the user hits enter " Why do you think that? and what does 'naturally' mean to you? – 2785528 Sep 23 '19 at 22:21
  • 2785528 and Chelmy88, the text is printed in-line with the rest of the output stream when the user presses enter. Until they press enter, the text is not mixed in with the rest of the program output. @scohe001, I'm not looking for password input in my chat application. I want the user to be able to see what they are typing until they press enter, at which point the input is suppressed from being mixed in with the rest of the output. – CompEng88 Sep 24 '19 at 15:03
  • Ahh I thought you wanted to handle output yourself, so you didn't want anything they type to show up. In that case, I still think ncurses is your best bet. – scohe001 Sep 24 '19 at 15:06
  • I think I need to revise my thinking. @WilliamPursell is right - it prints to the console as I am typing. The only way I see to stop that is to disable ECHO from input, which means the user can't see anything they type. – CompEng88 Sep 24 '19 at 15:36

1 Answers1

1

As far as I know this is not possible on standard C++ without using any 3rd party libraries. If you are willing to use OS specific libraries, then this link or this one might help. This is unfortunately not an option in native C++ because it is up to the OS to decide how it handles the console output (hence why for exemple you need OS specific solutions for advanced console stuff like colors).

  • In my case, this is a linux application. Both links suggest setting termios newt.c_lflag &= ~ECHO, which hides the user input completely (like in password mode), so the user can't see what he's typing unless they press enter. This is not a good answer to my question. – CompEng88 Sep 24 '19 at 14:59