Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

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:
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.
 
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.
 
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
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.