'why cout statement in Ubuntu not working as expected?

I tried to write this code in Ubuntu and run it from terminal but the problem is the second "cout" statement prined before the first one.

for (int i = 5; i > 0; i--) {
    cout << " the program will start within " << i << " seconds" << endl;
    cout << " be ready";
    sleep(1); //sleep 1 second before clearing the terminal
    system("clear");
}

** the output:

be ready the program will start within 4 seconds **

I wrote the corresponding code in windows and it runs successfully



Sources

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

Source: Stack Overflow

Solution Source