// File: main.cpp // Purpose: start of solution to CS II, HW 4, Spring 2006 #include #include #include #include #include "wishlist.h" using namespace std; // Returns an iterator pointing to the entry in the list of wishlists // with this name. If the entry is not in the list, the iterator // will be at the end of the list. list::iterator wish_find(list& wish_lists, const string& name) { list::iterator p; for (p=wish_lists.begin(); p!=wish_lists.end(); ++p) { if (p->name() == name) break; } return p; } int main(int argc, char* argv[]) { // Make sure there are three arguments. if (argc != 3) { cerr << "Usage:\n " << argv[0] << " infile outfile\n"; return 1; } // Open and test the input and output files. ifstream istr(argv[1]); if (!istr) { cerr << "Could not open " << argv[1] << " to read\n"; return 1; } ofstream ostr(argv[2]); if (!ostr) { cerr << "Could not open " << argv[2] << " to write\n"; return 1; } list wish_lists; // A container to store all the WishLists char request; // The input char indicating the request string name, item, buyer; // The input name, item and buyer name float price; // The item price list::iterator w_itr; // The iterator to access a particular WishList // The loop will end when there are no more input requests, skipping // over white-space chars. while (istr >> request) { if (request == 'a') { // ================================= // Add an item to the wish list // ================================= // read rest of input line istr >> name >> item >> price; // find the right wishlist w_itr = wish_find(wish_lists, name); if (w_itr == wish_lists.end()) { // need to add a new wishlist wish_lists.push_back(WishList(name)); ostr << "New wish list added for " << name << endl; w_itr = wish_lists.end(); w_itr--; } // add the item w_itr->add_to_list(item, price); ostr << "Item " << item << " added to wish list for " << name << endl; } else if (request == 'p') { // ================================= // Purchase an item from a WishList // ================================= } else if (request == 'w') { // ================================= // Output the WishList // ================================= } else if (request == 'b') { // ================================= // Output the purchased items // ================================= } } return 0; }