okay, I'm trying to input a bunch of data from a file then print it onto the screen. it's a little too complicated to explain so here's the code:
it's in five files. book class header and implementation, library class header and implementation, and the main file.Code://first off, here's what the data file looks like Title of a Book First Last //author's name X //number of copies XX.XX //price //there's two classes, one holds all the data for a book, the other // holds a dynamic array of books class Book { public: //bunch of accessor and set functions, only these two matter void print(ostream& outs); void fill(istream& ins); private: string title, author; int copies; double price; }; typedef Book* Bookptr; class Library { public: //big three, accessor functions, sort, search, etc only these //matter void print(ostream& outs); void fill(istream& ins, int& num); private: int size; Bookptr books; void resize(Bookptr b1, int& num); }; //here's the implementation for the ones that matter: void Book::print(ostream& outs) { outs << title << endl << author << endl << copies << endl << price << endl; } void Book::fill(istream& ins) { getline(ins,title); getline(ins,author); ins >> copies; ins >> price; } void Library::print(ostream& outs) { for (int i = 0; i < size; i++) books[i].print(outs); } void Library::fill(istream& ins, int& num) { books[num].fill(ins); if (num == (size - 1)) resize(books,size); } Library::Library() { size = 1; books = new Book[size]; } void Library::resize(Bookptr& b1, int& num) { Bookptr temp; temp = new Book[size + 1] for (int i=0; i < size; i++) temp[i] = b1[i]; //assignment operator is overloaded delete [] b1; b1 = temp; size++; } //basically I'm just testing these for now, the library fill function //will eventually have a loop in it, but for now I need it to work // here's what I'm doing in my main file int main() { ifstream fin; Library l1; //default constructor sets size = 1 int i = 0; fin.open("database.dat"); l1.fill(fin, i); //I'll eventually use a loop, but this is for testing i++; l1.fill(fin,i); fin.close(); l1.print(cout); return 0; }
I get no syntax or run-time errors, and the first time I try call fill it works perfectly and prints it perfectly. but the second time it gets the title and then nothing after that. see if you can find anything wrong with this. I'll keep trying in the mean time



LinkBack URL
About LinkBacks



I used to be an adventurer like you... then I took an arrow to the knee.