How do I make text seem to be typed letter by letter?

I don't need to involve the code I'm using, for once there aren't any errors. But I was just wondering how I make text look like its being typed? Its been a while since I did this, and I remember something either about for loops or a sleep identifier.
1
2
3
4
5
6
7
void type( const std::string& str, int delay = 50 )
{
    for( char c : str ) {
        std::cout << c << std::flush;
        std::this_thread::sleep_for( std::chrono::milliseconds( delay ) );
    }
}
If the output looks like it is being typed, you need to use std::this_thread::sleep_for().
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <chrono>
#include <thread>
#include <string>

int main()
{
    for(auto && c : std::string("Hello World"))
    {
         std::cout << c << std::flush;
         std::this_thread::sleep_for(std::chrono::milliseconds(300)); // Sleep for 0.3 second    
    }
    return 0;
}
The easiest way is just use the function Sleep. Easy to type, easy to remember and easy to use.

1
2
3
#include <windows.h>

Sleep(150); // I want to sleep for 150 milliseconds 
closed account (E0p9LyTq)
Pattako wrote:
The easiest way is just use the function Sleep. Easy to type, easy to remember and easy to use.

And not guaranteed to be supported by non-Windows compilers.

http://en.cppreference.com/mwiki/index.php?title=Special%3ASearch&search=sleep
> The easiest way is just use the function Sleep. Easy to type, easy to remember and easy to use.

Something like this, perhaps:
1
2
3
4
5
#include <thread>
#include <chrono>

inline void sleep( unsigned int msecs ) noexcept
{ std::this_thread::sleep_for( std::chrono::milliseconds(msecs) ) ; }



Manipulator slowly():
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
#include <iostream>
#include <string>
#include <thread>
#include <chrono>

template < typename C, typename T = std::char_traits<C>, typename A = std::allocator<C> >
struct slow_text
{
    std::basic_string<C,T,A> str ;
    unsigned int delay_msecs ;
};

template < typename C, typename T, typename A >
std::basic_ostream<C>& operator<< ( std::basic_ostream<C>& stm,
                                    const slow_text<C,T,A>& text )
{
    const auto period = std::chrono::milliseconds( text.delay_msecs ) ;

    for( auto c : text.str )
    {
        std::this_thread::sleep_for(period) ;
        stm << c << std::flush ;
    }

    return stm ;
}

template < typename C, typename T, typename A >
slow_text<C,T,A> slowly( std::basic_string<C,T,A>& str, unsigned int msecs = 200 )
{ return slow_text<C,T,A>{ str, msecs }; }

template < typename C > slow_text<C> slowly( const C* cstr, unsigned int msecs = 250 )
{ return slow_text<C>{ cstr, msecs }; }

template < typename C > slow_text<C> slowly( C ch, unsigned int msecs = 250 )
{ return slow_text<C>{ std::basic_string<C>( 1, ch ), msecs }; }

int main()
{
    std::cout << slowly( "hello" ) << slowly( " narrow", 300 )
              << slowly( " world!", 400 ) << slowly('\n') ;

    std::wcout << slowly( L"hello" ) << slowly( L" wide", 300 )
               << slowly( L" world!", 400 ) << slowly( L'\n' ) ;
}
Topic archived. No new replies allowed.