Why getline skips first line?

cin >> T;

This consumes the integer you provide on stdin.

The first time you call:

getline(cin, line)

…you consume the newline after your integer.

You can get cin to ignore the newline by adding the following line after cin >> T;:

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

(You’ll need #include <limits> for std::numeric_limits)

Leave a Comment