Console window waiting on cin.ignore() or cin.ignore(2)

This is a discussion on Console window waiting on cin.ignore() or cin.ignore(2) within the C++ Programming forums, part of the General Programming Boards category; Why is it, that you have to use two lines like: cin.ignore; cin.ignore; or one time: cin.ignore(2); to make the ...

  1. #1
    C++ SharK The SharK's Avatar
    Join Date
    Mar 2004
    Location
    Denmark
    Posts
    62

    Console window waiting on cin.ignore() or cin.ignore(2)

    Why is it, that you have to use two lines like:

    cin.ignore;
    cin.ignore;

    or one time:

    cin.ignore(2);

    to make the console window wait for a keypress.

    One cin.ignore, doesn't seem to do the work

    Code:
    /**************************************************************
     * This program sums the numbers in a user-specified range,   *
     * omitting the if test that sets the upper and lower bounds. *
     **************************************************************/
    
    #include <iostream>
    
    using std::cin;
    using std::cout;
    using std::endl;
    
    int main()
    {
    	int Num_1;
    	int Num_2;
    	int Num_one = 0;
    	int Num_two = 0;
    	int Range = 0;
    
    	cout << "Exercise 1.20:" << endl << endl;
    	cout << "Enter two numbers !" << endl << endl;
    	cout << "Enter number one: ";
    	cin >> Num_one;
    	Num_1 = Num_one;
    	cout << "Enter number two: ";
    	cin >> Num_two;
    	Num_2 = Num_two;
    
    	if ( Num_1 < Num_2 )
    		for ( Num_one = Num_one ; Num_one <= Num_two ; ++Num_one  )
    		{
    			Range += Num_one;
    
    			if ( Num_one >= Num_two )
    			{
    				cout << endl << "The sum of all numbers from " << Num_1
    				     << " to " << Num_2 << " is: " << Range << endl;
    			}
    		}
    
    //	else if ( Num_one > Num_two )   // This works, but I "haven't learned" this
    									// technique yet...
    	if ( Num_1 >= Num_2 )
    		for ( Num_two = Num_two ; Num_two <= Num_one ; ++Num_two  )
    		{
    			Range += Num_two;
    
    			if ( Num_one <= Num_two )
    			{
    				cout << endl << "The sum of all numbers from " << Num_1
    					 << " to " << Num_2 << " is: " << Range << endl;
    			}
    		}
    
    	cout << endl << "Press [Enter] to exit... ;-)" << endl;
    	cin.ignore(2);	// To make the console window, wait for a keypress !
    	
    	return 0;
    }
    Last edited by The SharK; 07-16-2006 at 08:50 AM.
    Studying programming languages,
    you'll ALWAYS be a student ;-)

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    You typically don't use a cin.ignore to wait for a key press - you
    use cin.get. The thing is, when you read in a value using
    cin >> variable, say an int, when you press the enter key, the
    enter key is left in the input buffer. The call to cin.ignore just
    reads that value and discards it - if you use cin.ignore (2), and
    there is only one character in the buffer, it'll wait for another
    character (newline I mean) - thats's why you need cin.ignore (2).
    Since there is typically only one newline in the buffer, the usual
    format is:

    Code:
    //body of program
    
    .
    .
    .
    
    cin.ignore ();
    cin.get ();
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  3. #3
    C++ SharK The SharK's Avatar
    Join Date
    Mar 2004
    Location
    Denmark
    Posts
    62
    Hi Richie T


    Oh, I see !

    So I'll use:
    Code:
    cin.ignore();
    cin.get();
    To make my program discard the "Enter" keypress, and
    then use cin.get() to wait for the new keypress "Enter/newline"
    Studying programming languages,
    you'll ALWAYS be a student ;-)

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Console window in VC 2008
    By swgh in forum Tech Board
    Replies: 0
    Last Post: 06-05-2008, 03:13 PM
  2. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  3. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  4. Problem with creating new window, from another window
    By Garfield in forum Windows Programming
    Replies: 6
    Last Post: 01-11-2004, 01:10 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM

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