//---------------------------------------- // Proj3.C // // A first try at a solution to project 3 // // Dennis L. Frey CMSC 202, Fall 2002 // // This solution to project 3 does NOT satisfy // the requirement for dynamically allocating // all local variables. It does satisfy the // dynamic array requirement for the book // and patron arrays in the Library class // // It's meant to show a reasonable approach // to the project if the pointers were not required. // //------------------------------------ // // This solution is one in which a rather // large if/else statement is in the middle of // main() and all the code for each command // is in-line. A simple else clause handles // any invalid commands in the file. // // This solution is what we expect // from you at this point in your programming // experience. //----------------------------------------------- // // Note that at least the if/else statement is // arranged with the commands in alphabetical // order to make it easy to find the code associated // with each command. Note the use of whitespace // and in-line comments to separate the code for // each command to make it more readable. Also note // the use of local variables inside each if/else // clause. This makes the code more readable since // the variables are declared where they are used. // //------------------------------------------------ // Note the use of a simple function (ReadLine) // to skip all blank lines in the file without // needing while loops throughout the code. // The code for each command relies on the project // description's guarantee that all necessary data // for each command will be present. It's therefore // only necessary to check the return value from ReadLine() // when trying to read a new command // //------------------------------------------------------- // Also note that this code does none of the error // checking. All error checking and error message generation // is done in the library class. This is NOT the best way, // but error handling is done this way so you'll have something // to contrast with the correct method of error handling. // We'll discuss the correct method of error handling soon. //----------------------------------------------------- #include #include #include #include "library.H" using namespace std; // function prototypes for functions // in this file bool ReadLine (ifstream& in, string& s); int main (int argc, char *argv[] ) { // check for correct number of arguments if (argc != 2) { cerr << "Usage: Proj2 " << endl; exit (-1); } // instantiate and open command file ifstream infile (argv[1]); if (!infile) { cerr << "Unable to open file: " << argv[1] << endl; exit (-2); } Library library; string command; // read command from file until eof while (ReadLine (infile, command)) { // switch to function for the command // add a book to library // needs title and author if (command == "ADDBOOK") { string title, author; ReadLine (infile, title); ReadLine (infile, author); cout << "COMMAND:ADDBOOK," << title << ", " << author << endl; library.AddBook (title, author); } // add a patron to the libary // needs patron's name else if (command == "ADDPATRON") { string name; ReadLine (infile, name); cout << "COMMAND:ADDPATRON, " << name << endl; library.AddPatron (name); } // list all the books in the library // no other data necessary from the file else if (command == "BOOKS") { cout << "COMMAND:BOOKS" << endl; library.ListBooks( ); } // check out a book // read book title and patron's name else if (command == "CHECKOUT") { string title, patron; ReadLine (infile, title); ReadLine (infile, patron); cout << "COMMAND:CHECKOUT, " << title << ", " << patron << endl; library.CheckOut (title, patron); } // check in a book // needs book title from file else if (command == "CHECKIN") { string title; ReadLine (infile, title); cout << "COMMAND:CHECKIN, " << title << endl; library.CheckIn (title); } // list all patrons in the library // nothing else to read from the file else if (command == "PATRONS") { cout << "COMMAND:PATRONS" << endl; library.ListPatrons( ); } // list books checked out by a patron // read patron's name from file else if (command == "PATRONSBOOKS") { string name; ReadLine (infile, name); cout << "COMMAND:PATRONSBOOKS, " << name << endl; library.ListPatronsBooks (name); } else // unkown command; output error cerr << "COMMAND:" << command << " is invalid" << endl; } return 0; } //------------------------------------ // ReadLine // skips blank lines in the file // returns true if non-blank line read // returns false on eof //----------------------------------- bool ReadLine (ifstream& in, string& s) { s = ""; // empty string with length == 0 // loop until eof or non-blank string read while (!in.eof( ) && s.length() == 0) { getline (in, s); if (s.length() != 0) return true; } return false; }