Click to See Complete Forum and Search --> : the stupid |/-\ in C++


sincka
07-28-2001, 02:14 AM
WTF?!

I don't understand... I saw a post like yesterday about doing that kewl thing when the '|' spins... I was like "ah that's easy" I was boared today and wanted to do it...


#include <iostream.h>

void main()
{
while(1)
cout << '|' << '\b' << '\\' << '\b' << '-' << '\b' << '/' << '\b';
}


I mean I thought that would do it... it can't be more complex than that... wtf am I doing wrong... AAAHHH!!! It's driving me crazy that something I thought so simple doesn't work.

GGRRR... I'm gonna go punch some pillows now.

binaryDigit
07-28-2001, 05:40 AM
could it be that it's running so fast you can't see it.
put those characters in an array

int
main(void) {

int index = 0;
char spinner[4] = { '|', '\\', '-', '/' };

for(; ;) {
cout << spinner[index] << '\b';
index++;
sleep(1);
}

return 0;

}

i don't remember if you need anything for sleep, and my syntax may not be right on, but you should get the general idea

[ 28 July 2001: Message edited by: binaryDigit ]

Xprotocol
07-28-2001, 02:54 PM
I did the same thing a while back, here's mine.

#include <iostream.h>
#include <stdlib.h>

int main()
{
long int i;
int n;
while (n > 0)
{
cout << "How many cycles? Use a negative number to exit.\n";
cin >> n;
system("cls");
for( i = 0; i < n; i++ )
{
cout << "|";
system("cls");
cout << "/";
system("cls");
cout << "-";
system("cls");
cout << "\\";
system("cls");
}
}
return 0;
}

If using linux, you should be able to just replace the "cls" with "clear".

[ 28 July 2001: Message edited by: Xprotocol ]

sincka
07-28-2001, 10:08 PM
Yeah... but you would think that it would work with something so simple. Just like the one posted.

miller
07-28-2001, 10:25 PM
You've got two problems.

1. It would happen very fast, which means you need to put a sleep between each print of a character.

2. Streaming IO is buffered. You also need to flush stdout after you print each character. with a cout << flush;

The following worked for me:

#include <iostream.h>
#include<unistd.h>

void main()
{
while(1) {
cout << '|' << flush;
sleep(1);
cout << '\b' << '\\' << flush;
sleep(1);
cout << '\b' << '-' << flush;
sleep(1);
cout << '\b' << '/' << flush;
sleep(1);
cout << '\b';

}
}


Oh, if the 1 second sleep is too long, use the usleep function.

Good luck.

[ 28 July 2001: Message edited by: miller ]