-
I/O headache
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:
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;
}
it's in five files. book class header and implementation, library class header and implementation, and the main file.
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
-
Hard to say when you miss out so much functionality from your post.
-
Need more code, especially your classes. You mention a default constructor in your comment, "default constructor sets size = 1" but you don't include that code. Also you seem to have a resize member function that I can guess resizes your dynamically allocated memory but I don't see any code for that either. You are entering two items into your Library class but you do not call this resize function even though your comment says the constructor only allocates memory for 1 object. Bottom line: we are missing the whole picture with the limited amount of code you are showing.
Also, could you simply use an STL container for this program instead of the Library class? You wouldn't need to worry about the memory management aspects if you did use one.
-
I accidently left the resize function out, and I think my default constructor is fine, but it's up there now
if you need any more code let me know, I just don't want to post up more than you really need
also, I don't know what an STL container is. this is an assignment for a class and we're supposed to be learning to use pointers and such things
-
I don't know what your input file looks like, but assuming each title start on a new line, you probably need an ignore in your fill() method to eat the newline. This is where some cout's would help, to be sure everythings happening correctly.
Code:
void Book::fill(istream& ins)
{
getline(ins,title);
getline(ins,author);
ins >> copies;
ins >> price;
ins.ignore(std::numeric_limits < int >::max(), '\n');
}
-
sweet, it's working now. thanks, I completely forgot to put ignore in there