13

I am coding a Console Application in c++ and I need to make something like a "loading.gif" just by using ASCII characters.

The following is the list of the characters that I should be using:

  1. --
  2. \
  3. |
  4. /
  5. --

These symbols will make a loading animation by cycling.

However, when I write the output, it become like:

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

I need to do this like this:

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

It should never go to the second line.

How can I do this in C++? Is there any kind of replace function?

Francesco
  • 3,200
  • 1
  • 34
  • 46
Aristona
  • 8,611
  • 9
  • 54
  • 80
  • possible duplicate of [Print spinning cursor in a terminal running application using C](http://stackoverflow.com/questions/199336/print-spinning-cursor-in-a-terminal-running-application-using-c) – Nicol Bolas Dec 13 '11 at 08:37

7 Answers7

19

You can use the backspace character ('\b') to go back and overwrite characters on the console. You'll also need to flush the output after each change, otherwise the output might stay in a buffer and not appear 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;
    }
}
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
6

The 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;
}
Software_Designer
  • 8,490
  • 3
  • 24
  • 28
4

Mike Seymour's Logic 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;
    }
}
Peter
  • 8,776
  • 6
  • 62
  • 95
3
#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);
    }
}
C0LD
  • 121
  • 1
  • 2
2

And a cross-platform answer:

#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, you can use std::chrono::seconds s(1); instead, passing s to std::thread::sleep_for();

scx
  • 3,221
  • 1
  • 19
  • 37
2

Staying very simple, you can use the backspace character, as explained here, which actually appears to be a duplicate of your question. For more complicated things, it gets platform specific; to set the cursor position maybe this can help, if you're on Windows.

Community
  • 1
  • 1
codeling
  • 11,056
  • 4
  • 42
  • 71
-6
for(i=0; i<100; i++)
{
    system("cls");
    cout << "Loading: " << i << " %  \n";
    if(i==100)
    {    
        redirect to void or... ?
    }
}
ArnonZ
  • 3,822
  • 4
  • 32
  • 42
l3mpik
  • 1