Mayank Baiswar
Mayank Baiswar

Reputation: 595

taking input in c++ after getline

I'm facing some issues in taking input in C++ after using getline. Following is the piece of code:

    cin>>n;

    vector<string>v(n);
    string s, type;
    bool reversed;

    getline(cin, s);

    for(int i=0;i<n;i++) {
        getline(cin, s);
        v[i]=s;
    }

    cin>>key;
    cin>>reversed;
    cin>>type;

for the following input --

3
92 022
82 12
77 13
2 true digital

What I need is the following lines to be taken as string with spaces--

92 022
82 12
77 13

which I'm able to, but the value of reversed is always 0 and type is always blank. Please help what I'm doing wrong in taking input?

I also had to use getline once before for loop to take the input for "n" string correctly. How to avoid that?

Upvotes: 0

Views: 93

Answers (1)

rustyx
rustyx

Reputation: 85541

Unless you set the boolalpha flag, cin >> bool expects 0 or 1.

To use literals "true" / "false", set boolapha like this:

  cin >> boolalpha >> reversed;

Upvotes: 2

Related Questions