Thread: flushing ....

  1. #1
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273

    flushing ....

    Why is it that flushall(); works for me in this code and cout.flush(); doesn't??

    Code:
    while ( i<drivers )
    {
    	cout.flush();	//not working like flushall();
    
    //	flushall();		//was used, but it don't really want
    			//to need to have it there if possible
    
    	cout<< endl << "What is your name: ";
    	cin.getline( TempName, NAME_SIZE );	
    
    //	to be entered into a struct, but I didn't put it in this
    //	cause it would just clutter up the post 
    
    }
    basically, when I have flushall(); it works fine in that the program doesn't skip the getting name thing. but if I put cout.flush(); it doesn't work and if I don't have any flushing thing there it acts as it would with cout.flush() or rather cout.flush acts as if there wasn't any flusher.

    I bet that if someone compiles this they actually won't need a buffer for it which leads me to further confusion ... argh

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    did you try:
    Code:
    std::cout<<std::flush();
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Quote Originally Posted by major_small
    did you try:
    Code:
    std::cout<<std::flush();
    thanks major_small for your reply!!

    I just tried it, it may just be that I don't have a header, but I got this error:

    [ERROR]

    U:\CAss\One\Rally.cpp(65) : error C2780: 'class std::basic_ostream<_E,_Tr> &__cdecl std::flush(class std::basic_ostream<_E,_Tr> &)' : expects 1 arguments - 0 provided
    c:\program files\microsoft visual studio\vc98\include\ostream(398) : see declaration of 'flush'

    [/ERROR]

    EDIT:
    I've just deduced there that the problem is the .getline thing in the cin .... is there another way of reading in the whole space thing without using .getline (short of making your own function to do it of course)?
    Last edited by twomers; 02-09-2006 at 03:29 AM.

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    I just realized there are no parenthesis on the end of flush when used the way I showed... so try giving those the axe and see if it helps...

    you may also want to add:
    Code:
    std::cin.getline(TempName,NAME_SIZE,'\n');
    just so that it stops when you hit enter (the way you have it, it'll keep taking input until the array is full)
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    In your example code I don't see any need to flush cout

    Code:
    while ( i<drivers )
    {
    	cout<< endl << "What is your name: ";  // endl will flush cout
    	cin.getline( TempName, NAME_SIZE );	// cin / cout are synchronized so getline flushes cout
    
    }
    Kurt

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    you may also want to add:

    Code:
    std::cin.getline(TempName,NAME_SIZE,'\n');

    just so that it stops when you hit enter (the way you have it, it'll keep taking input until the array is full)
    cin.getline(TempName, NAME_SIZE)

    is eqivalent to:

    cin.getline(TempName, NAME_SIZE, '\n')

  7. #7
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    thanks for your reply Zuk/Kurt and major_small !!

    Quote Originally Posted by ZuK
    In your example code I don't see any need to flush cout
    I didn't see any reason to have to flush either, but when I compiled the code it skipped the "Enter your name" thing and went straight to the second thing without inputting anything ...

    Quote Originally Posted by major_small
    you may also want to add:
    Code:
    std::cin.getline(TempName,NAME_SIZE,'\n');
    that sir, is highly aloof! I had it in the code, but it didn't copy ... must have been a backup file that I copied it from, one which didn't have the '\n' terminator or something similar.

    Quote Originally Posted by major_small
    did you try:
    Code:
    std::cout<<std::flush();
    I just tried that, with the correction that you posted, and it still didn't do anything to help with the flushing ... does anyone know what the cause of that may be?? Could it have anything to do with the code preceeding it?? if so, all that's being done before is creating a dynamically allocated array and testing to see if it was created correctly .... i'm big time confused!

  8. #8
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    >> Could it have anything to do with the code preceeding it??
    Normally it has. But it has nothing to do with cout.
    if you use some statement like
    Code:
    cin >> number;
    before using getline() it will retun an empty string because there would still be a '\n' left in the buffer.
    It's never a good idea to mix different types of input statements.
    But that is all in the FAQ's
    Kurt

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    One easy solution to the issues from mixing cin >> and getline is to add cin.ignore() after any call to cin >>, and specifically after any call to cin>> that comes before a call to getline.

  10. #10
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Quote Originally Posted by Daved
    One easy solution to the issues from mixing cin >> and getline is to add cin.ignore() after any call to cin >>, and specifically after any call to cin>> that comes before a call to getline.
    good call Daved!! works like a treat now! thanks a bunch! I'm still kinda confused about why I had to do it, cause i don't really think I needed to do it before in similar things ...

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    As ZuK said, operator>> leaves a newline in the buffer. For example, if you prompt the user for a number and they type 3 and then hit enter, then cin >> number will read the 3 into the variable and stop. However, there will be a newline from when the user hit enter that is still in the stream. Later, you call getline, which just reads until it reaches it's delimiter (which is a newline by default). So if there is a newline in the stream from the previous cin >> call, then that newline is picked up by the getline call and nothing else is read in. The cin.ignore() works by ignoring the newline left in the input stream.

    The reason the ignore() isn't necessary when you have a second call to cin >> after the first one is that operator>> ignores leading whitespace (including newlines). So the newline that is in the input buffer from the first call to cin >> is ignored automatically and the second call to cin >> just waits for the user to type in more stuff.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Flushing buffer
    By $l4xklynx in forum C Programming
    Replies: 6
    Last Post: 03-04-2009, 10:07 PM
  2. Need help flushing buffer
    By MSF1981 in forum C Programming
    Replies: 2
    Last Post: 02-15-2009, 07:30 PM
  3. flushing stdin
    By crash88 in forum C Programming
    Replies: 2
    Last Post: 06-29-2006, 02:23 PM
  4. endl and flushing the output buffer
    By dan20n in forum C++ Programming
    Replies: 6
    Last Post: 01-28-2005, 02:41 PM
  5. Flushing the input buffer.
    By civix in forum C++ Programming
    Replies: 6
    Last Post: 08-19-2002, 06:41 PM