Skip to content

Instantly share code, notes, and snippets.

@rjzak
Created May 31, 2013 20:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rjzak/5687633 to your computer and use it in GitHub Desktop.
Save rjzak/5687633 to your computer and use it in GitHub Desktop.
A progress bar in C/C++
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;
void printProgBar( int percent );
int main(int argc, char* argv[]) {
int N = 100;
for(int i = 0; i < N; i++) {
float p = (i / (float) N) * (float) 100;
printProgBar(p);
//cout << p << endl;
sleep(1.1);
}
printProgBar(100);
cout << endl;
return 0;
}
void printProgBar( int percent ) {
string bar;
for(int i = 0; i < 50; i++){
if( i < (percent/2)){
bar.replace(i,1,"=");
}else if( i == (percent/2)){
bar.replace(i,1,">");
}else{
bar.replace(i,1," ");
}
}
cout<< "\r" "[" << bar << "] ";
cout.width( 3 );
cout<< percent << "% " << std::flush;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment