views:

233

answers:

4

Hello,
I want to get input from an amount of integers,but after it get input to a string with getline(cin,stringname);
But the enter that the user gives when he input numbers to the ints,gets into the string and it doesn't get any input(sentence) to the string,only the "enter" key.
how can I solve that?
Thanks.

A: 

Code:
cin>>track.day; //Int
cin>>track.seriesday; //Int
getline(cin,track.comment); //String


The string fill itself with the "enter" key and "skips" getting the string from the user,because of the enters pressed when the program requested input to the int.
so how can i clear the cin(cin.clear() don't work) so the string won't fill itself with "enter" key?

A: 

Can you please paste the code where you read the integers? Also, what compiler are you using, and on which platform?

dguaraglia
+1  A: 

I think that your cin of the ints is not reading the new line before the sentence. cin skips leading whitespace and stops reading a number when it encounters a non-digit, including whitespace.

So:

std::cin >> num1;
std::cin >> num2;
std::cin.ignore(INT_MAX, '\n'); // ignore the new line which follows num2
std::getline(std::cin, sentence);

might work for you

Evan Teran
Yes it works thank you very much!
A: 

it's a normal input reciving,nothing special at the top of the code,
i had a problem like this but I forgot the solution
i need to clear the cin someway so the string won't get filled with "enter" key.