2

I am creating a basic search function for an entry level programming course and ran across this problem.

string query; 
cout<<"Enter a query: ";
cin>>query;

cout<<query<<endl; // debug line
find(query); // calls to another function
  • If I input: Wellington, Florida, I get output: Wellington,
  • If I input: Miami, Florida, I get output Miami,

It seems like cin ignores everything after the spaces and makes my function call mess up. Is there anyway around this ?

Dr.Kameleon
  • 22,145
  • 19
  • 110
  • 215
Kevin J
  • 29
  • 5
  • 3
    Shouldn't be too hard to find: http://en.cppreference.com/w/cpp/string/basic_string/getline – chris Feb 18 '13 at 04:48
  • link with example: http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1048377843&id=1043284385 – David Saxon Feb 18 '13 at 04:50
  • 1
    After it was updated, it got a bit different, but the answer you need is definitely there: [C++ cin input with spaces?](http://stackoverflow.com/questions/5838711/c-cin-input-with-spaces) – chris Feb 18 '13 at 04:50

1 Answers1

6

Try using :

getline(cin,query);

instead of cin >> query;.

Dr.Kameleon
  • 22,145
  • 19
  • 110
  • 215