Reading sentences as string inputs from users

I have been trying to implement a simple code which takes a sentence as an input from the user, stores it in a string and displays it back.

Here are the issues:
1. When T = 1, the program exits immediately.
2. When T>1, the loop runs for only T-1 times.

I think the usage of cin to store the value of T is an issue here. Is the value of T entered being stored as a string due to some buffer capacity of cin?

#include <iostream>
#include <string>

int main()
int T;
std::cin >> T;
while (T--)
    std::string song;
    getline(std::cin, song);
    std::cout << song << std::endl;
return 0;

Source: c++

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.