 |
 |
Ultra-newbie question about iostream
|
 |
|
 |
|
Phlog
|
|
Hi;
I'm trying to use the C++ compiler included with OS X (command line -- not Project Builder) to develop programs for a first-year computer science course. I've run into an elementary problem. If I do something like:
cout << "Enter a number: " ;
cin >> n;
nothing appears on the console until I type a number and hit return. Then, the "enter a number text is displayed" and the program continues. If I compile the same program on Solaris 5 this does not happen.
Any ideas?
Thanks.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Feb 2000
Location: Storrs,Connecticut, USA
Status:
Offline
|
|
You need to flush the output buffer before you take input. Usually compilers will do that for you when they encounter a cin after a cout, but don't count on it. In order to do that you'd write it like this:
cout << "Enter a number: " << flush;
cin >> n;
Or, if you want it to flush the buffer and move down to the next line you'd use endl instead of flush.
|
|
|
| |
|
|
|
 |
|
 |
|
Phlog
|
|
Curious. Appending " << endl" to the cout works, but " << flush" seems to do nothing.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Feb 2000
Location: Storrs,Connecticut, USA
Status:
Offline
|
|
Yea, I just tried it. It doesn't seem to work with flush. It's strange that it doesn't produce any error either. I looked in iostream.h and flush is defined there. I don't know why it doesn't seem to work. Maybe Apple's iostream library isn't very good. At least endl works. I usually use stdio.h anyway, so I never noticed that it didn't work on my computer before this.
|
|
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Sep 2000
Location: Calgary, Alberta, Canada
Status:
Offline
|
|
When mixing input types (numerics and strings..)
You'll want to put a cin.ignore('\n',100); after
your cout
Maybe that'll do the trick!
|
|
"The pool on the roof must have a leak"
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |