Thread: Can't seem to come to conclusion : cin.ignore()

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    cin.ingore() has a default value of 1 for its parameter, so it's equivalent to cin.ignore(1). cin is your console input source, i.e. whatever the user types in. So, let's say your program prompts the user to enter their name:

    cout<<"Please enter your name: "<<endl;

    and the user entered:

    Mike\n

    (The \n is an invisible newline character that is entered when the user hits return.) Now, if you used this as your next statement in your program:

    cin.ignore(1);

    the remaining input would look like this:

    ike\n

    which you could then read into a variable that was previously declared:

    cin>>name;

    Since cin>> is defined to stop reading input when it encounters any whitespace(spaces, tabs and newlines), the variable name will contain 'ike'. That means the remaining input is:

    \n

    That remaining input can cause problems when you try to read some additional input from the user later on. Any additional input from the user gets in line behind the remaining input. cin.ignore() is often used to skip that \n that is left over in the input.
    Last edited by 7stud; 05-10-2005 at 02:20 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin.ignore()
    By valthyx in forum C++ Programming
    Replies: 2
    Last Post: 07-22-2008, 04:23 AM
  2. cin.ignore() in C
    By swgh in forum C Programming
    Replies: 10
    Last Post: 08-09-2007, 10:45 PM
  3. Console window waiting on cin.ignore() or cin.ignore(2)
    By The SharK in forum C++ Programming
    Replies: 3
    Last Post: 07-19-2006, 04:17 PM
  4. Why do we use cin.ignore()
    By himanch in forum C++ Programming
    Replies: 2
    Last Post: 02-04-2005, 01:52 PM
  5. need help with cin.get, or cin.ignore
    By yoyo in forum C++ Programming
    Replies: 5
    Last Post: 09-23-2003, 01:14 AM