-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogress_bar.cpp
More file actions
46 lines (35 loc) · 972 Bytes
/
progress_bar.cpp
File metadata and controls
46 lines (35 loc) · 972 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <string>
#include <unistd.h>
class ProgressBar {
public:
ProgressBar(int total) : total(total), current(0) {}
void update(int amount) {
current += amount;
int percent = (int)(((float)current / (float)total) * 100);
// calculate the length of the progress bar
int length = (int)((float)percent / 100.0f * 20.0f);
// output the progress bar to the terminal
std::cout << "\r[";
for (int i = 0; i < length; i++) {
std::cout << "#";
}
for (int i = 0; i < 20 - length; i++) {
std::cout << " ";
}
std::cout << "] " << percent << "%" << std::flush;
}
private:
int total;
int current;
};
int main() {
ProgressBar bar(100);
for (int i = 0; i < 100; i++) {
bar.update(1);
// simulate doing some work
usleep(100000);
}
std::cout << "\nDone." << std::endl;
return 0;
}