How to make loading animation in console application written in C ++?

I am coding a console application in C ++, and I need to do something like "load.gif", just using ASCII characters.

The following is a list of the characters I should use:

  • -
  • \
  • |
  • /
  • -

These characters will make the loading animation by looping.

However, when I write the output, it becomes the following:

Output line 1: - Output line 2: \ Output line 3: | Output line 4: / Output line 5: -

I need to do it like this:

Output line 1: [this will be replaced all the time]

It should not go to the second line.

How can I do this in C ++? Is there any replacement function?

+6
source share
7 answers

You can use the backspace character ( '\b' ) to go back and overwrite characters on the console. You will also need to clear the output after each change, otherwise the output may remain in the buffer and not be displayed on the console.

Here is a simple example:

 #include <iostream> #include <unistd.h> // for sleep() int main() { std::cout << '-' << std::flush; for (;;) { sleep(1); std::cout << "\b\\" << std::flush; sleep(1); std::cout << "\b|" << std::flush; sleep(1); std::cout << "\b/" << std::flush; sleep(1); std::cout << "\b-" << std::flush; } } 
+10
source

Microsoft Windows version.

 #include <iostream> #include <windows.h> // for sleep() int main() { std::cout << '-' << std::flush; for (;;) { Sleep(10); std::cout << "\b\\" << std::flush; Sleep(10); std::cout << "\b|" << std::flush; Sleep(10); std::cout << "\b/" << std::flush; Sleep(10); std::cout << "\b-" << std::flush; } return 0; } 
+5
source

Remaining very simple, you can use the backspace symbol as described here , which actually looks like a duplicate of your question. For more complex things, it becomes platform specific; to set the cursor position, maybe this can help if you are on Windows.

+2
source

Mike Seymour Logic is used, but with a realistic look.

 #include <iostream> #include <unistd.h> // for sleep() int main() { std::cout << "Loading Please Wait"; while (true) { sleep(1); std::cout << "." << std::flush; sleep(1); std::cout << "." << std::flush; sleep(1); std::cout << "." << std::flush; sleep(1); std::cout << "\b\b\b \b\b\b" << std::flush; } } 
+1
source

And cross platform response:

 #include <iostream> #include <chrono> #include <thread> /* Comment for c++11 solution. */ using namespace std::literals::chrono_literals; int main(int argc, char** argv) { /* Uncomment for c++11 solution. */ // std::chrono::seconds s(1); std::cout << '-' << std::flush; for (;;) { std::this_thread::sleep_for(1s); std::cout << "\b\\" << std::flush; std::this_thread::sleep_for(1s); std::cout << "\b|" << std::flush; std::this_thread::sleep_for(1s); std::cout << "\b/" << std::flush; std::this_thread::sleep_for(1s); std::cout << "\b-" << std::flush; } return 0; } 

Note that chrono_literals are only available in C ++ 14, instead you can use std::chrono::seconds s(1); by passing s to std::thread::sleep_for();

0
source
 #include <iostream> #include <stdio.h> #include <windows.h> int main() { for (;;) { std::cout << "\b\b\b\b\b\b\b\b\b\bLoading " << std::flush; Sleep(100); std::cout << "\b\b\b\b\b\b\b\b\b\bLOading " << std::flush; Sleep(100); std::cout << "\b\b\b\b\b\b\b\b\b\bLoAding " << std::flush; Sleep(100); std::cout << "\b\b\b\b\b\b\b\b\b\bLoaDing " << std::flush; Sleep(100); std::cout << "\b\b\b\b\b\b\b\b\b\bLoadIng " << std::flush; Sleep(100); std::cout << "\b\b\b\b\b\b\b\b\b\bLoadiNg " << std::flush; Sleep(100); std::cout << "\b\b\b\b\b\b\b\b\b\bLoadinG " << std::flush; Sleep(100); std::cout << "\b\b\b\b\b\b\b\b\b\bLoading. " << std::flush; Sleep(100); std::cout << "\b\b\b\b\b\b\b\b\b\bLoading.." << std::flush; Sleep(100); std::cout << "\b\b\b\b\b\b\b\b\b\bLoading..." << std::flush; Sleep(100); } } 
0
source
 for(i=0; i<100; i++) { system("cls"); cout << "Loading: " << i << " % \n"; if(i==100) { redirect to void or... ? } } 
-2
source

Source: https://habr.com/ru/post/903660/


All Articles