-1

I am trying to read two strings from the users and output them. If I type in "Micheal Jordan" for the first input, the program won't give me a chance to enter my second string.

#include "iostream"
#include "cstring"

int main()
{
  using namespace std;
  string name;
  string dessert;

  cout << "Enter your name: \n";
  cin >> name;

  cout << "Enter your favorite dessert: \n";
  cin >> dessert;

  cout << "I have some delicious " << dessert << " for you, " << name << "." << endl;

  return 0;
}

output:

./a.out 
Enter your name: 
Micheal Jordan
Enter your favorite dessert: 
I have some delicious Jordan for you, Micheal.

The code works fine if I input a name without a space.

./a.out 
Enter your name: 
MichealJordan
Enter your favorite dessert: 
Cake
I have some delicious Cake for you, MichealJordan.

So I am guessing it's the space that creates this problem, can you guys tell me how to fix it? And I am also wondering what's going on with the space. Thank you!

Yu Zhou
  • 173
  • 1
  • 2
  • 10

1 Answers1

0

std::getline(cin, name); get the input untill the end of the line;

also if you still have the same problem you can just make the user type the first name then the last name but hopefully getline will work :)

Amr Sharkawy
  • 44
  • 1
  • 9