Created
May 31, 2013 20:09
-
-
Save rjzak/5687633 to your computer and use it in GitHub Desktop.
A progress bar in C/C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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