c++ - Can't read first string -
here code
#include<iostream> #include<cstring> #define limit 25 using namespace std; int main() { int te; //number of test cases cin>>te; while(te) { char m[limit]; char w[limit]; cin.getline(m,limit); // line not getting executed reason cin.getline(w,limit); cout<<"m "<<m<<" "<<endl<<"w "<<w<<endl; te--; } }
for god knows reason, machine refuses read m first test case. reads , prints values both m , w in other cases, first case, refuses read m.
sample:
input 1 hello m w hello 2 hello m w hello stack overflow m stack w overflow
cin>>te;
this extract 1
input stream , stop @ not extract \n
. you'll need ignore()
character, otherwise next line extraction read empty line.
cin.ignore();
or ignore all characters , including next \n
character (in case inputs 1foo
or something), can do:
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
Comments
Post a Comment