1

I have a C++ console application that prints some output constantly while it also accepts commands (using std::cin) from the user - output and input happen in separate threads.
If I write a text while some output appears the written text is mixed with application output. How can I prevent this behaviour? The console

To solve this problem, I need to display the program one line above the line where the text is typed. I'd inspire myself in Minecraft Bukkit server's solution - however I need the same for C++.

desired behaviour

Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
  • 3
    Well, what is the desired behaviour? It's quite natural that this happens. If you want something like a console chat client (`irssi` for example), look into a library like `ncurses` instead of trying to patch it together with `cin`/`cout` only. – us2012 Oct 06 '13 at 13:28
  • You may use some lock... – Jarod42 Oct 06 '13 at 13:38
  • 1
    @Jarod42 That doesn't sound like a great idea. In the example of a chat program that would mean that once you start typing, you miss *all* new messages until you press enter, and only then see them. – us2012 Oct 06 '13 at 13:42

2 Answers2

0

Assuming you want the output to appear while things are being typed, you'll need some screen control facilities to have the output go somewhere different than the input area. If I were tasked to implement something like this writing to a terminal I would refresh my ncurses experience. I realize you are on a Windows console and I have no idea if the Windows console is capable of the screen control needed to make it happen.

You can possibly tie custom stream buffers into std::cin and std::cout using the curses functionality under the hood but it may not be worth it. In any case, it isn't entirely trivial.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
0

There's a windows port of ncurses called pdcurses. But if you are using visual studio there's a simple function provided called SetConsoleCursorPosition()

Duncan Smith
  • 530
  • 2
  • 10