samedi 28 octobre 2017

read line until white space in C++ using getline()

I am new to C++ and I have trouble reading two lines separated by a space into two strings. Part of my code is as following:

for(int i = 0; i < T; i++){
        string pre_order;
        getline(cin, pre_order);
        string in_order;
        getline(cin, in_order);
}

And input is something like this: 1 BCAD CBAD

I would like pre_order string to be BCAD and in_order string to be CBAD.

However, now I would get something like pre_order="BCAD CBAD" and in_order = "". I also tried something like getline(cin, in_order," "); to identify delimiter, but it was still not working. Could someone tell me how to fix this? Thanks!

Aucun commentaire:

Enregistrer un commentaire