getline ??

This is a discussion on getline ?? within the C++ Programming forums, part of the General Programming Boards category; I want to read a string from a file and then put each word into a linked list node. Would ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    4

    getline ??

    I want to read a string from a file and then put each word into a linked list node. Would I need to use getline() for this or is there another way????

    e.g THIS IS A SAMPLE

    node 1 --> THIS
    node 1 --> IS
    node 1 --> A
    node 1 --> SAMPLE


    Cheers

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Nah, I'd just use regular the regular input operator (>>).

  3. #3
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    when handeling strings getline() is usually a good idea. You could use it but you dont have too.

  4. #4
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Originally posted by RoD
    when handeling strings getline() is usually a good idea. You could use it but you dont have too.
    No, getline is a good idea when you want to get an entire line. This person only wants to get a single word at a time.


    The only reason I would use getline in this program would be to get all the input at once and then parse at a later time. This just doesn't seem necessary for this problem though.

  5. #5
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    well cin>> will only read one word because it stops at a space right? Hmm, ya guess that is the better option. I tend to use getline when i work with strings if its anything more than a quick help program.

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    4
    Just one more thing...when the user inputs a number which I get with cin>>ans will the '\n' remain in the buffer??? Because I ask the user to also enter a string straight after it, for which I was also using cin.getline()??? So will getline be taking in '\n' OR the actual string entered by the user???

    If getline is reading in the '\n'...how can I prevent it from doing so??

    cheers

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    >If getline is reading in the '\n'...how can I prevent it from doing so??

    Code:
    cin.getline(string,[int],'\n');
    it will destroy the '\n'



    Originally posted by golfinguy4
    No, getline is a good idea when you want to get an entire line. This person only wants to get a single word at a time.


    The only reason I would use getline in this program would be to get all the input at once and then parse at a later time. This just doesn't seem necessary for this problem though.
    why not use getline?

    Code:
    cin.getline(string,[int],' ');
    that would take in the first token and destroy the space...


    oh well, i'd just use cin as well...
    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

  8. #8
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Originally posted by major_small
    >If getline is reading in the '\n'...how can I prevent it from doing so??

    Code:
    cin.getline(string,[int],'\n');
    it will destroy the '\n'



    why not use getline?

    Code:
    cin.getline(string,[int],' ');
    that would take in the first token and destroy the space...


    oh well, i'd just use cin as well...
    To me, that implies that you want to get an entire line when you are really just taking a word. It would work, I just don't like the implications it gives.

  9. #9
    Registered User
    Join Date
    May 2003
    Posts
    4
    Thanx all....

    I think that I have found a solution to the lagging '\n'

    cin.clear();
    cin.ignore (BUFFER, '\n');

    seems to be doing what I want, but whether it will cause other complications is yet to be seen..


    Cheers

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Just one more thing...when the user inputs a number which I get with cin>>ans will the '\n' remain in the buffer??? Because I ask the user to also enter a string straight after it, for which I was also using cin.getline()??? So will getline be taking in '\n' OR the actual string entered by the user???
    You understand what is happening pefectly: the '\n' is left in the input stream by the >> operator. Since '\n' is the default delimiter for getline(), getline() will read in the '\n' and end, making it appear that the getline() statement was skipped. An easy solution is to use cin.ignore() after using the >> operator when you are subsequently going to use getline(). cin.ignore() has 3 forms:

    1)No arguments: A single character is taken from the input buffer and discarded:
    cin.ignore(); //discard 1 character

    2)One argument: The number of characters specified are taken from the input buffer and discarded:
    cin.ignore(33); //discard 33 characters

    3)Two arguments: discard the number of characters specified, or discard characters up to and including the specified delimiter (whichever comes first):
    cin.ignore(26, '\n'); //ignore 26 characters or to a newline, whichever comes first

    So, for your code you can do this:

    inFile>>word;
    cin.ignore();
    getline(inFile, text);
    Last edited by 7stud; 05-23-2003 at 12:19 AM.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    And to clear the input buffer completly, without any wait:
    cin.ignore(cin.rdbuf()->in_avail());
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

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, 10:50 AM
  2. getline problem
    By Bitphire in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2004, 04:42 PM
  3. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 11:12 AM
  4. getline not working right
    By talz13 in forum C++ Programming
    Replies: 11
    Last Post: 12-10-2003, 10:46 PM
  5. getline with string class in Borland C++
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-08-2003, 01:59 PM

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