0

I'm new to C++ and I can't figure out how to use cin (or any other method) to take a string input and store it in a variable. It's frustrating that it takes just the first word of a string.

It seems that my program ignores the getline() or cin.getline() , can somebody explain what is the diference and how should I use these?

I've also tryed scanf but that doesn't work either and just stores the first word of the input string in my variable.

This is my code :

#include <iostream>

using namespace std;

int main() {

    int l;
    double m;
    string n;

    cin >> l;
    cin >> m;
    getline(cin, n);

    cout << "This is my int: " << l << endl;
    cout << "This is my double: " <<  m << endl;
    cout << "This is my string: " + n << endl;

    return 0;
}
  • cout << "This is my string: " + n << endl; that '+n' should be '< – Exceptyon Sep 27 '16 at 08:13
  • Getline gets current line, and current line in your case is empty, because you read 2 numbers from it already. You may write `getline` twice or something like that – Denis Sheremet Sep 27 '16 at 08:13

0 Answers0