0

My goal is to read a string input after reading an int input. The problem is I'm either getting an error or getline() isn't waiting for input from console.

I tried the following

Original method:

int num;
string name;
cout << "Enter number: ";
cin >> num;
cout << "Enter name: ";
cin >> name;

However if I enter name which has multiple words, the prompt won't take any more inputs.

Second method:

I tried this

int num;
string name;
cout << "Enter number: ";
cin >> num;
cout << "Enter name: ";
getline(cin, name);

But according to this, this method is wrong.

Method 3:

Finally I tried this

int num;
string name;
cout << "Enter number: ";
//cin >> num;
getline(cin, num);
cout << "Enter name: ";
getline(cin, name);

But it gives me error error: no matching function for call to 'getline'

Biffen
  • 5,354
  • 5
  • 27
  • 32
Amit Kumar
  • 468
  • 2
  • 6
  • 15
  • Use [`ignore`](http://en.cppreference.com/w/cpp/io/basic_istream/ignore) to read and discard the newline from the numeric input. Calling [`getline`](http://en.cppreference.com/w/cpp/string/basic_string/getline) with wrong arguments won't help you. – Some programmer dude Sep 05 '18 at 07:59
  • `std::getline` takes a `string&` as its second parameter, so `std::getline(cin, num)` can't find a matching definition. – Zinki Sep 05 '18 at 08:00
  • [this](https://stackoverflow.com/a/22660390/5767939) answer helped! Thanks – Amit Kumar Sep 05 '18 at 08:08

0 Answers0