Thread: Flushing the input buffer.

  1. #1
    Seven years? civix's Avatar
    Join Date
    Jul 2002
    Posts
    605

    Flushing the input buffer.

    Well, when my program runs the block of code below, I enter help for the input, and it does the correct thing, but when system("PAUSE"); comes up and I hit a key, the console window flashes, and wont do anything. I ultimately have to shut it down with the X at the top right-hand corner, which locks up my CPU. DA has told me that I need to flush the input buffer, so I used fflush(stdin); which was the only thing I know. What am I doing wrong?


    Code:
    /* this is the code that I think is the culprit*/
    clearscreen
    _header
    br
    int cmndline;
    cout<<"INPUT: ";
    cin>>cmndline;
    if(strcmp("string help", "string help") ==0)
    {
    fflush(stdin);
    cout<<"hello";
    br
    pause
    goto startofprog;
    }
    br, _header, clearscreen, and pause are all macros.

    Pause is a macro for system("PAUSE");


    if you want to compile this for yourslf, use the following block of code:

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <windows.h>
    #include <string.h>
    
    int main()
    {
    startofprog:int cmndline;
    cout<<"INPUT: ";
    cin>>cmndline;
    if(strcmp("string help", "string help") ==0)
    {
    fflush(stdin);
    cout<<"hello";
    cout<<endl;
    system("PAUSE");
    goto startofprog;
    }
    return 0;
    }
    Thanks in advance for any help!
    Last edited by civix; 08-19-2002 at 06:39 PM.
    .

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    i think this may work for your needs,

    // where INT_MAX s the maximum number of characters to clear up to '\n'

    cin.ignore( INT_MAX, '\n' );
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    "The Oldest Member Here" Xterria's Avatar
    Join Date
    Sep 2001
    Location
    Buffalo, NY
    Posts
    1,039
    I'm pretty sure this is what you're trying to accomplish(please dont yell at me for using int..it works!)
    //edit yeah sorry i meant goto..but hey, i'm just manipulating his old post so it functions right

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <windows.h>
    #include <string.h>
    int main()
    {
    	startofprog:
    	char cmndline[20];
    	cout<<"INPUT: ";
    	cin>>cmndline;
    	if(strcmp(cmndline, "help") ==0)
    	{
    		cout<<"hello";
    		cout<<endl;
    	}
    	if(strcmp(cmndline, "exit")!=0)
    	goto startofprog;
    	return 0;
    }
    Last edited by Xterria; 08-19-2002 at 07:15 PM.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'm pretty sure this is what you're trying to accomplish(please dont yell at me for using int..it works!)
    Using int? No, that's not what I'd start yelling about...

    goto startofprog;
    Now that on the other hand...

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Civix,

    Please, don't use 'fflush(stdin);'. Prelude will tear her hair out (provided she hasn't already done so) if she sees this again in anyone's post.

    The behavior - from what I've gathered from the gazillion threads that have been posted concerning this recently - is undefined. Not good. (That much, I do know! )

    no-one suggests using cin.ignore() which is an excellent choice, and the superior one to fflush(stdin).

    Also, do away with the 'goto' command. It's legitimate, but not a good habit to form, hence, quzah's post.

    Personal note:
    if you want to compile this for yourslf, use the following block of code:
    First, it won't compile because you don't include the appropriate, terminating brace from the IF statement. Without it, we have no idea what your logic is, i.e. which statements you want included inside of the IF statement.

    Second, 'goto' will never allow 'return 0;' to execute. One step beyond an infinite loop. You now have an infinite program. (I refer you back to quzah's post.)

    I leave the conclusion of this to you. [hint] You've given us supplemental code that's more problematic than what the original was. [/hint]

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  6. #6
    Seven years? civix's Avatar
    Join Date
    Jul 2002
    Posts
    605
    Whoops, dumb, common Civix mistake. Sorry

    EDIT: Fixed the first post!
    Last edited by civix; 08-19-2002 at 06:41 PM.
    .

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Civix,
    Whoops, dumb, common Civix mistake. Sorry
    I doubt that.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Leaving charscters in input buffer
    By pavlosgr in forum C Programming
    Replies: 5
    Last Post: 11-24-2008, 02:10 PM
  3. How to avoid buffer overflow at input?
    By netstar in forum C++ Programming
    Replies: 4
    Last Post: 02-13-2005, 01:58 AM
  4. How to put a Char into the Input buffer of a console?
    By Aidman in forum C++ Programming
    Replies: 10
    Last Post: 03-09-2003, 10:05 AM