What is WRONG with my function? Loop goes crazy?

This function is having some errors, even when I use "getline" to get the string input from the user, if the input is more than one word it passes onto cin>>doubVar which terribly messes with the loop (keeps repeating the error message)

In short, why isn't getline working? Why does it mess with the next input value?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//****************************************************
//This function allows the user to input data into the class array
void inputData(Fishing *data, int size)
{
	//Dummy variables to temporarily store user-entered values
	string strVar;
	double doubVar;

	for(int i=0;i<size;i++)
	{
                //Problem is here
		cout<<"Enter data for fish #"<<(i+1)<<": "<<endl;
		cout<<"Type: ";
		getline(cin,strVar);
		data[i].setFishType(strVar);

                //Input entered into strVar messes with the validation loop of this input (if it is more than 1 word)
		cout<<"Length (in inches): ";
		cin>>doubVar;
		while(doubVar<=0)
		{
			cout<<"ERROR. Enter a positive non-zero value: ";
			cin>>doubVar;
		}
		data[i].setFishLen(doubVar);

		cout<<"Weight (in pounds): ";
		cin>>doubVar;
		while(doubVar<=0)
		{
			cout<<"ERROR. Enter a non-zero positive value: ";
			cin>>doubVar;
		}
		data[i].setFishWeight(doubVar);

		data[i].getWhatToDo();
		system("CLS");
	}
}


I tried cin.ignore() and cin.get() to no avail. What is wrong with it?
Oh I think I found the problem, I was using cin.ignore() in the wrong place

1
2
3
4
5
cout<<"Enter data for fish #"<<(i+1)<<": "<<endl;
cout<<"Type: ";
cin.ignore();
getline(cin,strVar);
data[i].setFishType(strVar);


Solves the issue. Do I always have to use it like this when I am using getline?
Topic archived. No new replies allowed.