1

I'm trying to get user input for a function. The following code works, but not when there is a space between words.

string cityNew;
string cityPrevious;
cout<<"Enter a city name:"<<endl;
cin>>cityNew;
cout<<"Enter a previous city name:"<<endl;
cin>>cityPrevious;
comNet.addCity(cityPrevious, cityNew);

I've attempted using getline(cin,cityNew) but this causes both cout statements to print before allowing for the user to input anything.

Σqu
  • 47
  • 7
  • 1
    For the most likely reason see my answer to http://stackoverflow.com/questions/35246732/class-functions-are-hit-are-miss-when-it-comes-to-working-even-though-they-are-t. – R Sahu Feb 10 '16 at 05:47

1 Answers1

1

I think there is a line break('\n') which hasn't been read in your input, so the first getline() will read an empty string.

Add cin.get() before the first getline() might be helpful.

DDoSolitary
  • 328
  • 2
  • 13