Does anyone know a way to actively modify text output speed?

I'm trying to figure out a way to actively adjust text output speed. I currently have a function that outputs text, letter by letter, with a slight delay in between each letter.

For example, if the delay is normally half a second, I'd like it to change to a quarter second when enter is held down.

Here is a sample of the code, I know it's sloppy but it works.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
const char *introText = 
"aa aaa aaaaa aaaaa aaa aaa aaaaa aa aaaaa, aaa aaaa aaaaaa aa a aaaaa aaaaaaaaa\n"
"aaaa aaa aa aaa aaaa.\n" 
                                                          
int introTextLength = 0;

while (introTextLength < 102)
{
	cout << introText[introTextLength];

	++introTextLength;

	sleep_for(nanoseconds(30000000));

	if (introTextLength == 42)
		{
			sleep_for(nanoseconds(300000000));
		}

	if (introTextLength == 101)
		{
			sleep_for(nanoseconds(1500000000));
		}
}
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
#include <iostream>
#include <thread>
#include <chrono>
#include <string>
#include <vector>

struct text_segment
{
    unsigned int delay_msecs ;
    std::string content ;
};

std::ostream& operator<< ( std::ostream& stm, const std::vector<text_segment>& text )
{
    for( const text_segment& ts : text ) for( char c : ts.content )
    {
        std::this_thread::sleep_for( std::chrono::milliseconds( ts.delay_msecs ) ) ;
        stm << c << std::flush ;
    }
    return stm ;
}

int main()
{
    using text = std::vector<text_segment> ;
    std::cout << text{ { 0, "how " }, { 250, "now " }, { 500, "brown " }, { 1000, "cow" } } << '\n' ;
}
Once again, my hero to the rescue. Thanks big dog!
Topic archived. No new replies allowed.