How do I make a program in c++ that prints on char at a time?

Here is my code:

#include <iostream>

using namespace std;

void beginning()
{
cout << "H";
sleep(1);
cout << "e";
sleep(1);
cout << "l";
sleep(1);
cout << "l";
sleep(1);
cout << "o";
}
int main()
{
beginning();
return 0;
}

For some reason, it waits 4 seconds and then prints it all at once...can someone help?
@dingo25

You need a capital 'S' in sleep, and include Windows.h And 1000 is a one second delay.

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
// Print Hello.cpp : main project file.

#include <iostream>
#include <Windows.h>

using namespace std;

void beginning()
{
cout << "H";
Sleep(500);
cout << "e";
Sleep(500);
cout << "l";
Sleep(500);
cout << "l";
Sleep(500);
cout << "o";
Sleep(500);

}
int main()
{
beginning();
cout << endl;
return 0;
}
Did you mean Sleep(1000); rather than sleep(1); ?

Anyway, if the output does not appear, you may need to follow the output statement (just before the Sleep()) with a call to cout.flush(); in order to clear the output buffer.
Another thing. I am programming using CodeBlocks for mac. I tried entering whitenite1's code and it said "Sleep was not declared in this scope" and "Windows.h does not exist" Is there something like a Mac.h?
The cout.flush(); fixed my problem, thank you
Topic archived. No new replies allowed.