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)


Most likely there is a newline in your input file, and that is being processed immediately, as explained on this page:

http://augustcouncil.com/~tgibson/tutorial/iotips.html

You may want to call cin.ignore() to have it reject one character, but, you may want to read more of the tips, as there are suggestions about how to handle reading in numbers.


This line only reads a number:

cin >> T;

If you want to parse user input you need to take into account they keep hitting <enter> because the input is buffered. To get around this somtimes it is simpler to read interactive input using getline. Then parse the content of the line.

std::string userInput;
std::getline(std::cin, userInput);

std::stringstream(userInput) >> T;