No doubt you'll be surprised to find that your problem DOESN'T lie in your getline() statement -- which is working perfectly. Rather it lies in the earlier cin statement;
1 2
|
cout << "Do you have a passenger that you wish to enter? Yes or No? ";
cin >> morePassengers;
|
The problem is that here you're using cin to read a string - but when reading a string cin stops reading as soon as it encounters any white space (tab, return, or space).
Keep in mind here that the user is not responding to this question merely by typing "Yes" or "No", but by typing "Yes<return>" or "No<return>" and this is what the computer is working with.
What's happening is that cin is taking "Yes" or "No" -- reading up to but not including <return> -- and then moving on leaving <return> in the input buffer.
So when your program gets to;
all the computer sees is a nice fat <return> sitting in the input buffer. And as getline() reads up to and including <return> then it responds perfectly; reading a string of zero characters, ending in a <return> character (which it discards). The computer THINKS that you actually meant to do this. So your getline() is, as I said earlier, working perfectly fine.
Just for interest sake, try responding to your original "Yes/No" question by typing "Yes thanks very much" -- you'll see that your passenger is assigned the name " thanks very much" because cin reads "Yes" -- stopping at the first space and leaving " thanks very much<return>" sitting in the input buffer. getline(cin, name) then reads everything up to and including <return>, discards <return> and assigns " thanks very much" to the string "name".
What you need to do is to flush this <return> from the input buffer -- which you could do in at least two ways;
1. Don't use cin << morePassengers. Change it to;
|
getline (cin,morePassengers);
|
Or...
2. Flush the input buffer by reading a single char from the buffer after your call to cin;
1 2 3
|
cin >> morePassengers;
char a;
cin.get(a);
|
Given the flexibility that C++ allows when it comes to input, there's probably another dozen ways to do this, but both the above should do the trick -- although they will give you OTHER problems you probably haven't thought about yet! You need to keep in mind that EVERYTHING you type goes into the input buffer and whatever is NOT removed by any particular function call just sits there to be taken up by the NEXT function call that looks at the input buffer. I/O function calls, in other words, read from the input buffer EXACTLY what their specifications say they do and terms like "reads a single char", "up to the first white space", "up to and including <return>", "discarding <return>" and so on, need to be kept in mind when using I/O functions.
I'd suggest you read up a bit on C++ I/O and the input buffer -- Google is your friend.
BTW: if your wondering why getline() in your previous programs seemed to work fine - I'd guess that you DIDN'T have a call to cin before calling getline() so this buffer problem probably wasn't occuring.