'How to write output of two different functions at the same times on separate lines in console? (C++, Windows 10)

I have the write() function which takes a string and an int for time as parameters. It takes the given string and writes the same string, letter by letter, with a set time delay.

For example, write("Cat is moving", 50) would output each letter one after the other with a 50 millisecond delay.

Right now I am wondering how I could make it so the console can output 2 different write() functions on the screen at the same time, on two separate lines of the console, without overlapping each other.

I tried doing something with multi-threading but it didn't work and I believe there has to be a simpler way.

This program is intended for Windows only.

I did implement a change cursor position function already, so that shouldn't be a problem.

This is the function:

void write(string word, int time)
{
    for (int i = 0; i < word.length(); i++)
    {
        cout << word.at(i);
        Sleep(time);
    }
}


Solution 1:[1]

You are basically asking for a way for your program to be logically running several instances of the same function at the same time (without using recursion).

Although this can be accomplished by using multithreading, I don't recommend doing this, because in your case, this will probably introduce more problems than it will solve.

An alternative to multithreading may be to use coroutines, which were introduced in C++20. If you make write a coroutine instead of a function, then you may be able to solve the problem by having several of these coroutines logically running at the same time (they will probably not actually be running at the same time, because they will be suspended most of the time).

Unfortunately, I have no practical experience with coroutines, so I am unable to provide the details of a solution which would use coroutines. All I can say is that they are probably worth looking into for you, because as far as I can tell, they coincide with your way of thinking and with your way of wanting to solve the problem.

A more traditional way of solving the problem in C++ would be the following:

In the main loop of your program, you could call a function write_next_characters (which you write yourself) every 50 milliseconds, which writes the next characters of all words that are currently to be written.

In order for this function to work, your program will have to keep track of

  • a list of all words that are currently to be written,
  • the screen coordinates to which each word is to be written, and
  • how many characters of each word have already been written.

The function write_next_characters should receive a pointer to this information whenever it is called by the function main. It can then jump to the appropriate screen coordinates and print the next letter of each word. It will also have to update the state information mentioned above.

I would suggest that you define the following struct which contains the state information mentioned above for every word:

struct word_state
{
    //the word that is to be written
    std::string word;

    //the screen coordinates to which the word is to be written
    int x;
    int y;

    //how many characters have already been written
    int written;
};

For every word that you are currently writing, you will have to create one object of type word_state. You can use a container of type std::list<word_state> to keep them in a linked list. I believe that using a linked list is appropriate, so that you can easily remove words from the list after the last character of that word has finished printing.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1