Below is the code, when it run shows a combination of back-slash and forward slash and by putting them together and printing them shows a stick rotating. The problem is that it is only rotating at a same position I want to move it back and forth in a line.

void run()
{
 while(1)
  {

                printf("\\");
                sleep(0.5);
                printf("\t");
                printf("\r");
                printf("|");
                sleep(0.5);
                printf("\t");
                printf("\r");
                printf("/");
                sleep(0.5);
                printf("\t");
                printf("\r");

  }
}

Dani AI

Generated

A quick diagnosis: your loop prints a spinner frame at the same column then returns, so it only appears to rotate in place. pointed this out — the character must be drawn at a changing column to slide — and is correct that delay calls are platform-specific.

A simple, portable approach is: keep a horizontal position index and a direction flag. Each tick do a carriage return, print pos spaces, print one spinner character, flush stdout, wait a short interval, then update pos and flip direction when you hit the ends. Use spaces instead of tabs (tabs vary by terminal) and make sure output is flushed or turn off buffering so frames appear immediately. For portability use a small millisecond sleep wrapper (POSIX usleep vs Windows Sleep).

Example implementation (POSIX/Windows portable):

#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif
#include <stdio.h>

static void msleep(unsigned int ms) {
#ifdef _WIN32
    Sleep(ms);
#else
    usleep(ms * 1000);
#endif
}

int main(void)
{
    const int width = 30;
    const char frames[] = "-\\|/";
    int pos = 0, dir = 1, fi = 0, max = width - 1;

    setvbuf(stdout, NULL, _IONBF, 0); /* disable buffering so output appears immediately */

    while (1) {
        printf("\r");
        for (int i = 0; i < pos; ++i) putchar(' ');
        putchar(frames[fi]);
        fflush(stdout);

        msleep(100);

        fi = (fi + 1) % (sizeof frames - 1);
        pos += dir;
        if (pos <= 0) { pos = 0; dir = 1; }
        else if (pos >= max) { pos = max; dir = -1; }
    }
    return 0;
}

Notes: if the console does not honor carriage returns the same way (Windows older consoles), consider using ANSI cursor sequences or the platform API (SetConsoleCursorPosition) or a library like ncurses/PDCurses for robust control. Redraw flicker can be reduced by updating only the two changed positions (erase previous, draw new) instead of reprinting the whole line. This should give the back-and-forth motion wanted.

Recommended Answers

All 2 Replies

You have to move it then. What you are doing is

output a character at the left edge of screen;
wait;
output a tab;
return to left edge;
repeat;

How can you put the character where you want it?

My compiler would not let me use sleep (with a small 's').
And...I always use larger numbers. I don't think your delay is very long. If you want a half-second delay:

Sleep(500);

You don't need the tab, if you want the stick to show up in the same spot each time (at the left). Or if you want it to show up in the middle of the screen somewhere, then put the tab after the return. Put one tab before the first line printed and omit the last tab.

If you want it to look like it's rotating, you'll need to add a horizontal line, won't you?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.