# cout and cin



## machg4 (Jun 22, 2001)

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.)


----------



## shtjonathan (Jun 28, 2001)

I remember something with flush.
Try something like:

cout << "Enter value: " << flush;
cin >> a;

Good luck,
     Samuel


----------



## MikeySquid (Jun 28, 2001)

I know that with g++ you have to flush the buffers after each cout<< or it won't work right. 

Try:  
cout<<"some stuff to output: ";
cout<< endl;"
cin>>get;

I think it has something to do with the integration of the macos and bsd, but i don't really know.


----------



## ericmurphy (Jun 28, 2001)

Okay, here's what probably amounts to a really stupid question: in your code snippet, your #include statement doesn't include a file name, so when I try to compile it, I get the error message "illegal file name 'int'". If I take out the #include statement entirely, 'cout' becomes an undeclared identifier.

So, I guess my question is, what's the #include file that contains a definition of cout?

If my question doesn't make sense, it's because I know almost nothing about what I'm talking about.


----------



## endian (Jun 28, 2001)

the original post probably had angle brackets in it, which got stripped as html. cout is in iostream.h i believe


----------



## MikeySquid (Jun 29, 2001)

I did some somewhat complex (500 lines of code) programs for my advanced data structures class using g++ on the command line in OSX. As long as I ended each line of code that contained a cout with an endl; everything compiled and ran fine. Without the endl... trouble. You should be able to take code out of any book on c++ and just add endl; to any line of code with a cout and it should compile and run fine. If you do not add the endl it will not compile or it will not run right. 

by the way... My circular linked list ran twice as fast on OSX on a G4 400 as it did on Windows 98 on a P111 550 with an UW 7200rpm scsi HD!


----------



## shtjonathan (Jun 30, 2001)

Hello all,

putting endl into cout will act as a newline character as in printf("\n");, it will put the cursor at the beginning of the next line.

putting flush or cout.flush or cout.flush() into cout will flush anything left in the console output buffer to the console view.

So in your case you might want to choose for flush, so the question will be printed in the console before cin requests input from the user. 

(The user of your program will now be able to enter his answer at the same line of your question, unlike when you use endl)

Good luck,
     Samuel


----------



## MikeySquid (Jul 1, 2001)

putting an endl; at the end of each cout causes a newline character to be output to the buffer and it causes the buffer to be flushed. You could do a newline character and a flush to accomplish this but just doing an endl is a lot easier.


----------



## sd (Aug 27, 2001)

OK, I had the *exact* same problem, and was about to post a similar thread when I saw this one... The *flush* thing seemed like it was the answer, and I was quite happy, until I tried it.. 


- When I insert " << flush " or "cout.flush()" it changes nothing. 
- _However_, when I insert a "\n" it *does* display the prompt before waiting for input, which doesn't seem right. According to the thoughts above, "endline" does "\n" and flush, so "\n" alone shouldn't do anything.


Can anyone offer any thoughts? It appears to me that this *is* a bug, where the buffered text is never displayed until it is sent a new line... Any help / thoughts?


----------



## MikeySquid (Aug 28, 2001)

I dunno if this has anything to do with it, but I was at the Fizzilla (Mozilla on MacOSX ) website where they claimed that there were "problems" with the g++ compiler in MacOSX. Hopefully this will get fixed.


----------



## coffeenowdammit (Sep 8, 2001)

IIRC you need to either "tie" the streams together, or use the "in_avail()" method of cin. I'm not up on the actual syntax or method calls, but Stroustrup addresses this very issue in the 3rd edition of "The C++ Programming Language". 
<p>Using endl and flush to acheive your goals may just be a happy accident of the iostream class implementations.
<p>Sorry if this didn't point you to exactly what you needed (I don't have my copy of Stroustrup with me -- dammit!), but I hope it at least shoves you in the general direction.


----------



## ladavacm (Sep 8, 2001)

> _Originally posted by sd _
> *OK, I had the *exact* same problem, and was about to post a similar thread when I saw this one... The flush thing seemed like it was the answer, and I was quite happy, until I tried it..
> 
> 
> ...



Seems to be fixed in 10.1, albeit wrongly (the same overzealous autoflush as in FreeBSD iostream implementation;  i.e. cout.flush() is not needed, ever)


----------



## jarinteractive (Sep 9, 2001)

use 

fflush(stdout);

before you're cin call. It does work, I was having the same problem.


----------

