User interupts program output C++

I have written a program that gets user input with getline(), and then prints some information to the terminal with several std::cout's, with some delay in-between pieces of data. However, if the user types while the output is being printed to the screen, whatever they type will appear, spliced into the output in a very ugly way. (I don't think that the getline() actually has anything to do with this problem, but I thought I'd mention it just in case it does.)

Is there some way to stop user input from being written to the screen, while still using the terminal (I don't want to write a GUI)?
Last edited on
Standard C++ doesn't provide a way for you to do that.
The characters appearing on the screen is a function of the terminal, not of your program.

However, there are probably platform-specific ways for you to do this. What system are you using?
Linux - windows doesn't seem to have this problem. I got an answer here: http://www.dreamincode.net/forums/topic/396710-user-interupts-program-output-c/
Last edited on
I already found a solution and marked this question as solved. See the above link for the solution.
Short answer for people who don't care to trawl the link (I didn't, myself).
Under Linux, see
$ apropos -s3 '^tc' and $ man 3 termios.

In your code, write:
1
2
3
4
5
6
7
8
# include <termios.h>
# include <unistd.h>
...
termios tattr;
tcgetattr(STDIN_FILENO, &tattr); 
tattr.c_lflag &= ~(tattr.c_lflag & ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &tattr);
...

Not too bad.

Note that not echoing the input now doesn't mean that it goes away.
To get rid of the stuff you're not reading, do
 
tcflush(STDIN_FILENO, TCIFLUSH);

At some point (maybe at the end of your program).
Last edited on
Topic archived. No new replies allowed.