Pausing briefly inbetween lines

i have tried doing something with my code where i have some text displayed to the user. Then there is a brief pause, say 2 seconds, then the next line should appear. However when i run the code it just comes up with sleep not declared in the scope.
Heres my code below:

// testing sleep function
// By Camryn Procter
#include <iostream>
#include <time.h>
using namespace std;

int main()
{
cout << "HI peeps.\n";
sleep(2);
cout << "Hi again.\n";
sleep(2);
cout << "Bye now.\n";
return 0;
}

Can someone help?
Cheers =)
Last edited on
The way i did it was :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// testing sleep function
// By Camryn Procter
#include <iostream>
#include <thread>
#include <chrono>
using namespace std;

int main()
{
	cout << "HI peeps.\n";
	this_thread::sleep_for(chrono::seconds(2));
	cout << "Hi again.\n";
	this_thread::sleep_for(chrono::seconds(2));
	cout << "Bye now.\n";
	return 0;
}
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
#include <iostream>
#include <chrono>
#include <thread>

struct pause
{
    explicit constexpr pause( unsigned int millisecs ) : pause_for(millisecs) {}
    unsigned int pause_for ;
};

template < typename CHAR, typename TRAITS >
std::basic_ostream<CHAR,TRAITS>& operator<< ( std::basic_ostream<CHAR,TRAITS>& stm, pause msecs )
{
    stm << std::flush ;
    std::this_thread::sleep_for( std::chrono::milliseconds( msecs.pause_for ) ) ;
    return stm ;
}

int main()
{
    std::cout << "HI peeps.\n" << pause(2500) // 2.5 seconds
              << "Hi again.\n" << pause(1700) // 1.7 seconds
              << "Bye now.\n";
}
Thanks for that =)
Cheers
Topic archived. No new replies allowed.