cout and cin

machg4

Registered
Before I say what I must say, this "bug" has been apparent throughout all the minor 10.0.* releases, and the original dev tools CD that comes with OS X as well as that installed with the dev tools update that's been out a few days as of today.

#include <iostream.h>
int main()
{
int a;
cout << "Enter a new value for a: ";
cin >> a;
return 0;
}

This program, when compiled with the apple devtools in c++ (in the term or the IDE), wont work as expected (seeing the value message, and then entering a new value for a). It works, actually, quite backwards from how you think it should. It gets the user input and THEN outputs the message, which *should* have come before the input. Am I doing something wrong? I've tested a ton of different methods of placing functions between the statements, and nothing seems to work. Good old printf and scanf work, but I love OO code too much to back away from it.

(I would rather be using -> 10 million times, in size 9 monaco, watching little ants of code blaze fire across the screen, but I can't stand this annoying bug.)
 
Originally posted by machg4


smallish C++ program deleted

This program, when compiled with the apple devtools in c++ (in the term or the IDE), wont work as expected (seeing the value message, and then entering a new value for a). It works, actually, quite backwards from how you think it should. It gets the user input and THEN outputs the message, which *should* have come before the input. Am I doing something wrong?

Yes, you are not flushing the output; please remember, the standard output is buffered (line buffered when directed to tty, block buffered otherwise). I'm not certain whether cout.flush() is available, in the sense of fflush( stdout ), but there is a mechanism to flush the output in C++ ofstream class.

You would have had the same effect using printf, but you did add the newline character into the string every time, right :)
 
Back
Top