why doesn't this work?!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <unistd.h>
using namespace std;

int main()
{
	for(int i=0;i<150;i++){
		cout << "0_0";
		sleep(1);
		cout << "/r";
		cout << "^_0";
		sleep(1);
		cout << "/r";
	}
	return 0;
}


it just doens't show anything, its like stuck with no output,

also when i remove the sleep functions i get this output :" 0_0/r^_0/r0_0/r^_0/r0_0/r^_0/r ... "
Your output might be staying in the buffer, and not actually be printed to the screen.

Try flushing. You can use cout << flush or you can use cout << endl which is basically a newline + flush:

1
2
3
4
5
	for(int i=0;i<150;i++){
		cout << "0_0" << endl;
		sleep(1);
		cout << "^_0" << endl;
		sleep(1);
thanks a lot :) that works, but i want the next line to replace the old one in same place, how should i do that?!
There isn't really a portable way to do that. At least not with the STL.

You might be able to use the backspace character:

1
2
3
cout << "\b\b\b"; // <- backspace 3 times
cout << "0_0"; // <- output your thing
cout << flush;  // <- flush 


But whether or not that will actually work depends on your platform/terminal.
that works on my ubuntu terminal :) thanks, but just wondering do the console based game's use the same technique ?

i see the little guy blinking :P :D
just wondering do the console based game's use the same technique ?


Probably not. They probably reposition the cursor to wherever they want it.

But if you are planning on making a console game... please allow me to try and dissuade you. A simple 2D games with graphics in an actual window is actually easier to make in a lot of ways.

See this for more:
http://www.cplusplus.com/forum/articles/28558/
Use flush to flush the output. Try using '\r' (carriage return) instead of "/r" ( a two character string) This might work

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <unistd.h>
using namespace std;

int main()
{
	for(int i=0;i<150;i++){
		cout << "0_0" << flush;      // flush it
		sleep(1);
		cout << '\r';             // \r and /r are different. 
		cout << "^_0" << flush;
		sleep(1);
		cout << '\r';
	}
	return 0;
}
Topic archived. No new replies allowed.