0

I have got this C++ code

cin>>a>>b;
getline(cin,str2);

Where a - Int,b - double and str2 - string.So when my input should be

12 4.0 best coding website

My program only takes 12 and 4 and then exits the program taking str2 as an empty string

1 Answers1

4

There's still a new line in the stream. getline will fall for it and return "". Just use:

cin.ignore();

That will ignore the persisting newline and move on to the data you want.

Charles
  • 1,307
  • 11
  • 16