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="Jonathan Mcdougall, post: 2557190"] This seems to be quite of an assignment for you, and seeing the code you provided, you don't seem to be ready for it. However, I'll do my best. Note that your teacher seems to teach C more than C++. Specifically: 1) c-style strings are used instead of std::string 2) arrays are used instead of standard containers 3) problems with const-correctness Since your class is in C++, I'll convert that program in C++. These should be std::string author; std::string title; std::string code; These should be const std::string& getAuthor() const; const std::string& getTitle() const; const std::string& getCode() const; int getNcopies() const; int getOnLoad() const; These can be merged by giving nonload a default value (such as 0). And what does that give? Not all classes should have default constructors. A book with no title or author or code is not a book. Scrap that one. There don't seem to be a format for that file. You'll have to devise a simple one, such as a line separated list. Each book element is on a line so you'll have five lines per book. That'll make it easier to read strings containing spaces or commas (such as a book title). So your file could contain b 1234 2 // borrowed two copies of book 1234 That's easy to parse. That should be simple. I hope you're not :) # include <string> std::string author; std::string title; std::string code; int ncopies; int onload; Book( const std::string& auth, const std::string& tit, const std::string& cd, int ncop, int nonload=0); Forget that one. You don't need a destructor. Make these const std::string& getAuthor() const; const std::string& getTitle() const; const std::string& getCode() const; int getNcopies() const; int getOnLoan() const; Take the habit of mentionning the parameters name, even if the compiler doesn't use them. void Borrow(int qty); void nReturn(int qty); const std::string& Book::getAuthor() const { return author; } And do the same for the other member functions. Don't! I won't start a discussion concerning arrays, they are evil. Use a vector: typedef std::vector<Book> Library; int main() { Library lib; readLibrary(lib); Change that to int readLibrary(Library& lib) { [i][i][i][i][i][i] Now now now, how about some C++ here? As I said, putting book values on different lines will make it easier. // data example, books.dat Bjarne Stroustrup The C++ Programming Language, 3rd Edition 0001 10 2 # include <fstream> # include <sstream> int readLibrary(Library& lib) { // open the file std::ifstream ifs("books.dat"); // we'll detect EOF inside while (true) { // string values from the data file // copies and load have a _s suffix (string) // because they are temporary objects // they will be converted to ints later on std::string title, author, code, copies_s, loan_s; // get five lines getline(ifs, title); getline(ifs, author); getline(ifs, code); getline(ifs, copies_s); getline(ifs, loan_s); // if we were at the end of file, // or if there was any problems, // one of the calls to getline() failed. // we check it here if (!ifs) break; // istringstream is useful for // converting strings to integers std::istringstream iss; // convert copies_s to an int int copies = 0; iss.str(copies_s); iss >> copies; // convert loan_s to an int int loan = 0; iss.str(loan_s); iss >> loan; // create the book and add it // to the library lib.push_back(Book(title, author, code, copies, load)); } } As an example, here's how to output all the books in the library void f(Library& lib) { for (Library::iterator itor=lib.begin(); itor!=lib.end(); ++itor) { Book& b = *itor; std::cout << "Title: " << b.title() << "\n" << "Author: " << b.author() << "\n"; } } Jonathan[/i][/i][/i][/i][/i][/i] [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C++
Classes: Reading in data? Using constructors?
Top