Delay output in C ++ on linux

I want some random characters printed on the console and then deleted with "\b". After that, I want to put my own variable so that it looks like "randomization." The problem is that this is happening too fast. I wanted to delay the output with the function usleepor sleep, but when I use it, nothing is printed in the console.
A brief example:

#include <iostream>
#include <unistd.h>
using namespace std;

int main()
{
    char chars[]={'a','b','c','g','h','u','p','e','y'};
    for(int i=0; i<8; i++)
    {
        cout << chars[i];
        usleep(200000);
        cout << "\b";

    }
}
+4
source share
3 answers

Try my little slowtty program from github.

pty rs232c, , stty(1) .

$ slowtty
$ stty 1200
$

(, 1200ba)

+1

, std::cout . , ( ). std::flush std::cout:

cout << chars[i] << flush;

:

  • ++ 11, . std::this_thread:sleep_for, >= ++ 11:

    std::this_thread::sleep_for(std::chrono::milliseconds(200));
    
+4

.

To make sure that what you sent to coutreally out of the buffers, you need to call

cout.flush();

before bedtime

+1
source

Source: https://habr.com/ru/post/1614030/


All Articles