vendredi 11 août 2017

getline and cin.ignore skipping one character in while loop?

#define t() int test; cin>>test; while(test--)


int main() {
    t() {
        string a;
        stack<char> s;
        bool flag;
        cin.ignore();
        getline(cin,a,'\n');
        cout<<a[0];
        //more code
     }
   }

I do not understand why is getline skipping one charracter for the second time. For example, if I take the input

2
<<>>
<>

The output coming is:

<
> // skips '<'

If I use getline without cin.ignore, it just simply skips the input.

Aucun commentaire:

Enregistrer un commentaire