Text manipulation problem

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <unistd.h>

using namespace std;

void spin();

int main()
{
    int pos;
    int spaces;
    int x;
    string text;
    
    cout<<"Enter text: ";
    getline(cin, text);
    
    for(pos = 0; pos <= text.length(); pos++)
    {
        for(x = 0; x <= pos; x++)
        {
            if(x < pos)
            {
                cout << text[x];
            }
            else if(x == pos)
            {
                spin();
            }
        }
        cout << "\r";
    }
    cout<<endl;
    return 0;
}

void spin()
{
      char spinner[] = {'/', '-', '\\', '|'};
    for(int i = 0; i < sizeof(spinner); i++)
    {
        cout << spinner[i];
        fflush(stdout);
        sleep(1);
        cout << "\b";
    }
}
Try something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>

/* unistd.h no longer required for sleep(). */
using namespace 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";
    }
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>
#include <thread>
#include <chrono>
#include <initializer_list>

void spin( unsigned int millisecs = 100, unsigned int cnt = 1,
           std::initializer_list< const char > ilist = { '/', '-', '\\', '|' } )
{
    if(cnt)
    {
        const std::chrono::milliseconds sleep_interval(millisecs) ;
        for( char c : ilist )
        {
            std::cout << c << std::flush ;
            std::this_thread::sleep_for(sleep_interval) ;
            std::cout << '\b' << std::flush ;
        }
        spin( millisecs, cnt-1, ilist ) ;
    }
}

int main()
{
    std::string text;
    std::cout << "Enter text: ";
    std::getline( std::cin, text );

    for( char c : text ) // favour range based for
    {
        if( !std::isspace(c) ) spin() ; // with defaults
        std::cout << c << std::flush ;
    }
    std::cout << "\b\n" ; // note: \b to remove the last spin character
}
JLBorges wrote:
this_thread::sleep_for(chrono::milliseconds(100));

I did not know the <thread> and <chrono> doesn't have to be included to use that function.

EDIT:

Isn't using != better than < or <= in for loops (type saftey) in C++?
Last edited on
Applied one suggestion and added an if statement to correct the spin problem. Haven't touched the time yet.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <unistd.h>

using namespace std;

void spin();

int main()
{
    int pos;
    int spaces;
    int x;
    string text;
    
    cout<<"Enter text: ";
    getline(cin, text);
    
    for(pos = 0; pos <= text.length(); pos++)
    {
        for(x = 0; x < pos; x++)
        {
                cout << text[x];
        }
        if(pos+1 <= text.length())
        {
            spin();
        }
        cout << "\r";
    }
    cout<<endl;
    return 0;
}

void spin()
{
      char spinner[] = {'/', '-', '\\', '|'};
    for(int i = 0; i < sizeof(spinner); i++)
    {
        cout << spinner[i];
        fflush(stdout);
        sleep(1);
        cout << "\b";
    }
}
Topic archived. No new replies allowed.