The problem is that cin >> temp; reads the next word and leaves everything after the word still inside the stream. If you pressed enter after writing the word there will be a new line character after the word so when you call getline it will find a new line character and return right away, giving you an empty string.

You need to get rid of the new line character before calling getline. You can use cin.ignore(); to remove one character. If there can be more characters before the new line character you can use cin.ignore(numeric_limits<streamsize>::max(), '\n'); to ignore the rest of the line.