Jump to content

Moving a line of cin.getline down to the next available line (C++ console)?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
mholt

mholt

    Newbie

  • Members
  • PipPip
  • 27 posts
Hi,

I have a case in which the user is presented with a cursor to type input in a console window (in C++ with Dev-Cpp) - but a couple threads are running at the same time. These threads respond to events by "cout"ing text to the console on its own line.

So I need a way to have the current line with the cin.getline stuff move DOWN to the next available line, then put the text from the other threads above it. Oh, and if the user has typed something, that text should not be lost; the user should be able to just continue typing.

Is there a way to do this? What methods might you recommend?

Thanks,
-Matt

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Are you using strictly console, or are you using system API calls?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
G_Morgan

G_Morgan

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 537 posts
This is probably going to be complicated. What you'll need to do is create a thread safe console driver with your own API (i.e. you'll have to set up your own input buffers and write to the console yourself using system API's). You probably aren't going to be able to achieve what you want through the standard functions, C++ wasn't designed with threading in mind and the standard functions reflect this.

#4
mholt

mholt

    Newbie

  • Members
  • PipPip
  • 27 posts
Ah yes, sorry I should have clarified. I'm using the standard Windows API (no .NET stuff).

If it really is that complicated, could somebody suggest an alternative? It's a utility I'm writing that is sort of a "chat" program (except it's all encrypted, plus some other little features I included), so I basically need to "reserve" a space in the console for the user to type messages without being interrupted by an incoming message. (using Winsock)

I really want to avoid a GUI. Is there a way to do this? I'm still rather new, I think, and thinking out-of-the-box like this is still a bit beyond me.

Cheers!

#5
G_Morgan

G_Morgan

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 537 posts
Offhand I can't think of any. Dealing with CLI interaction with threaded programs is not something I've ever dealt with (at least I've never allowed more than one thread access to it).

This is the problem with threading. You need to be able to control access to a shared resource and AFAIK there is no way to achieve this with the basic iostreams library. Do these other messages have to go to the CLI? Can you divert them to a different stream like stderr?

Otherwise I think you will have to program the console directly rather than using iostream. Essentially I'd setup a console driver where a buffer takes up the bottom line of the console and the rest forms a message list. Your messages would be diverted to list and avoid your input buffer altogether.

#6
mholt

mholt

    Newbie

  • Members
  • PipPip
  • 27 posts
Okay. Thank you for this information! It's apparent that I should find a different way to prevent this problem. Thanks!