I have this done to where you enter any text you want and it will be revealed by a spinner. I have two problems:
1. When it's done, I can't figure out how to get rid of the spinner after the last character. It spins one extra time.
2. What sleep can I use to make it less than one second? I'm actually using an IDE on Android, so it needs to be Linux compatible.
#include <iostream>
/* unistd.h no longer required for sleep(). */
usingnamespace std;
void spin();
int main()
{
int pos;
/* 'spaces' is unused and not needed. */
int x;
string text;
cout<<"Enter text: ";
getline(cin, text);
/* I changed <= to <. It's mind-bending at first but you almost always want
just plain less-than. This stops you from printing the spinner one extra
time. */
for(pos = 0; pos < text.length(); pos++)
{
/* This is the same as what you had before, just the loop bound test was
adjusted and the spin call moved ouside the inner loop -- it happened
last every-time already. */
for(x = 0; x < pos; x++)
cout << text[x];
spin();
cout << "\r";
}
cout << text << endl;
return 0;
}
/* required for our new sleep call */
# include <thread>
void spin()
{
char spinner[] = {'/', '-', '\\', '|'};
for(int i = 0; i < sizeof(spinner); i++)
{
cout << spinner[i];
fflush(stdout);
/* Also chrono::seconds, nanoseconds, hours, etc -- this sleeps for
100 milliseconds at least. */
this_thread::sleep_for(chrono::milliseconds(100));
cout << "\b";
}
}