Can someone explain this line to me?/Input help

Dec 2, 2011 at 3:15am
cin.ignore( numeric_limits <streamsize> ::max(), '\n' );

I've had some issues with input in the past, and I was given this line, which helped to get rid of some residual newline, but after the first time it goes through the while loop, it prompts the user for input himself instead of taking it from the newline. I was wondering if anyone could tell me what this line means, and possibly if they could help me with my problem
Dec 2, 2011 at 3:58am
It's like cin.ignore(10000,'\n');
It says that we go through 10000 character to first occurrence of '\n' . When it's found, simply ignore the '\n' and stop the ignore function. It simply means : Eat whatever before the target and eat the target too...So when you say cin.getline() it will read the new line.

because the getline() will stop when encounter the '\n' and if you don't use ignore,the '\n' is still in the input stream (first position) and when you use one more getline() it will input nothing.
Dec 2, 2011 at 4:14am
closed account (43RGz8AR)
Duoas said:

2) cin.ignore()
http://www.cplusplus.com/reference/iostream/istream/ignore.html

numeric_limits
http://msdn.microsoft.com/en-us/library/yefe7a47(VS.80).aspx
Describes things about basic data types, like streamsize

So basically what it is saying is "ignore all the characters you get from cin until you find '\n' or you read as many characters as a stream can hold."
Dec 2, 2011 at 11:47pm
Then why doesnt the ignore line read the newline and ignore it after it goes through the while loop one time?
Dec 3, 2011 at 12:14am
because the getline() will stop when encounter the '\n' and if you don't use ignore,the '\n' is still in the input stream (first position) and when you use one more getline() it will input nothing.
Dec 3, 2011 at 12:51am
But if theres a newline in the input stream, shouldnt the ignore line get rid of it?
Dec 3, 2011 at 1:07am
Yes ,that's why it's there :

I'll explain :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <string>
#include <iostream>
using namespace std;

int main ()
{
	char hehe[80];
	char aChar;
	cin >> aChar;
	cout << aChar << endl;
	cin.getline(hehe,79);
	cout << hehe;
	return 0;
}


You press 'A' then enter key.

The 'A' will go into the input stream and is assigned to aChar;
But since you press enter key, the '\n' exist in the input stream now and will be the next character to be inputted.
When you call cin.getline(hehe,79) It encounters '\n' hence it stops and won't let you input anything else.
Dec 3, 2011 at 2:17am
So why isnt the '\n' from the cin.getline residual?
Dec 3, 2011 at 2:21am
getline takes the whole line,including '\n'.

cin >> just take what it needs and always skips special character like \t \n \o etc...even blank space

So it leaves \n in the input stream if you press enter
Last edited on Dec 3, 2011 at 2:22am
Dec 3, 2011 at 2:47am
Oh i see. So, if that whole section of code were in a while loop, how would I go about making it to where the ignore line wouldnt ask for input after the first time it executes?
Dec 3, 2011 at 2:58am
The reason for eliminating '\n' is for make the input stream ready for a getline. (maybe more I don't know)

And the reason for the ignore line is to clean up the input stream if you've done with all the inputs and want to clean it up so it will be ready for another inputs. "Clean what's left !".

See what I rewrote to illustrate :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <string>
#include <iostream>
using namespace std;

int main ()
{
	char hehe[80];
	char aChar;
	cin >> aChar;
	cin.ignore(10000,'\n');
	cin.getline(hehe,79);
	cout << hehe;
	return 0;
}
Dec 3, 2011 at 3:03am
About the loop,run the ignore in the first line of the loop to clean it and ready for the new inputs.
Dec 3, 2011 at 3:05am
But the getline already gets eliminates the '\n', so when the theoretical while loop restarts, theres nothing in the input stream, so the user has to input a '\n' himself to get past the ignore line, and i dont what that
Dec 3, 2011 at 3:13am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <string>
#include <iostream>
using namespace std;

int main ()
{
	for (int i = 0 ; i < 2; i ++ )
	{
       //cin.ignore(10000,'\n');
	   cout << "Attemp No : " << i+1 << endl;
       char a;
       char b[80];
       cin >> a;
	   cout << a << endl;
       cin.ignore(10000,'\n');
       cin.getline(b,79);
	   cout << b << endl;
	}
}


This is one use.

1
2
3
4
5
6
7
8
9
10
11
int main ()
{
	for (int i = 0 ; i < 2; i ++ )
	{
	   cout << "Attemp No : " << i+1 << endl;
       char a;
       cin >> a;
	   cin.ignore(10000,'\n');
	   cout << a;
	}
}


2nd use : MAY BE THIS IS WHAT YOU MEANT.
Dec 3, 2011 at 3:19am
hmm... but what if the code calls for the cin >> to be outside of the loop?
Dec 3, 2011 at 3:25am
Let me get this straight, after cin >> should have an ignore.

Before cin.getline() should have an ignore.

After an getline() you don't need an ignore.


If you cin >> data the '\n' will be in the stream so the ignore will clean it for you.

And when it cleans succesfully , eats the '\n' that's left...it won't ask you for input anymore.
Last edited on Dec 3, 2011 at 3:26am
Dec 3, 2011 at 3:39am
Like this:

1
2
3
4
5
6
7
8
cin >> var;
while(true)
{
     cin.ignore(10000, '\n');
     string str;
     getline(cin, str);
     cout << str;
}


The first time it goes through, there's a '\n' in the input stream that gets cleared, but the second time the while loop executes, there is no '\n' for the ignore line to ignore, so the user has to input one himself
Dec 3, 2011 at 3:43am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <string>
#include <iostream>
using namespace std;

int main ()
{
	char var;
	cin >> var;
	cin.ignore(10000, '\n');
	while(true)
	{
	     string str;
	     getline(cin, str);
	     cout << str << endl;
	}
}


The code is corrected ! getline() will take the '\n' so it won't be in the input stream anymore. But cin won't !
Dec 3, 2011 at 3:48am
Oh duh! why didnt I think of that :P Thanks for your help :D
Topic archived. No new replies allowed.