getline function

Hi
I am trying to read numbers from a file using getline.My main aim is to read the first number and store the number zombies struct as the number of zombies .The second and third stores the health and distance respectively.
if the file has 2 4 5 7 6
then there a 2 zombies, the first has a health of 4 and distance of 5.the secong a health of 7 and a distance of 6.



ifstream inFile;
std::getline(inFile, zombieCount);

bool end;
while(!end)
{
std::getline(inFile, health);
std::getline(Infile, distance);

zombie[count].health = health;
zombie[count].distance = distance;
count++;
}
Thanks in advance.
maybe try while( !inFile.eof() ) instead of yours because you never actually set end to be true or false and then you are saying when it is false then to do the loop. and never actually set it equal to true at the end.

ps eof means end of file
the while loop is not a problem, the problem is using the getline function to retrive data from a file.
Even if i change the loop is still det an error

i get this error
" error: no matching function for call to ‘getline(std::ifstream&, int&)’"
anyways why are you using getline because that gets the whole line I think maybe try something like
1
2
3
4
5
6
7
8
9
10
11
unsigned int count = 0;
std::ifstream inFile;
inFile >> zombieCount;
while( !infile.eof() )
{
     inFile >> health;
     inFile >> distance;
     zombie[count].health = health;
     zombie[count].distance = distance;
     count++;
}


http://www.cplusplus.com/reference/istream/istream/getline/
Last edited on
That error is saying that you are using an int variable, zombieCount, when the function needs a string variable. You will have to create a string variable and then convert to int/double.

You can also put in a third part if you wanted to stop at a space or a new line
getline(inFile, zombieString, '\n');
Topic archived. No new replies allowed.