Hey guys.
Im having some trouble with reading from a text file, into a class with strings and ints.
Here is my code for my read file function:
ifstream infile;
infile.open("books.txt");
if (infile.good())
{
for (int i=0; i<=5;i++)
{
getline(infile, book[i].title); //string
getline(infile, book[i].isbn); //string
getline(infile, book[i].publisher); //string
infile >> book[i].year; //int
infile >> book[i].price; //double
infile >> book[i].copies; //int
infile >> book[i].numAuthor; //int
getline(infile, book[i].author); //string
}
infile.close();
}
else
cout << "Error opening file!" << endl;
It seems to work fine reading from the file until it gets to 'numAuthor'. It reads that int in, then nothing happens for 'author'. It automatically skips to the next book, and then reads in the Author of book1, as the title of book2. Here is the output:
TITLE: Book1 Title
ISBN: 1234567
PUBLISHER: ABCDEF
YEAR: 2005
PRICE: $22.5
COPIES: 2
NUMBER OF AUTHORS: 1
AUTHORS:
TITLE: Author1, Author
ISBN: Book2 Title
PUBLISHER: 7654321
YEAR: 1900
PRICE: $0
COPIES: 0
NUMBER OF AUTHORS: 0
AUTHORS: Default Author
Then from there it stuffs up and just displays the default values set by the default constructor for the class.
What am i doing wrong?
Any help would be greatly appreciated.
Cheers!