0

When I use cin to get a string from the user, then use getline() after it, the compiler skips over getline() and doesn't get input from the user.

cout << "What is your name: ";
string name;
cin >> name;
cout << "What is your favorite book: ";
string book;
getline(cin, book);

Output:

What is your name: Example Input
What is your favorite book:
Program exists

I tried using cin.ignore() but I don't know what to put exactly in the parameters.

  • 1
    Change `cin >> name;` to `getline(cin, name);` – Eljay Jun 29 '21 at 12:22
  • 1
    string book; string name; std::cout << "What is your name: "; std::cin >> name; std::cout << "What is your favorite book: "; std::cin.ignore(); std::getline(std::cin, name); – sam2611 Jun 29 '21 at 12:26

0 Answers0