Reading in strings and int from file

HI!!
i HAVE CREATED THIS PROGRAM:
struct database //GLOBAL TYPE
{
string name;
int scores;
float weight;
};


void readData(database [], string);
void printData(database [], int); //printData prototype

int main()
{
database myCats[10]; //there are 10 cats -in an array of struct of type database
readData(myCats,"names.txt"); // read in data
printData(myCats,10); // print my cats - Note: 10 cats in all to be printed

return 0;
}



void readData(database myDogs[], string myfile) // reading in data
{
ifstream input(myfile); //declare file input to read
if (input.fail()) //check if file exist
cout << "File does not exist" << endl;


else
{ // file exist, now read in data
int row=0;
while (!input.eof()) // Continue reading until end of file eof()
{
input >> myDogs[row].name>>myDogs[row].scores >> myDogs[row].weight; //reading

row++;
}
input.close();
}
}
void printData(database myDogs[], int size) //print array myDogs
{
cout << " Name Scores Weight " << endl;
for(int row = 0; row < size; row++)
{
cout <<row<<" " << setw(5)<<myDogs[row].name<< setw(20)<<myDogs[row].scores<<setw(10)<<myDogs[row].weight<< endl;

}
}


MY question:
this works fine if my data file looks like this:
Billy 41 4.6
Jesssie 67 3.1
Eric 34 3.4
Lily 0 4.5
Jasmine 69 3.1

Problem: will not work if my data file looks like this:
Billy 41 4.6
Jesssie Lee 67 3.1
Eric Majors 34 3.4
Lily Rose 10 4.5
Wild Jasmine 69 3.1
Sarah Joe 67 3.1
Melor 34 3.4
Jimmy Long 34 4.5


(I did modify my read to look like below & modify my input file by adding character '#' after the name:):
getline(input, myDogs[row].name,'#');
input >> myDogs[row].scores >> myDogs[row].weight;


What I got from this is that myDogs[row].name contain end of line characters followed with the name read.


please help me here.

thanks

Topic archived. No new replies allowed.