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

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    7

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

    I've read many things, including the tutorial here (I'm starting C++!! YAY!!), but I can't seem to get a good definition of
    Code:
    cin.ignore()
    I understand that it "reads and discards a character," but what exactly does that mean? I can recall a variable after it and it recalls fine. So can someone please give me an adequate (sp) definition, because I can not understand this!

    -SkyHi

  2. #2
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    It's not discarding values in the variable, it's discarding char left in the input stream.

    try this code, 1st with the ignore commented, then without
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    int main()
    {
    	string test;
    	cout << "Enter Your First and Last Name: ";
    	cin >> test; // type your full name with a space between 1st and last
    	cout << test << endl; //outputs 1st name, last still in inout stream
    
    	//cin.ignore(100,'\n');
    	cout << "Now enter your age :";
    	cin >> test;
    	cout << test << endl;
    	system("pause");
    	
    	return 0;
    }
    Last edited by Darryl; 05-09-2005 at 11:30 PM.

  3. #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.

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    7
    First, thanks for all the help. However, I still do not understand. What I'm getting from you, 7stud, is that cin.ignore() will discard the number of characters (being letters?)that you put in the ()s. So as you said, the input would be ike\n, so then what's the point of using cin.ignore()? It's just to take out as many characters (again, letters?) as you permit? Will it also take out numerical values?
    Sorry for my stupidness.

    -SkyHi

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    To understand how ignore works I think it helps to step back and ask how does user input get from the keystrokes of the keyboard into the variables declared in the program? As I understand it, the keystrokes are stored in a container, frequently called the input buffer, until the user hits the enter key. When the program you are using sees the enter key it enters the new line character into the input buffer and then calls input stream is indicated by the program, in this case let's use cin, but it could be an fstream in ios::in mode or an ifstream or a different istream, whatever. The program you are running also tells cin which method to use to retrieve material, read characters for material, from the input buffer. The method in turn contains information about what to do with the material in the input buffer. For example, the variable may be stored in a char array using using the >> operator which does this by igoring any leading whitespace chars in the input buffer, then reads all the chars in the input buffer into the char array until the next whitespace char is found. In addition to placing the non-whitespace char in the char array, >> removes any leading whitespace char from the input buffer, removes the char it puts into the char array variable from the input buffer, adds a terminating null char to the char array variable for you, automatically, BUT does not remove the terminating whitespace char from the input buffer. "Yippeee!," you say, "but what does that have to do with ignore()". Well, the information contained in the ignore() method of istreams tells the stream to remove however many char are in the parenthesis from the input buffer and ignore them---don't put them into any variables memory, etc. If there is no number in the () then ignore() just ignores the first char in the input buffer. If the input buffer becomes empty before the full number of char indicated in the () has been "ignored/remvoed", then ignore() will stop doing it's thing, too.

    Does that help?
    You're only born perfect.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    So as you said, the input would be ike\n, so then what's the point of using cin.ignore()?
    That's sort of like asking: "what's the point of a programming language allowing addition"? If I have a number:

    int num = 10;

    and I want to display it:

    cout<<num;

    I could say: "addition is a useless programming feature because I don't want to add anything to my variable num--I just want to display it". Well, addition allows you to do something, namely add some number to another number. If I don't want to use that capability, then I don't have to. Does that mean addition won't ever prove useful in any of my programs? There are thousands of C++ features I may not use in every program. Does that mean I would be better off if those features didn't exist?

    cin.ignore() allows you to do something. If you don't think it's useful, then don't use it. However, try this program:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int num;
    	cout<<"Enter your age: "<<endl;
    	cin>>num;
    
    	char personalInfo[100];
    	cout<<"Enter your name and address:\n";
    	
    	
    	cin.getline(personalInfo, 100); 
    	//Reads in a whole line of data-spaces and all--unlike
    	//cin>> which stops as soon as it encounters any whitespace.
    	//A line of data is deemed to end when a \n character is encountered.
    
    	cout<<"Here is the info you entered:\n";
    	cout<<num<<endl;
    	cout<<personalInfo<<endl;
    
    	return 0;
    }
    Does it work correctly? Why or why not?
    Last edited by 7stud; 05-10-2005 at 11:38 AM.

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    7
    Sorry I haven't been here in a while, but I've read all of this, and I sort-of-get it.

    7stud:

    When running your program, it doesn't even ask the second cou<<
    Code:
    char personalInfo[100];
    	cout<<"Enter your name and address:\n";
    Now, I suspect it has something to do with this:

    Code:
    cin.getline(personalInfo, 100);
    Please help further.

    -SkyHi

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Now, I suspect it has something to do with this:

    cin.getline(personalInfo, 100);
    It actually has to do with the combination of:
    Code:
    cout<<"Enter your age: "<<endl;
    cin>>num;
    
    //and
    
    cin.getline(personalInfo, 100);
    If you remember from above:
    the user entered:

    Mike\n

    (The \n is an invisible newline character that is entered when the user hits return.)
    and:
    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
    \n

    That remaining input can cause problems when you try to read some additional input from the user later on.
    In the program I posted, after this line:
    Code:
    cout<<"Enter your age: "<<endl;
    The user enters something like:

    21\n

    and this line:
    Code:
    cin>>num;
    extracts 21 from the input, but leaves the \n in the input:

    \n

    Then, the getline() function is programmed to read in a whole line of data and:
    //A line of data is deemed to end when a \n character is encountered.
    getline() also reads in the terminating \n and discards it (which is different than cin>>). So, getline() looks at the input and sees that there is already some available input, so it doesn't have to wait for the user to enter anything. Then, getline() starts reading the input, and when it reads '\n', that's its signal to stop reading, so getline() terminates while discarding the \n. Execution then continues with the next lines of the program. The result is the user doesn't get a chance to enter any input the second time.

    Can you figure out how ignore() might be useful in that situation? Try to incoporate it into the program and make the program work correctly.
    Last edited by 7stud; 05-12-2005 at 10:57 AM.

  9. #9
    Registered User
    Join Date
    Apr 2005
    Posts
    7
    Okay, I will try, but I'm still not totally getting it. By the way, THANKS for the help!

    -SkyHi

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