usleep() problem

Apr 24, 2010 at 7:14am
Hi all!!
I want to make loading effect in c++. When I use this code, program works, but I want cout in one line.
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{	int i;
	for(i = 0; i < 50; i++)
	{	usleep(100000);
		cout <<"|"<< endl;
	}
	return 0;
}


When I use this code, cout is in one line, but the program doesn't do what I wanted to do. He waits about 50*100000 and then prints 50 times |.
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{	int i;
	for(i = 0; i < 50; i++)
	{	usleep(100000);
		cout <<"|";
	}
	return 0;
}

Please help!!!
Apr 24, 2010 at 7:53am
Try
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{	int i;
	for(i = 0; i < 50; i++)
	{	usleep(100000);
		cout <<"|" << flush;
	}
	return 0;
}
Apr 24, 2010 at 7:57am
Oh...
Thank you very much...
Can You tell me what means flush??
Apr 24, 2010 at 8:07am
Synchronizes the buffer associated with the stream to its controlled output sequence. This effectively means that all unwritten characters in the buffer are written to its controlled output sequence as soon as possible ("flushed").


http://cplusplus.com/reference/iostream/manipulators/flush/
Topic archived. No new replies allowed.