Author Topic: Purely Cosmetic :D  (Read 10106 times)

ahluka

  • Jackass IV
  • Posts: 794
  • Karma: +10/-201
Purely Cosmetic :D
« on: August 26, 2005, 01:16:06 PM »
I thought I'd make my latest brainchild of a program a bit snazzy and have one of those spinning things constructed of '|/-\' looping over a number of times. I also thought it'd be easier to program:

Code: [Select]

void spinny_thingy (long duration)
{
    int    i;
    char    spinner[] = "|/-\\";

    for (i = 0; i < duration; i++) {
        putchar (spinner[i]);
        putchar ('\b');
        fflush (stdout);
    }
}


I get the infamous 'hang a little bit but don't output anything' when I call spinny_thingy.
Yes I know it's a useless waste of processing power :)

*edit*

And that I should be bitchslapped for bringing it up.

ahluka

  • Jackass IV
  • Posts: 794
  • Karma: +10/-201
Purely Cosmetic :D
« Reply #1 on: August 26, 2005, 01:29:43 PM »
OH YEAH

I was so hardcore about finding this I searched for 'spinning' and actually went all the way to the 4th page... AND I FOUND AN ANSWER!

So in case anyone was wondering
http://cboard.cprogramming.com/showthread.php?t=17736&highlight=spinning

*bitchslaps self*

JaWiB

  • definitelys definately no MacGyver
  • Jackass V
  • Posts: 1443
  • Karma: +57/-4
Purely Cosmetic :D
« Reply #2 on: August 26, 2005, 01:30:14 PM »
Well you don't want to use duration like that to control the loop, because you'll go out of the array bounds (besides, it wouldn't be basing it on time)

You want to cycle through the array (index: 0,1,2,3,0,1,2,3,etc) and exit the loop when a certain time has gone by (look at the clock() function in time.h maybe)
Edit: Too slow, here's my solution:
Code: [Select]

#include
#include
void Sleep(double time)
{
clock_t start = clock();
while ((clock()/(double)CLOCKS_PER_SEC)<((start/(double)CLOCKS_PER_SEC)+time));
}
void spinny_thingy (long duration)
{
    int    i;
    char    spinner[] = "|/-\\";
    clock_t start = clock();
   
    for (i = 4;(clock()/(double)CLOCKS_PER_SEC)<((start/(double)CLOCKS_PER_SEC)+duration); i++) {
        putchar (spinner[i%4]);
        putchar ('\b');
        fflush (stdout);
Sleep(.5);
    }
}

int main()
{
spinny_thingy(10);
}

ahluka

  • Jackass IV
  • Posts: 794
  • Karma: +10/-201
Purely Cosmetic :D
« Reply #3 on: August 26, 2005, 01:45:38 PM »
Hey what do you know it is faster. Cheers :)