second cin.getline is ignored in some cases

Hi, i don't understand how cin and cin.getline work exactly. I had problem with cin, which was solved here http://www.cplusplus.com/forum/beginner/88267/ , but still noone explained me how it works exactly. Today i stepped into another problem. I was writing a strstr function that scans one string for another string. It looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int m_strstr(char* base, char*targ)
{
	char* targbuf = targ,* basebuf = base;
	while (*base)
	{
		if (*base==*targ)
			targ++;
		else
			targ=targbuf;
		if (!(*targ))
			return (int(base) - int(basebuf));
		else
			base++;
	}
	return -1;
}

It works perfectly no matter what. The problem is in mother-function of strstr in which user is asked to type base and targ strings. It looks like:
1
2
3
4
5
6
7
8
char l1[40]={' '}, l2[40]={' '};
	cout << "Gimma string to scan, yo! ";
	cin.getline(l1,10);
	cout << "Na gimma string dat i look 4. ";
	cin.getline(l2,30);
	int bit = m_strstr(l1,l2);
	cout << "Stry' pos': " << bit;
	getch();

It also works great assuming that l1 has no spaces inside. If it has, second cin.getline is ignored and output looks like that:
Gimma string to scan, yo! Chickens ain't bad
Na gimma string dat i look 4.
Stry' pos': 0

And everytime i run it again (during one stand of program) it looks just like:
Gimma string to scan, yo!
Na gimma string dat i look 4.
Stry' pos': -1
.

I have already figured out that it is not because of spaces, it's because of the fact that second argument of cin.getline is smaller than length of string i typed here. But still, why does it behave so strangely?
Last edited on
Try and put cin.get() before the getline that is being skipped.

See if it stops. Let me know if that works

If you meant
1
2
3
4
5
6
7
8
9
char l1[40]={' '}, l2[40]={' '};
cout << "Gimma string to scan, yo! ";
cin.getline(l1,10);
cin.get();
cout << "Na gimma string dat i look 4. ";
cin.getline(l2,30);
int bit = m_strstr(l1,l2);
cout << "Stry' pos': " << bit;
getch();

it didn't work. But changing 10 to 40 did :)
1
2
3
4
5
6
7
8
char l1[40]={' '}, l2[40]={' '};
	cout << "Gimma string to scan, yo! ";
	cin.getline(l1,40);
	cout << "Na gimma string dat i look 4. ";
	cin.getline(l2,30);
	int bit = m_strstr(l1,l2);
	cout << "Stry' pos': " << bit;
	getch();

Could someone explain me this strange behavior with returning 0 and after that -1?
Last edited on
This is the user input: "Chickens ain't bad".
This line of code reads that input: cin.getline(l1,10);. That code specifies the maximum length to be read from cin is 10 characters, including the null terminator.

Because the size limit (10) is reached before the newline character '\n' is found, the operation was deemed unsuccessful since it did not read the entire line. That means the cin stream error flag is set.

If you check cin.good() after the getline, you will find it is false. As a result of that, any further operation using cin will not work, until the flag is cleared using cin.clear().

The simplest solution here would be to make sure that the arrays l1 and l2 are large enough to hold the user input. In addition, make sure that the length specified in the getline is big enough (usually the same as the array size).

1
2
3
4
5
6
    const int size = 200;
    char l1[size]= "", l2[size] = "";
    cout << "\nString to scan: ";
    cin.getline(l1,size);
    cout << "\nString  to find: ";
    cin.getline(l2,size);


http://www.cplusplus.com/reference/istream/istream/getline/
Last edited on
Thank you, Chervil. Your explanation is all i need :)
Topic archived. No new replies allowed.