C:cin而cout

好吧,我正在用C语言写一个用于 linux终端的聊天.即使我正在打字,我也希望收到消息.基本上,一个非阻塞的cin.我设法通过创建一个线程来完成这项工作.但问题是传入的消息和我的打字正在合并在一起.

有没有办法将cin线与来自cout的其他线分开?

对不起我的英语不好.

Is there any way to separate cin line from other lines coming from cout?

是的,但这有点棘手:你需要为控制台禁用echo,并在他输入时打印最终用户键入的字符.

当您检测到传入消息时,您需要执行以下三项操作:

>清除最终用户到目前为止输入的行中的所有字符
>打印传入的消息
>打印到目前为止输入的字符在下一行.

最终结果如下:

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
相关文章
相关标签/搜索