Learn how to Make Money Online | Free Tech Magazines
Go4Expert
Go4Expert RSS Feed

Go Back   Programming and SEO Forum >  Go4Expert > Articles / Source Code > Programming > C-C++

Discuss / Comment Copy HTML to Clipboard  Copy BBCode to Clipboard  Add to del.icio.us  Add to Google  Digg it  Add to Yahoo !  Add to Windows Live  Add to Facebook  Add to StumbleUpon 
 
Bookmarks Article Tools Search this Article Display Modes

Flush cout output carefully

By shabbir shabbir is offline

On 6th July, 2005
Flush cout output carefully

ADVERTISEMENT
Show Printable Version Email this Page Subscription Add to Favorites Copy Flush cout output carefully link

Author

shabbir ( Go4Expert Founder )

Shabbir is a developer in the field of Applications, web as well as database designing and is devoted to the optimization and usability of the code. He maintains Programming forum and is a C++ addict.


All articles By shabbir

Recent Articles

Similar Articles

  • Oops, No Similar Articles found for Flush cout output carefully but you can check out the Recent Articles.
  • Also see more articles by shabbir.
When you compile the following code in VS 6 you get the output as

Code: cpp
#include <iostream.h>
#include <stdio.h>
void main()
{
  cout<<"hello";
  printf("hi");
}
hihello

The reason behind this is cout is not flushed and you can get the desired out put by doing
Code: cpp
#include <iostream.h>
#include <stdio.h>
void main()
{
  cout<<"hello";
  flush(cout);
  printf("hi");
}
You can also use the <<endl; to flush the output after each cout but keep in mind that flushing the output can degrade performance.

Thanks
Shabbir Bhimani
Old 10-18-2005, 08:35 PM   #2
AhmedHan
Go4Expert Member
 
AhmedHan's Avatar
 
Join Date: Oct 2005
Location: Ankara/Turkey
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
AhmedHan is on a distinguished road

Re: Flush cout output carefully


Wov, that's really interesting.

In which header flush() is defined in? stdio.h or iostream.h?
AhmedHan is offline   Reply With Quote
Old 10-18-2005, 08:59 PM   #3
shabbir
Go4Expert Founder
 
shabbir's Avatar
 
Join Date: Jul 2004
Location: On Earth
Posts: 10,943
Thanks: 35
Thanked 167 Times in 139 Posts
Rep Power: 10
shabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud of
Send a message via Yahoo to shabbir

Re: Flush cout output carefully


Quote:
Originally Posted by AhmedHan
Wov, that's really interesting.

In which header flush() is defined in? stdio.h or iostream.h?
Both.
shabbir is offline   Reply With Quote
Old 12-07-2006, 09:33 PM   #4
DaWei
Team Leader
 
DaWei's Avatar
 
Join Date: Dec 2006
Location: Texan now in Central NY
Posts: 834
Thanks: 0
Thanked 2 Times in 2 Posts
Rep Power: 3
DaWei is on a distinguished road

Re: Flush cout output carefully


I might mention that iostream.h is deprecated and is provided for backward compatibility. One should use iostream without an extension. The phenomenon occurs because cout and printf use two different streams; they may, however, be synced. It is generally not good practice to mix the two forms.
__________________
DaWei on Pointers Grumpy on C++ Exceptions
Functionality rules; clarity matters. If you can work a little elegance in there, you're stylin'.
DaWei is offline   Reply With Quote
Old 12-07-2006, 09:36 PM   #5
DaWei
Team Leader
 
DaWei's Avatar
 
Join Date: Dec 2006
Location: Texan now in Central NY
Posts: 834
Thanks: 0
Thanked 2 Times in 2 Posts
Rep Power: 3
DaWei is on a distinguished road

Re: Flush cout output carefully


Forgot to mention that "void main" is non-standard code. The standard specifies that main return an int. Some compilers, particularly less compliant ones, accept the void specification, but its use is non-standard and not recommended practice.
__________________
DaWei on Pointers Grumpy on C++ Exceptions
Functionality rules; clarity matters. If you can work a little elegance in there, you're stylin'.
DaWei is offline   Reply With Quote
Old 12-08-2006, 10:23 AM   #6
shabbir
Go4Expert Founder
 
shabbir's Avatar
 
Join Date: Jul 2004
Location: On Earth
Posts: 10,943
Thanks: 35
Thanked 167 Times in 139 Posts
Rep Power: 10
shabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud of
Send a message via Yahoo to shabbir

Re: Flush cout output carefully


Quote:
Originally Posted by DaWei
I might mention that iostream.h is deprecated and is provided for backward compatibility. One should use iostream without an extension. The phenomenon occurs because cout and printf use two different streams; they may, however, be synced. It is generally not good practice to mix the two forms.
Quote:
Originally Posted by DaWei
Forgot to mention that "void main" is non-standard code. The standard specifies that main return an int. Some compilers, particularly less compliant ones, accept the void specification, but its use is non-standard and not recommended practice.
Both the points well accepted.
shabbir is offline   Reply With Quote
Old 03-06-2008, 01:52 PM   #7
rahul.mca2001
Ambitious contributor
 
Join Date: Feb 2008
Posts: 104
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 2
rahul.mca2001 is on a distinguished road

Re: Flush cout output carefully


i will try to use flush now
rahul.mca2001 is offline   Reply With Quote
Discuss / Comment Copy HTML to Clipboard  Copy BBCode to Clipboard  Add to del.icio.us  Add to Google  Digg it  Add to Yahoo !  Add to Windows Live  Add to Facebook  Add to StumbleUpon 


Currently Active Users Reading This Article: 1 (0 members and 1 guests)
 
Article Tools Search this Article
Search this Article:

Advanced Search
Display Modes
Bookmarks

Posting 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
Forum Jump

 

All times are GMT +5.5. The time now is 04:40 AM.