ignore() function

In the following code snippet, please explain the purpose of using cin.ignore (both the times). I am not getting it from book.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
        int i;
	while(true)
	{
		cout<<"Enter an integer: ";
		cin>>i;
		if(cin.good())
		{
			cin.ignore(10,'\n');
			break;
		}
		cin.clear();
		cout<<"Incorrect input";
		cin.ignore(10,'\n');
	}
	cout<<"Integer is "<<i<<endl;

Thank you
I request you to please explain it in a simpler language.I tried from the above link before posting the question but i was not getting clear picture from it.
This sounds like a homework question.

We're not going to do your homework for you. If you have specific questions about the material linked above, then ask your specific questions and we will try and help.
Speaking respectfully,

This is not at all a homework question.
This is just a code given in a book which i was reading.
As i was not getting from there , so i asked here.
On this forum, no one is forcing anyone to answer any question.
Everyone is answering with his or her own wish.

Well, I will try my best to be more specific from next time.
it's just making sure the stream is cleared by the time you get to line 15. That's all it's doing really.

it's done twice to make sure it's happening in both possible code paths the program could take. It doesn't have to be done twice. It could be done like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
        int i;
	while(true)
	{
		cout<<"Enter an integer: ";
		cin>>i;
		if(cin.good())
		{
			break;
		}
		cin.clear();
		cout<<"Incorrect input";		
	}
        
        // clear the stream before proceeding with the rest of the program.
        cin.ignore(10,'\n');
	cout<<"Integer is "<<i<<endl;
Thankyou,
But I still have a doubt.What does the number 10 as a argument indicates?
@mutexe Your example code doesn't work. Let's say the user entered a letter of the alphabet instead of a number when requested. cin.good() will be false, so the break isn't executed. Then at line 10, cin.clear() resets the flags for the cin stream. But the letter of the alphabet typed by the user is still in the buffer, so the user doesn't get a chance to try again. Instead it fails again and enters an infinite loop. Oops.
Last edited on
Referring to your original code:

Lets say the user enters "12AB" at line 5. The istream input operator will parse out the "12" and store that in i. That leaves "AB\n" in the input buffer which has not been read. The cin.ignore at line 8 will discard everything in the buffer up until the \n or 10 characters have been discarded, presumably leaving the buffer empty. If the user had entered more than 10 characters after the "12", then the buffer would not be fully cleared.

Line 13 does the same thing, but only if cin's goodbit was not set (line 6 was false).


What does the number 10 as a argument indicates?

The meaning of the parameters is described on the reference page(linked previously).

10 is the maximum number of characters to extract (and ignore).
'\n' is the delimiter.

The ignore function here will read and discard up to 10 characters. If the newline character is found before that number is reached, the newline itself is ignored and then it stops.

Last edited on

Referring to your original code:

Lets say the user enters "12AB" at line 5. 
The istream input operator will parse out the "12" and store that in i. 
That leaves "AB\n" in the input buffer which has not been read.
 The cin.ignore at line 8 will discard everything in the buffer up until
 the \n or 10 characters have been discarded, 
presumably leaving the buffer empty. If the user had entered
 more than 10 characters after the "12", then
 the buffer would not be fully cleared.

Line 13 does the same thing, but only if cin's goodbit was not set (line 6 was false).

If suppose user enters more than 10 alphabets after integer, then will it create any problem while entering inputs?
Yes. If the user enters 12 alphas after the "12", then only the first 10 would be removed. That would leave 2 alphas and the \n in the buffer.
A subsequent input operation would start from those two remaining characters in the buffer.

The intent here is to leave cin's buffer empty so that subsequent code can input from cin without worrying about anything left over from previous input operations. It's normal to specify a number larger than the longest input the user might enter.
Okay.Thanks all.I got it.
Topic archived. No new replies allowed.