Skip to content

Instantly share code, notes, and snippets.

@ak9999
Last active December 11, 2016 20:23
Show Gist options
  • Save ak9999/8c35a4ac2058914bcc518f4aaac07983 to your computer and use it in GitHub Desktop.
Save ak9999/8c35a4ac2058914bcc518f4aaac07983 to your computer and use it in GitHub Desktop.
// File: progress_bar_example.cpp
// Modified from https://stackoverflow.com/questions/14539867/how-to-display-a-progress-indicator-in-pure-c-c-cout-printf
// Compile with:
// g++ -std=c++14 -o prog progress_bar_example.cpp -Werror -pedantic
#include <iostream>
#include <chrono> // for literals
#include <thread> // for cross-platform sleep
int main()
{
using namespace std::chrono_literals;
float progress = 0.0;
const int bar_width = 50;
while (progress <= 1.0) {
std::cout << '[';
int pos = bar_width * progress;
for (int i = 0; i < bar_width; ++i) {
if (i < pos) std::cout << '=';
else if (i == pos) std::cout << '>';
else std::cout << ' ';
}
std::cout << "] " << static_cast<int> (progress * 100.0) << "% \r";
std::this_thread::sleep_for(500ms); // Cross-platform sleep
std::cout.flush(); // Must manually flush or else output won't appear.
progress += 0.20; // To simulate progress. Can change number to whatever you want.
}
std::cout << std::endl; // Newline before printing new things.
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment