How to Flush standard input?

This is a discussion on How to Flush standard input? within the C++ Programming forums, part of the General Programming Boards category; Hi! Im trying to fulsh standard input - all though this seems not possible to me. My code is: Code: ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    29

    Cool How to Flush standard input?

    Hi!

    Im trying to fulsh standard input - all though this seems not possible to me. My code is:

    Code:
    	bool exit_status;
    	char current;
    
    	exit_status=false;
    	while(!exit_status){
    		cin.clear();
    		cin >> current;
    		switch(current){
    		 case 'p':
    			// do something
    		 break;
    		 case 'u':
    			// do something
    		 break;
    		 case 's':
    			// do something
    		 break;
    		 default:
    		 cout << "Unkown command" << endl;
    		}
    }
    When I run this, I dont expect when typing: "pussy", that the program runs the functions ascossiated with p,u,s,s and types "Unkown command"....

    Anyone knows how to flush the inputstream so that only function p will be run???

  2. #2
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    y not

    char variable[1];

    now only the first letter is read....and i hope u mean pussy in the feline sense.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    29
    Sure I could read the thing into a string, but what I search is a function that will FLUSH unread input. I thought clear(): did that, but it doesnt... Any suggestions?

  4. #4
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    i used

    cin.clear();
    cin.ignore();

    in my bot.

    http://cboard.cprogramming.com/searc...der=descending
    Last edited by RoD; 02-20-2003 at 12:23 PM.

  5. #5
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    cin.clear() clears the flag that would be set if a user entered a character into an integer variable or such.

    cin.ignore(80, '\n') *should* do what you want as it *should* strip off everything but your 'p'

  6. #6
    Registered User kiss_psycho's Avatar
    Join Date
    Feb 2003
    Posts
    49
    Use the folowing code fragment..
    Code:
    void ClearStdInput()
    {
       while(1)
       {
          if(kbhit())
             getch();
          else
             break;
       }
    }
    
    void main()
    {
       ........
       .......
       ClearStdInput();  //Clears standard input.
       .......
       ........
    }
    Happy Hunting...
    Last edited by kiss_psycho; 02-21-2003 at 02:28 PM.
    Definition of Programmer : A red-eyed mumbling mammal capable of conversing with inanimate objects.

    Happy Hunting...
    The Root

  7. #7
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    What about something like fflush()?

  8. #8
    Registered User kiss_psycho's Avatar
    Join Date
    Feb 2003
    Posts
    49
    fflush fails to work sometimes on some compilers such as Turbo C++. The ClearStdInput() is foolproof.
    Definition of Programmer : A red-eyed mumbling mammal capable of conversing with inanimate objects.

    Happy Hunting...
    The Root

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. large program code ,please help
    By Ash1981 in forum C Programming
    Replies: 14
    Last Post: 01-30-2006, 05:16 AM
  2. Trouble with a lab
    By michael- in forum C Programming
    Replies: 18
    Last Post: 12-06-2005, 10:28 PM
  3. Can someone help me understand this example program
    By Guti14 in forum C Programming
    Replies: 6
    Last Post: 09-06-2004, 12:19 PM
  4. Custom Made Safe Input Function
    By Beast() in forum C Programming
    Replies: 6
    Last Post: 08-21-2004, 10:19 PM
  5. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 12:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21