c++ - Reading sentences as string inputs from users -


i have been trying implement simple code takes sentence input user, stores in string , displays back.

here issues: 1. when t = 1, program exits immediately. 2. when t>1, loop runs t-1 times.

i think usage of cin store value of t issue here. value of t entered being stored string due 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; } 

how terminate input becomes t? newline. happens newline after read t? it's still left in input buffer. happen when next call std::getline, first character read? newline, , happens next? loop iterates , t 0 (for first case t 1) , loop , program exits.

the solution problem ignore characters , including newline.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -