简体   繁体   中英

Error while using getline() in c++

I am using getline to read strings and print but while using that I am unable to give input so I used ignore()

Code

  int t;
  cin>>t;
  string str;
  for(int i=0;i<t;i++)     
      {     cin.ignore();
            getline (cin, str);//reading string
           cout  << str << endl;
      }
  return 0;

When using that while printing output every first character from second line of output are missing?

The std::getline function reads (but doesn't store) the newline, so in the second (and every following) iteration of the loop your call to ignore will read the first character of that line.

You should move the ignore call to before the loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM