This is what i have, but the program doesnt wait for user input after "Name: " which is a bit confusing for me. I've never really used getline before, so am i missing something here? thanks in advance
cout << endl << endl << "What name would you like to store in element " << (list1->pHead->elementNo) + 1 << "?" << endl;
cout << "Name: ";
you put a `cin` before this code segment, right?
what's the rest of your code?
my guess is that you probably have a cin or get() somewhere above that.
just make sure that after every cin or get you have a line that says:
both cin and get() leave a character in the stream, which may trip things up later on in your program. cin.ignore(1); takes in and kills that character.
also, try this:
that's much more efficient than using endl so many times. endl not only puts a newline ('\n'), but it also flushes the buffer, which you don't need to do that often.
cout <<"\n\nWhat name would you like to store in element " << (list1->pHead->elementNo) + 1 <<"?\nName: ";
The code above is:
Which includes a cin, as you said. So i should put cin.ignore(1) in the line below it? I'm a little confused as to why, but ok, lol.
cout << "The list currently has " << list1->pHead->elementNo << " people in it." << endl << endl;
cout << "What would you like to do?" << endl;
cout << "1. Add a member to the list" << endl;
cout << "2. View current members in the list" << endl;
cout << "3. Delete a member from the list" << endl;
cout << "4. Search for a member from the list" << endl;
cout << "5. Quit" << endl;
cout << "Choice: ";
cin >> choice;
I tend to use endl rather than \n because i find it more readable, but i'll bear that in mind, thanks
yeah. what's happening is say a user enters 1. what's now in the stream is a '1' and a '\n'. what happens is cin reads in the '1', but stops at (and leaves) the '\n'. so when you get to the line with getline, if they enter "foo", the stream now contains '\n' 'f' 'o' 'o' '\n'. getline stops at the first '\n',' ',or '\t' it finds (they're called whitespaces).
when you call cin.ignore(1);, it takes in and destroys however many characters you tell it to (in this case 1). after cin, if you call cin.ignore(1), it'll take in and destroy that '\n' it left over.
like I said, cin and cin.get() leave the terminating character in the stream. getline() takes it in and destroys it. a call to getline() is almost like a call to get() followed by a call to ignore(1).
about the \n vs. endl thing, it really does make a difference in programs with alot of output.
>getline stops at the first '\n',' ',or '\t' it finds (they're called whitespaces).
That's the behavior for >> or get(). getline() stops at the first terminating char, EOF, or the number of char indicated as the second paratmeter--if you are using the version of getline() used for C style strings. The default terminating char for getline() is the newline char, but any valid char will work. If the default terminating char for getline were set as something other than newline (or other whitespace char), then the problem of skipping over input would disappear as well, although there may still be an undesireable whitespace char as the first char in the input stream buffer, but that's a related story.