Skip to content

Instantly share code, notes, and snippets.

@lbl1985
Created May 10, 2013 04:35
Show Gist options
  • Save lbl1985/5552400 to your computer and use it in GitHub Desktop.
Save lbl1985/5552400 to your computer and use it in GitHub Desktop.
During processing a bunk of computation, people always eager to know how far from finish. The progress bar is exactly design for it. Console program does not support those fancy GUI, therefore something like below might be preferred. [======> ] 37%
void DrawProgressBar(int len, double percent){
std::string progress;
bool firstSpace = true;
for (int i = 0; i < len; ++i) {
if (i < static_cast<int>(len * percent)) {
progress += "=";
} else {
if (firstSpace)
{
progress += ">";
firstSpace = false;
}
progress += " ";
}
}
std::printf("[%s] %d %% \r", progress.c_str(), static_cast<int>(100*percent));
std::flush(std::cout); // Required.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment