Need help with getline function

This is a discussion on Need help with getline function within the C++ Programming forums, part of the General Programming Boards category; Basically what I want the program to do is to open up a file, grab the number at the end, ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    45

    Question Need help with getline function

    Basically what I want the program to do is to open up a file, grab the number at the end, and then output it into another file using getline and the stream extraction operator >>.

    I have set up all the basic stuff like setting up the header already. What I am having problems with is getting the "5" into it's own variable in this line:

    Code:
     "X-Cord": 5 ,
    Note there is a space in front of "X-Cord" and a space between the 5 and the comma at the end.

    To further clarify what I mean by 5 into it's own variable, I want to separate each word into a variable such as a, b, and c using the getline and >> operator.

    How would I do that?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    The getline function normally reads a line, although it can be made to stop at any character you give it. What line were you thinking getline should read?

    The >> operator reads in text until it hits whitespace. So if you have code that reads up to the ':', then use operator>> to read into an int, you'll grab the 5.

    It sounds like you might be wanting to use getline to get the entire line into a string, then use a stringstream to get each part out. If that's the case, the above comment still applies, but you have to use a stringstream with operator>> instead of a file stream.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    45
    "The >> operator reads in text until it hits whitespace. So if you have code that reads up to the ':', then use operator>> to read into an int, you'll grab the 5."

    Yes that is exactly what I want to do. How would I incorporate >> into getline? Isn't that usually associated with cin?

    Could you give an example.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    You wouldn't translate it into getline, whatever that means. You use getline first. Then use operator >>.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    It's not about incorporating >> into getline. The getline function reads in a line. The operator>> reads in a word or number. Is there a reason you need to do both? Are these instructions from an assignment or something?

    It's hard to give a helpful example if I don't exactly know the rules you have to follow, other than providing simple examples that can be found through a search.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    45
    Thank you for helping me thus far.

    Well my assignment is to assign the 5 or any number at the end of the line into an int variable.

    Here is a hint my teacher gave me:
    The simplest way to clear out all the characters except the integer at the end is to declare some "junk" variables and use a combination of the stream extraction operator (>>) and the getline function to read past the irrelevant characters. You can ignore the junk variables.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Ok. So if you know that operator >> stops at any space and getline stops at the end of the line (by default) then you should be able to determine in normal logic how to grab that five. Answer these questions for yourself and you should be able to get closer to the right code.

    What's the first piece of junk that you will read in and discard?
    What's the next piece of text you will read in? Is it junk or is it the 5?
    How will you read in the rest of the text after the 5?

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    45
    Ah I see. Thank you for helping me. I got the code working now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sudoku Brute Force Algorithm Help (recursion?)
    By ridilla in forum C Programming
    Replies: 22
    Last Post: 05-07-2010, 04:31 PM
  2. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  3. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM

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