getline()
will read one line of text. It can’t read directly an int. This is why you get your error message.
You have to be aware that there are two getline()
. There is one which is istream::getline()
and std::getline()
. Both have different signatures. The first is a member function of a stream and is defined in the stream header; the latter is defined in the <string>
header.
But pay attention: the return value of std::getline()
is not an int
! It’s a stream reference. This is why you get a second compiler error.
Finally if you want to read an integer x, it’s easier to use extractors:
int value; fruitFile >> value; fruitFile.ignore(SIZE_MAX, '\n'); // in case you'd need to go to next line
Or if you really want to read an int in a full line:
string line; getline(fruitFile, line); stringstream sst(line); // creates a string stream: a stream that takes line as input sst >> value;