Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Ultra-newbie question about iostream

Ultra-newbie question about iostream
Thread Tools
Phlog
Guest
Status:
Reply With Quote
Jan 20, 2001, 11:57 AM
 
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
Reply With Quote
Jan 20, 2001, 12:06 PM
 
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
Guest
Status:
Reply With Quote
Jan 20, 2001, 12:27 PM
 
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
Reply With Quote
Jan 20, 2001, 12:48 PM
 
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
Reply With Quote
Jan 21, 2001, 12:17 AM
 
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"
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 12:08 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2