When executing this line:
cin>>num_players;
the program skips leading whitespace and then reads every digit from the input, and not a byte more. You pressed enter after entering those digits, and the endline character is still waiting to be processed. If you used another cin >>, it would skip over that endline because it skips over leading whitespace (and endline is a form of whitespace)
When executing this line:
getline(cin, players[i].name);
the program *doesn't* skip whitespace: it reads every character from the input up to and including the next endline. There is an endline already, so, exactly as you said,
it stores one of the names as a blank. |
This is a common stumble for the beginners: after cin >> and before getline(), you have to dispose of the endline character:
cin.ignore();
will do just that (ignore one character)
In case someone entered a bunch of junk after the number of players and before hitting enter, you may want to use the ultimate ignore:
cin.ignore(numeric_limits<streamsize>::max(), '\n' );