![]() |
| | #1 |
| Registered User Join Date: Jan 2005
Posts: 46
| I/O headache 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 Last edited by Strait; 02-04-2005 at 09:57 AM. |
| Strait is offline | |
| | #2 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,639
| Hard to say when you miss out so much functionality from your post. |
| Salem is offline | |
| | #3 |
| Registered User Join Date: Jan 2002 Location: Northern Virginia/Washington DC Metropolitan Area
Posts: 2,862
| 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.
__________________ On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. --Charles Babbage, 1792-1871 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0 |
| hk_mp5kpdw is offline | |
| | #4 |
| Registered User Join Date: Jan 2005
Posts: 46
| 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 |
| Strait is offline | |
| | #5 |
| Registered User Join Date: Oct 2001
Posts: 2,936
| 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');
}
__________________ http://www.freechess.org |
| swoopy is offline | |
| | #6 |
| Registered User Join Date: Jan 2005
Posts: 46
| sweet, it's working now. thanks, I completely forgot to put ignore in there |
| Strait is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Raw I/O vs. Stream I/O | NuNn | C Programming | 1 | 03-17-2009 08:32 AM |
| Buffered/UnBuffered I/O | valaris | C Programming | 1 | 08-06-2008 11:31 PM |
| asynchronized I/O == multiplexing I/O? | George2 | C Programming | 1 | 07-24-2006 10:06 AM |
| why page based I/O can improve performance? | George2 | C Programming | 1 | 06-12-2006 07:42 AM |
| Overlapped I/O and Completion Port :: Winsock | kuphryn | Windows Programming | 0 | 10-30-2002 05:14 PM |