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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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 ;-)

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, 02:10 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM