c++ - Making a console progress bar? (Windows) -
so have function (or rather, i'll turn function later) make random % progress in console window; this:
#include <iostream> #include <time.h> #include <cmath> #include <windows.h>  using namespace std;  int main() {     srand(time(0));     int x = 0;      for(int = 0; i<100; i++){         int r = rand() % 1000;         x++;         cout << "\r" << x << "% completed." << flush;         if(i < 43){            sleep(r/6);         }else if(i > 43 && < 74){            sleep(r/8);         }else if(i < 98){            sleep(r/5);         }else if(i > 97 && != 99){            sleep(2000);         }     }      cout << endl << endl << "operation completed successfully.\n" << flush;     return 0; }   the thing is, want output this:
1% completed  |   (later...)
25% completed  |||||||||||||||||||||||||   how can that?
thanks in advance!
printing character '\r' useful. puts cursor @ beginning of line.
since can not access previous line anymore, can have this:
25% completed: ||||||||||||||||||   after each iteration:
int x;  ...  std::cout << "\r" << percent << "% completed: ";  std::cout << std::string(x, '|');  std::cout.flush();   also, can use: portable text based console manipulator
Comments
Post a Comment