好吧,我正在用 C++ 编写一个聊天程序,以便在 Linux 终端中使用。我希望在打字时也能收到消息。基本上,非阻塞cin。我设法通过创建一个线程来完成这项工作。但问题是传入的消息和我的打字被合并在一起。
有什么办法可以分开cin来自其他线路的线路cout?
对不起我的英语不好。
有什么办法可以分开cin来自其他线路的线路cout?
是的,但这有点棘手:您需要禁用控制台的回显,并在最终用户键入时打印他键入的字符。
当您检测到传入消息时,您需要执行以下三件事:
清除最终用户迄今为止输入的行上的所有字符
打印收到的消息
在下一行打印到目前为止输入的字符。
最终结果如下所示:
Friend: How are you? <== This is your friend's prior message
You: > I am fi <== This is the text you've entered so far
当您完成输入之前收到下一条消息时,您可以按如下方式重新排列最后几行:
Friend: How are you? <== This is your friend's prior message
Friend: Are you coming to school tomorrow? <== This is the new line that came in as the user was typing
You: > I am fi <== This is the text you've entered so far, re-printed again