I was just wondering how some programs update certain parts of the terminal output.... Like when showing the percentage done. Its hard to explain...
Like if the output was this:
Where [number] was continuously updated.
How is that done?
Thanks
I was just wondering how some programs update certain parts of the terminal output.... Like when showing the percentage done. Its hard to explain...
Like if the output was this:
Where [number] was continuously updated.
How is that done?
Thanks
This code will do what you're asking for, however, I have only tested it in windows. Its behavior may be different under linux/mac.
Qt Code:
#include <iostream> using std::iostream; int main() { for (int i=0; i < 5; i++) cout << "Progress: " << i*25 << "%\r"; return 0; }To copy to clipboard, switch view to plain text mode
Shouldn't it be:
?Qt Code:
cout << "\rProgress: " << i*25 << "%";To copy to clipboard, switch view to plain text mode
I guess the difference is minimal... Oh, and don't forget to flush the output or you'll see nothing.
For completions sake:
Qt Code:
#include <iostream> using namespace std; int main() { for (uint i = 0; i <= 100; ++i) { cout << "\rProgress: " << i << "%" << flush; for (uint j = 0; j < 9999999; ++j); // pause } cout << endl; return 0; }To copy to clipboard, switch view to plain text mode
\r means carriage return. Without a \n, it just takes the cursor back to the beginning of the current line. flush flushes the buffer.
"The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry
Alright, thanks all.... Its nice to fill in one of those little gaps of knowledge.![]()
Bookmarks