Outputting text above user input with cout in separate thread

I'm running two threads in a program. One thread sends seconds elapsed to cout every second and the other thread runs a while loop that uses getline and cin to get input from the user in a sort of shell or command line.

My issue is that whenever something is sent to cout while the user is typing something it ends up on top of whatever the user has just typed and it ends up looking messy like this.

Is there any way to move what the user is typing a line down whenever something is sent to cout? Or maybe some alternative io that does this?

I would rather the output look more like this

Here's some extra details and some code:

#include <boost/algorithm/string.hpp>
#include <iostream>
#include <sstream>
#include <thread>
#include <chrono>
#include <vector>

using std::thread;
using std::vector;
using std::string;
using std::cout;
using std::cin;

void loop(bool &running)
{
    int secondsElapsed = 0;
    while (running)
    {
        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
        secondsElapsed++;
        cout << std::to_string(secondsPassed) << " Seconds Passedn";
    }
}

int main()
{
    //STARTING OTHER THREAD RIGHT AWAY
    bool running = true;                    
    std::thread t(loop, std::ref(running));

    string param;
    string currentLine;
    vector<string> params;

    while (true)
    {
        //PARSING USER INPUT FOR EVALUATION
        params.clear();

        getline(cin, currentLine);

        std::stringstream currentLineStream(currentLine);

        //CHECKING SIZE OF USER INPUT TO SEE IF THERE IS ANY
        if (currentLineStream.rdbuf()->in_avail() > 0)
        {
            while (getline(currentLineStream, param, ' '))
            {
                params.push_back(param);
            }

            //CAPITALISING FIRST PARAMETER TO MAKE EVALUATION EASIER
            boost::to_upper(params[0]);
        }
        else
        {
            params.push_back("");
        }
        //FINISHED PARSING INPUT, EVALUATING

        if (params[0] == "STOP")
        {
            running = false;
            break;
        }
        else
        {
            cout << "Unknown Command, help menu not implemented yet sorryn";
        }

    }         //END OF USER INPUT LOOP

    t.join(); //WAITING FOR OTHER THREAD TO FINISH
    return 0;
}
链接地址: http://www.djcxy.com/p/30820.html

上一篇: std :: async和std ::将来的行为

下一篇: 用cout在单独的线程中输出用户输入上方的文本