H3lp with text tricks.

I want to do two things with text.

1. I want to create a function that will receive as input a sentense and display it slowly (like it's typed that moment from a user).

2. I was womdering if there is a way to delete a character that is displayed with cout<<. The aim is to have a dot blinking (display with cout and then delete a couple of times), like the user is waiting for the CPU to process data.
I solved the first issue by using the following function.
1
2
3
4
5
6
7
8
    void displaytext (char text[256])    {

        for (int i=0; i < strlen ( text ); i++)
            {cout << text[i];
            Sleep(40);
            }
        cout << endl;
        }


Yet the second issue is trickiest.
Last edited on
If you want to delete one character on the same line, you can write
cout <<"\b \b";

EDIT: Why don't you use C++ strings instead of C-style strings?
1
2
3
4
5
6
7
8
   void displaytext (string text)    {

        for (int i=0; i < text.length(); i++)
            {cout << text[i];
            Sleep(40);
            }
        cout << endl;
        }
Last edited on
Console output is streamed. You'll need a library to get around this to achieve what you are after. Consider NCurses.
Null: Thabks for the suggestions. I had tried with string, but I was doing something wrong (I propably forgot to add #include<string>.

As for the blinking problem, it worked exactly as I wanted. Thank you so much. Here's the function. It shows three dots appearing gradually and dissapearing three times.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    void blinking ()      {
        cout << "\nPlease wait while we process your request";
        for (int i = 1; i < 4; i++)     {
            string blink = ".";
            cout << blink; 
            Sleep(400);
            cout << blink; 
            Sleep(400);
            cout << blink; 
            Sleep(400);
            cout << "\b \b";
            cout << "\b \b";
            cout << "\b \b";
            Sleep(400);
            }
        cout << endl;
    }


Another option I also liked is the blinking cursor I get If instead the for loop, I add a Sleep(5000). That's OK for me too.
Topic archived. No new replies allowed.