getline() Trouble...

Hi!

I'm having problem with getline(). Here's a method in my program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void CarRegister::searchRegister(int choice)
{
	system("clear");
	std::cout << "\nMata in ditt sökord: ";
	std::cin.ignore();
	getline(std::cin, searchWord);
	std::cout << searchWord;
	if (choice == 1)
	{
		for (int i = 0; i < carRegister.size(); i++)
		{
			if (carRegister[i].getBrand().compare(searchWord));
			{
				showRegister(i);
			}
		}
	}
}


When the user typed in something I get the error: ...."pointer being freed was not allocated". But in other part of my program
1
2
cin.ignore();
getline(cin, string);
have worked just fine. What's wrong? And believe me - I have googled after a solution for a while. That's why I'm asking here.

PS: If someone has a better solution for my search function, please post a suggestion.
Where is searchWord defined? What about carRegister? There is no way to understand the problem with a complete example that demonstrates the problem.

Read this:
http://cplusplus.com/forum/beginner/1/
searchWord is defined as a private member in the class CarRegister: std::string searchWord;. In the constructor I assign it nothing: searchWord("");.

carRegister holds different objects of the type Car. It is not empty.
Does this error appear if searchWord is a local variable, not the class member?
serge:

Yes. It's the same even if searchWord is local.

I can make the error message dissaper if I pass INT_MAX as a parameter into cin.ignore(). Like this: std::cin.ignore(INT_MAX);. But after I've hit enter nothing happens. For example I type in: Volvo [enter] - just blank.
Topic archived. No new replies allowed.