Hello I have a project due and do not understand some of the things I have to do. I mean to say is I know in my head what the missing TODO parts must do, but I do not know how to implement it. Can anyone help? I posted the code below. Some things I know I must do:
on the first TODO: use the delete operator somehow
on the second : delete a pointer and then set that pointer to point to something else
for the third function I am not sure where to start.
//addition
//forgot to add possibly critical details
You're developing software for a company that sells merchandise for students in CS courses. A catalog is a collection of the items that you have for sale. An item has an item number, a description, and possibly a picture of the item. In this skeleton program that you are to complete, a catalog is represented as a vector of Item objects. Refer to this code as you read this spec.
The main routine of the program opens a file named fallCatalog.txt (which details what should be in the catalog), loads the information from that file into a vector of Item objects, and displays all the items in that vector. It then does the same thing for the file winterCatalog.txt, reusing the same vector.
Each line of a file like fallCatalog.txt is an instruction in one of two forms:
A line consisting of an a followed by an integer followed by some text is an instruction to add an item to the catalog. The integer is the item number, and the text is the description of the item. The item added will not have any picture associated with it initially.
A line consisting of a p followed by an integer followed by some text is an instruction to set the picture associated with an item in the catalog. The integer is the item number, and the text is the location of the picture. If there already is a picture associated with the item, the previous picture is discarded.
Any number of spaces may precede the a or p, and one or more spaces must surround the item number. As an example, if the file fallCatalog.txt contains
a 1237 Stroustrup book
a 456 portrait of David Smallberg
a 2895 portrait of John Rohr
p 456 Smallberg.jpg
p 2895 Rohr.jpg
a 32108 used robot
p 32108 Abner.gif
p 456 Smallberg2.jpg
and winterCatalog.txt contains
a 135 portrait of David Smallberg
p 135 Smallberg.jpg
a 468 portrait of Carey Nachenberg
p 468 Nachenberg.jpg
a 246 Carrano book
p 135 Smallberg2.jpg
p 135 Smallberg3.jpg
p 468 http://www.cs.ucla.edu/csd/people/al...nachenberg.jpg
then the output of the program should be
Fall catalog:
Item #1237: Stroustrup book (no picture)
Item #456: portrait of David Smallberg (picture at Smallberg2.jpg)
Item #2895: portrait of John Rohr (picture at Rohr.jpg)
Item #32108: used robot (picture at Abner.gif)
Winter catalog:
Item #135: portrait of David Smallberg (picture at Smallberg3.jpg)
Item #468: portrait of Carey Nachenberg (picture at http://www.cs.ucla.edu/csd/people/al...nachenberg.jpg)
Item #246: Carrano book (no picture)
The lines of the file are processed from first to last. Notice how for the fall catalog, for example, the second line of fallCatalog.txt caused item number 456 to be added (with no picture), the fourth line associated the picture at Smallberg.jpg with the item, and the last line replaced that picture with Smallberg2.jpg. When the fall catalog was displayed, that last picture was the one listed for item 456.
The bulk of this project is implementing the loadCatalog function. This function takes two parameters: a reference to an ifstream object already opened to the file that contains the instructions for building the catalog, and a reference to the vector of Item objects that is the catalog that the the function will build. The function returns true if the catalog is successfully built, or false if there are any errors in the file. Errors include:
The line not being an a or p line.
There not being an item number.
The description or location text being empty or blank.
An item being added has a number that is already in the catalog.
A picture to be set refers to an item number that is not in the catalog.
//end of addition
Code:// catalog.cpp // Portions you are to complete are marked with a TODO: comment. #include <iostream> #include <fstream> #include <sstream> #include <string> #include <vector> using namespace std; /////////////////////////////////////////////////////////////////////////// // Type definitions /////////////////////////////////////////////////////////////////////////// const int PIXEL_ROWS = 240; const int PIXEL_COLS = 400; class Picture { public: Picture(string location); string location() const; // A full interface would probably have a member function for // drawing the picture. For this project, we won't bother. private: string m_location; char m_pixels[PIXEL_ROWS][PIXEL_COLS]; }; class Item { public: Item(int num, string desc); ~Item(); void setPicture(Picture* pic); int number() const; void display() const; // The following is here to address an issue that we won't discuss // until CS 32. You don't have to understand it, but without it, the // behavior of the program would be undefined. Item(const Item& other) : m_number(other.m_number), m_description(other.m_description), m_picture(other.m_picture == NULL ? NULL : new Picture(*other.m_picture)) {} private: int m_number; string m_description; Picture* m_picture; }; /////////////////////////////////////////////////////////////////////////// // Picture implementation /////////////////////////////////////////////////////////////////////////// Picture::Picture(string location) { m_location = location; // A full implementation would probably access the location and // fill the m_pixels array. For this project, we won't bother. } string Picture::location() const { return m_location; } /////////////////////////////////////////////////////////////////////////// // Item implementation /////////////////////////////////////////////////////////////////////////// Item::Item(int num, string desc) { m_number = num; m_description = desc; m_picture = NULL; } Item::~Item() { // TODO: Release any dynamically allocated picture } void Item::setPicture(Picture* pic) { // TODO: Establish pic as the picture associated with this item. If // there was already a picture associated with this item, get rid of it. } int Item::number() const { return m_number; } void Item::display() const { cout << "Item #" << m_number << ": " << m_description; if (m_picture == NULL) cout << " (no picture)"; else cout << " (picture at " << m_picture->location() << ")"; cout << endl; } /////////////////////////////////////////////////////////////////////////// // loadCatalog /////////////////////////////////////////////////////////////////////////// void trimLeadingBlanks(string& s) { int k; for (k = 0; k != s.size() && s[k] == ' '; k++) ; s.erase(0, k); } // loadCatalog: Each line of catalogFile either requests the addition of // a new item to catalog, or the association of a new picture with an // existing item in the catalog. Return true if all lines of the // catalogFile can be processed successfully; return false if there's // any error. If this function returns false, it can leave catalog in // any state it chooses. bool loadCatalog(ifstream& catalogFile, vector<Item>& catalog) { catalog.clear(); // TODO: Implement this function return true; // This line is here just so this function compiles in the // skeleton. } int main() { vector<Item> currentCatalog; ifstream catf("fallCatalog.txt"); if (!catf) { cout << "Error! Cannot open fallCatalog.txt" << endl; return 1; } if (!loadCatalog(catf, currentCatalog)) { cout << "Error loading fall catalog" << endl; return 1; } cout << "Fall catalog:" << endl; for (int k = 0; k != currentCatalog.size(); k++) currentCatalog[k].display(); catf.close(); catf.clear(); // Clear the end-of-file failure condition catf.open("winterCatalog.txt"); if (!catf) { cout << "Error! Cannot open winterCatalog.txt" << endl; return 1; } if (!loadCatalog(catf, currentCatalog)) { cout << "Error loading winter catalog" << endl; return 1; } cout << endl; cout << "Winter catalog:" << endl; for (int k = 0; k != currentCatalog.size(); k++) currentCatalog[k].display(); }


LinkBack URL
About LinkBacks






Reply With Quote