Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C++
Classes: Reading in data? Using constructors?
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="fakeprogress, post: 2557186"] For a homework assignment in my Data Structures/C++ class, I have to create the interface and implementation for a class called Book, create objects within the class, and process transactions that manipulate (and report on) members of the class. Interface consists of: - 5 private variables char author[20]; char title[25]; char code[4]; int ncopies; // number of copies owned int onloan; // number of copies currently on loan - 5 public methods + 1 other method char *getAuthor; // returns (a pointer to) the author's name char *getTitle; // returns (a pointer to) the title char *getCode; // returns (a pointer to) the code of the book int getNcopies; // returns the number of copies owned by the library int getOnLoan; // returns the number of copies currently on loan void Borrow( int ); // fixed number of copies are borrowed - 2 required constructors + 1 optional constructor Book( auth, tit, cd, ncop ) // creates an object of type Book with the info specified & assumes number of copies on loan is 0 Book( auth, tit, cd, ncop, nonloan ) // creates an object of type Book with the info specified Book( ) // allows you to declare objects of type Book without initializing any values * There is an initial file of information (called initbooks) that contains the information for the initial library. This information is stored on a file (and is not to be keyed in individually). Write a function readLibrary that will read the input file and create a book for each line of information read. As each data line is read, you should allocate a new record (possibly from within an available array) and initialize the data. Each data line contains an author, title, code, and number of copies. Some data lines contain information regarding the number of copies currently on loan. You should call the appropriate constructor and intialize the book to the type of data input. * There is a file of transactions (called trans) containing transactions to be processed. Each transaction contains the following: transaction type: b (borrow) or r (return) account number: book code amount: number of copies to be borrowed/returned The appropriate object (book) corresponding to the book code should be located. The balance should be updated by the amount input. The update should be an increase (in the case of a deposit) or a decrease (in the case of a withdrawal). Write a function processTransactions that will read the file trans and process the transactions (and create a log). * Write a function printFull. This function will print the inventory of books held, including the author, title, code, number of copies in holding, and number of copies available. The sequence of this report should be in the sequence of the actual array at the time that it is printed. * Write a main function that will initialize the system and declare whatever variables are needed. You should create an array of objects of type Book (max size = 25). The main function should call the following functions: readLibrary printFull processTransactions printFull -------------------- Now. I am NOT asking anyone to do this assignment for me. I needed to type all that out so that one would know exactly what I'm talking about and be of greater assistance :) (My professor's lesson on classes & objects left much to be desired. Turning to the textbook was no help, either, because he's actually one of the author's of the book... and he pretty much teaches directly from the text. I looked up the textbook on ACCU's book reviews... and couldn't find it. So. Yeah. Not the greatest textbook.) My class interface and the implementations of subsequent methods: -------------------------------------------------------- class Book { private: char author[20]; char title[25]; char code[4]; int ncopies; int onloan; public: Book( char *auth, char *tit, char *cd, int ncop ); Book( char *auth, char *tit, char *cd, int ncop, int nonloan ); Book( ); ~Book( ); char *getAuthor( ); char *getTitle( ); char *getCode( ); int getNcopies( ); int getOnLoan( ); void Borrow( int ); void nReturn( int ); }; char *Book :: getAuthor( ) { return author; } char *Book :: getTitle( ) { return title; } char *Book :: getCode( ) { return code; } int Book :: getNcopies( ) { return ncopies; } int Book :: getOnLoan( ) { return onloan; } void Book :: Borrow( int n ) { onloan += n; return; } Book :: Book( char *auth, char *tit, char *cd, int ncop ) { strcpy( author, auth ); strcpy( title, tit ); strcpy( code, cd ); ncopies = ncop; onloan = 0; } Book :: Book( char *auth, char *tit, char *cd, int ncop, int nonloan ) { strcpy( author, auth ); strcpy( title, tit ); strcpy( code, cd ); ncopies = ncop; onloan = nonloan; } Book :: Book( ) { strcpy( author, "" ); strcpy( title, "" ); strcpy( code, "" ); ncopies = 0; onloan = 0; } Book :: ~Book( ) { } -------------------------------------------------------- Now. Onto my question: For some reason, I CANNOT figure out how to read in data. And how do I use the constructors? I'm really confused. If I declare: #define NBOOKS 40 Book library[NBOOKS]; then my readLibrary will look like this: -------------------------------------------------------- int readLibrary( Book library ) { int i, j; char boundary[] = "STOP"; j = 0; /* j counts number of records in library */ for( i = 0; i < NBOOKS; i++ ) { fscanf( lib, "%s %s %s %d %d", &library[i].author, &library[i].title, &library[i].code, &library[i].ncopies, &library[i].onloan ); if( strcmp( library[i].author, boundary ) != 0 ) j++; else break; } return j; } -------------------------------------------------------- When I compile, I get the error: no match for 'operator[]' in 'library[i]' So it's not right. What am I doing wrong? How do I make it work? Also. If I use new and delete, how do incorporate that in there? I know my questions are incredibly silly... but I don't feel like I'm actually learning anything in this Data Structures class. I seem to leave with more questions than answers :/ Thank you for any and all suggestions you can offer :)[/i][/i][/i][/i][/i][/i][/i] [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C++
Classes: Reading in data? Using constructors?
Top