#include #include #include #include "cdlist2.h" #include "CPstring.h" // program for manipulating database of CD's // most elements of this program can be used // to manipulate any kind of list/database, not just CD list/db // Owen Astrachan // 3/13/95 // array of choices/operations // these should match up so that Choices[k] is character for Operations[k] // numOps is the number of entries in Choices (and in Operations) char Choices [] = { 'a','t','g','p','r','q' }; string Operations [] = { "add", "title search", "group search", "print","read","quit" }; const int numOps = sizeof(Choices)/sizeof(char); const int QUIT_CHAR = 'q'; // function prototypes for database manipulating functions // and for menu system void Menu(); void DoAdd(CDlist & list); void DoQuit(CDlist & list); void DoTitle(CDlist & list); void DoGroup(CDlist & list); void DoRead(CDlist & list); void DoPrint(CDlist & list); void Error(char ch); main() { CDlist list(4000); // the list of CD's string line; char choice; do{ Menu(); cout << endl << "enter choice: "; getline(cin,line); // don't mix >> and getline choice = tolower(line[0]); switch (choice) // what action to take? { case 'a': DoAdd(list); break; case 'p': DoPrint(list); break; case 'r': DoRead(list); break; case 't': DoTitle(list); break; case 'g': DoGroup(list); break; case 'q': DoQuit(list); break; default: Error(choice); } } while (choice != QUIT_CHAR); } void Menu() // postcondition: all choices and corresponding operations printed { int k; for(k=0; k < numOps; k++) { cout << "[" << Choices[k] << "]\t"; cout << Operations[k] << endl; } } void DoRead(CDlist & list) // postcondition: list is filled with entries from user-entered file // exception: if file doesn't exist, program aborts { string name; string price,group,title,number; // info for each CD ifstream input; cout << "name of input file: "; getline(cin,name); input.open(name); if (!input.good()) { cerr << "couldn't open " << name << endl; exit(0); } list.DeleteAll(); // remove old entries while (getline(input,number,':') && getline(input,price,':') && getline(input,group,':') && getline(input,title)) { list.Add(CDinfo(atoi(number),title,atof(price),group)); } cout << "total of " << list.Size() << " CD's" << endl; } void DoAdd(CDlist & list) // postcondition: new CD added to list (from user entered data) { cerr << "DoAdd not implemented" << endl; } void DoPrint(CDlist & list) // postcondition: all entries in list printed { list.Print(); } void DoTitle(CDlist & list) // postcondition: user prompted for title (or substring) and matches printed { CDlist match(1000); // holds all CD's that match user-entered title string name; cout << "title to look for: "; getline(cin,name); // uses advanced C++ feature, pointer to data member list.Search(&CDinfo::title,name,match); match.Print(); } void DoGroup(CDlist & list) // postcondition: user prompted for group (or substring) and matches printed { CDlist match(1000); // holds all CD's that match user-entered title string name; cout << "group to look for: "; getline(cin,name); list.Search(&CDinfo::group,name,match); match.Print(); } void DoQuit(CDlist & list) { cout << "thanks for using our CD database" << endl; } void Error(char ch) // postcondition: error message printed { cerr << "Unrecognized option: " << ch << endl; }