Need getline() for xcode

Discussion in 'Mac Programming' started by lolapurple, Feb 22, 2015.

  1. lolapurple, Feb 22, 2015
    Last edited by a moderator: Feb 22, 2015

    lolapurple macrumors newbie

    Joined:
    Feb 22, 2015
    #1
    So I am writing this code for my class.

    the information in TruckTireTemp.txt is as follows:
    Code:
    65
    77
    68
    80
    59
    78
    79
    89
    58
    69

    Can somebody tell me what the output looks like on a program that recognizes getline() as a function?

    Here is the code I wrote
    Code:
    #include<iostream>
    #include<fstream>
    #include <string>
    using namespace std;
    int main()
    {
        string myline;
        ofstream myfile ("TruckTireTemp.txt");
        if(myfile.is_open())
        {
            while ( ! myfile.eof())
            {
            getline(myfile, myline);
            cout << myline <<endl;
            }
            myfile.close();
        }
        else cout << "Ooops! Unable to open file" <<endl;
        system("pause");
        return 0;
    }
    When I do this it always says "no matching function for call to 'getline'" :(
     
  2. mfram macrumors 65816

    Joined:
    Jan 23, 2010
    Location:
    San Diego, CA USA
    #2
    First, you should probably be using an 'ifstream', not an 'ofstream'.

    Second, you shouldn't be using 'system("pause")' on a Mac. That's not going to do anything. "pause" is not a valid shell command.
     
  3. Madd the Sane macrumors 6502

    Madd the Sane

    Joined:
    Nov 8, 2010
    Location:
    Utah
    #3
    If getline isn't linking, try including the stdio header.

    Edit: to force it to use the c function, you can add two colons in front of getline.

    Also, you may need change your code more, as getline doesn't take the arguments you are trying to use.
     
  4. pstoehr macrumors member

    Joined:
    Aug 5, 2003
    Location:
    Scheßlitz, Oberfranken, Bavaria, Germany
    #4
    Hi,

    for me this task looks like the homework of a programming class ;-)

    Nevertheless some hints:
    1) As mfram already mentioned you have to use ifstream as you want to get an Input stream.
    2) getline(...) is a method (C++) not a function (C). That means you have to have an instance that receives this message.
    3) Get a good book about C++.

    Best regards
    Peter
     
  5. jsagurton macrumors member

    Joined:
    Aug 6, 2014
    Location:
    Pennsylvania
    #5
    Although it seems like your problem has been solved, you should definitely get a good book. I'd recommend Tony Gaddis's "Starting out with C++". That's the book I used a few years ago when I learned C++, and I've recommended it to quite a few people that have liked it as well. I think the current edition is the 7th, but I imagine the 6th edition isn't much different (although obviously look into that) and could likely be gotten dirt cheap on half.com.
     

Share This Page