getline help

This is a discussion on getline help within the C++ Programming forums, part of the General Programming Boards category; Code: cout << endl << endl << "What name would you like to store in element " << (list1->pHead->elementNo) + ...

  1. #1
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168

    getline help

    Code:
    	cout << endl << endl << "What name would you like to store in element " << (list1->pHead->elementNo) + 1 << "?" << endl;
    	cout << "Name: ";
    	getline(cin, input);
    	list1->add(input);
    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

  2. #2
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    you put a `cin` before this code segment, right?

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    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:
    cin.ignore(1);
    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:
    Code:
    cout <<"\n\nWhat name would you like to store in element " << (list1->pHead->elementNo) + 1 <<"?\nName: ";
    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.
    Last edited by major_small; 07-15-2005 at 05:28 AM. Reason: grammor
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    The code above is:
    Code:
    		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;
    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.

    I tend to use endl rather than \n because i find it more readable, but i'll bear that in mind, thanks

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    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.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    alright, thanks man

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    >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.
    You're only born perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 11:50 AM
  2. getline problem
    By Bitphire in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2004, 05:42 PM
  3. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 12:12 PM
  4. getline not working right
    By talz13 in forum C++ Programming
    Replies: 11
    Last Post: 12-10-2003, 11:46 PM
  5. getline with string class in Borland C++
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-08-2003, 02:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21