So I have a function (or rather, I'll turn it into a function later) to make a random % progress in a console window; like this:
#include <iostream>
#include <time.h>
#include <cmath>
#include <windows.h>
using namespace std;
int main()
{
srand(time(0));
int x = 0;
for(int i = 0; i<100; i++){
int r = rand() % 1000;
x++;
cout << "\r" << x << "% completed." << flush;
if(i < 43){
Sleep(r/6);
}else if(i > 43 && i < 74){
Sleep(r/8);
}else if(i < 98){
Sleep(r/5);
}else if(i > 97 && i != 99){
Sleep(2000);
}
}
cout << endl << endl << "Operation completed successfully.\n" << flush;
return 0;
}
The thing is, I want the output to be like this:
1% completed
|
(later...)
25% completed
|||||||||||||||||||||||||
How can I do that?
Thanks in advance!
Printing character '\r'
is useful. It puts the cursor at the beginning of the line.
Since you can not access to previous line anymore, you can have something like this:
25% completed: ||||||||||||||||||
After each iteration:
int X;
...
std::cout << "\r" << percent << "% completed: ";
std::cout << std::string(X, '|');
std::cout.flush();
Also, you can use: Portable text based console manipulator
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With