Help! Cin killed Cout ...



I made a C++ program. It's works very well on Linux and on CodeWarrior's SIOUX-WASTE interface, but not on Darwin's console. I mean :

when I do :
cout << "Enter a key : ";
cin >> akey;

Then on Darwin the application acts as if there was written in the code :

cin >> akey;
cout << "Enter a key : ";

Of course, if I use cerr instead of cout there's no problem, but more rare : even if I make a flush the problem still exists.

I tried to change the shell, the problem continues.

And the problem is the same in both Terminal application and console (when you log as >console).

Have you got an idea ? :confused:
Thank you to help me,

PS: I would like to don't put any endl at the end of text output, since I'd like the user to see "Enter a key : _" (where '_' is the cursor).


Unperson Spotter
That would seem to be a cout tty flush bug, present in gcc or libiostream.

From C on practically any UNIX, fprintf is autoflushing if you are writing to a tty; it is block-flushing otherwise. To deal with that, one uses:

fprintf( file, format, ... );
fflush( file );

The same should be done using C++:

cout << "whatever";

This does not seem to work with cout. The FreeBSD 2.95.2 version of gcc always autoflushes cout (wasteful), whereas OS X gcc seems to do that only on endl.


so without doing :

cerr << "enter a key : ";
cin >> mykey;

there's no solution ...

Thank you anyway,


you can find more information about this subject on a last thread I didn't see (sorry).

here you've got the <a href="">link</a>.



I'm short on time so look at the code, try it to prove to yourself that it works then read up on the function. Note that IMHO this is not a bug in Mac OS X. I believe the specification for the functions are so wooly that different implementations will work differently. VC++ on windows has a similar problem: -

int foo;
// important bit here!
cout << "Enter a number : ";
cin >> foo;
cout << "you entered " << foo << endl;
return 0;

Note the sync_with-stdio call. This is what makes it all work.