1

I have been learning C++ and thought it would be useful to have a nice progress bar to use in some of my future projects. I googled some things, mainly how to pause a program, but once I did that I came up with this basic script:

#include <iostream>
#include <random>
#include <chrono>
#include <thread>

int main() {
    int i = 0;

    std::string block = "[";

    std::cout << "[";   

    for (int i = 0; i < 10; i++) {
        // int time = randomInt(200, 600);
        std::this_thread::sleep_for(std::chrono::milliseconds(250));
        block += "█";
        std::cout << "\r" << block;
    }

    std::cout << "]" << std::endl;

    return 0;
}

I thought this was a perfect bit of code. However, when I go to run it, it waits for ~second and outputs the final bar out all at once.

I am a bit lost by this and cannot seem to pin down what is causing this.

Any guidance is appreciated.

Note:

I first thought this was a Windows gcc compiling issue so I switched to clang and after that didn't work, switched to Linux clang. All of these compilers gave me the final output all at once.

divinelemon
  • 1,925
  • 2
  • 23

0 Answers0