Need getline() for xcode

lolapurple

macrumors newbie
Original poster
Feb 22, 2015
1
0
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'" :(
 
Last edited by a moderator:

mfram

macrumors 65816
Jan 23, 2010
1,056
138
San Diego, CA USA
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.
 

Madd the Sane

macrumors 6502a
Nov 8, 2010
507
39
Utah
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.
 

pstoehr

macrumors member
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
 

jsagurton

macrumors member
Aug 6, 2014
43
7
Pennsylvania
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.