1

Disclaimer: I am very new to C++; Java is my skillset.

In the program I'm writing, I need to compare two strings, as follows:

#include<string>
#include<iostream>

using namespace std;

int main()
{
  string full_name = "John Doe";
  string find_name;

//User inputs "John Doe"

  cout << "Enter the name of the person to search for:" << endl;      
  **cin >> find_name;//THIS IS THE ISSUE I HAVE**

  if(find_name == full_name) //or some other compare function. NOT THE ISSUE.
      action_do_something;


return 0;
}

I understand that the buffer takes only "John" and "Doe" is a second, unrelated command. How can I stop the buffer from cutting off the second name? (Some names are 5 names long, some are just 1)

I've been fussing with getline(), but I guess I don't fully understand it--It doesn't wait for the input before plowing ahead.

Thanks in advance!

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
C-Love511
  • 358
  • 1
  • 7
  • 24

1 Answers1

2

Use standard function std::getline. For example

std::getline( std::cin, find_name );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335