Web Design and Web Development Forum

  1. #1
    Join Date
    Nov 2009
    Posts
    16
    Rep Power
    0
  2. gregr is on a distinguished road
  3. help with c++ project

    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();
    }
    Reply With Quote Reply With Quote
  4. #2
    Join Date
    Nov 2009
    Posts
    16
    Rep Power
    0
  5. gregr is on a distinguished road
  6. Re: help with c++ project

    On items destructor I simply put the line:
    delete m_picture;
    I think that is right and will move on to the second step.
    Reply With Quote Reply With Quote
  7. #3
    Join Date
    Nov 2009
    Posts
    16
    Rep Power
    0
  8. gregr is on a distinguished road
  9. Re: help with c++ project

    For the second todo I wrote these lines:
    delete pic;
    pic = m_picture;
    Am I doing what the directions say to do? I deleted whatever pic pointed to (which I perceived to be an or any existing picture) and set pic to a new picture (named m_picture).

    For the third function, I realized that I need to do checks if the files were read in correctly and am going to attempt to implement this.
    Reply With Quote Reply With Quote
  10. #4
    Stephen Bryant's Avatar
    Join Date
    Aug 2006
    Location
    North Carolina
    Age
    23
    Posts
    1,169
    Rep Power
    7
  11. Stephen Bryant is on a distinguished road
  12. Re: help with c++ project

    Hi gregr,

    It looks like you're doing well so far; one thing I would recommend though is to check and ensure the m_picture pointer is set before you delete it. For example:
    PHP Code:
    if (m_picture)
        
    delete m_picture
    If you have any specific questions about how to do something, I'd be glad to help.
    Reply With Quote Reply With Quote
  13. #5
    Join Date
    Nov 2009
    Posts
    16
    Rep Power
    0
  14. gregr is on a distinguished road
  15. Re: help with c++ project

    Thank you for your help!
    I have actually finished solving the problem. My solution to the load console function is not pretty, but it gets the job done. I can post it if anyone is interested.
    Reply With Quote Reply With Quote
  16. #6
    Join Date
    Dec 2009
    Posts
    1
    Rep Power
    0
  17. psst_hi is on a distinguished road
  18. Re: help with c++ project

    I am interested. Could you post up your code?
    Reply With Quote Reply With Quote
  19. #7
    Join Date
    Nov 2009
    Posts
    16
    Rep Power
    0
  20. gregr is on a distinguished road
  21. Re: help with c++ project

    Again, it is not pretty, but it produces the specified output and recognizes the errors. Hopefully they will not break it too badly.. and good thing I am not graded on style.
    Code:
    bool loadCatalog(ifstream& catalogFile, vector<Item>& catalog)
    {
        catalog.clear();    
       
        string s;
        while (getline(catalogFile, s))//needs to parameters
        {
              trimLeadingBlanks(s);
              int i = 1 ;
              //while (s[i-1] != ' ')
              //{
              //if (!isdigit(s[i]))
              //return false;
              //i++;        
              //}
              if (!isdigit(s[2]))
              return false;
              //gotta be a better implementation for this if it is a space i++until it is nto
              istringstream iss(s);
              int itemNum;
              string desc;
              char itemOrPic;
              iss >> itemOrPic >> itemNum;
              
    ////////////////////////////////////////////////////////////////////////////////
              int k = 0;
              int j = 0;
              for(k; k != s.size(); k++)
              {
                     if (s[k] == ' ')
                     j++;
                     
                     if (j==2)
                     break;
              }
              desc = s.substr((k+1), s.size()-(k+1));
    ////////////////////////////////////////////////////////////////////////////////          
              if (desc.size() == 0)
            {  return false; }
              
              Item itemel(itemNum, desc);          
              switch (itemOrPic)
              {
                     default:
                     {
                     cout << "neither an a or a p \n";
                     return false;
                     }
                     
               
                     case 'a':
                     for (int i = 0; i != catalog.size(); i++)
                     {
                         if (itemNum == catalog[i].number())
                         return false;
                         }
                     catalog.push_back(Item(itemel));
                     break;
               
               
               
                     case 'p':
                     {
                     Picture *picpoint = new Picture(desc);
                     int tester = 0;
                     for(int i = 0; i != catalog.size(); i++)
                     {
                             if (itemNum == catalog[i].number())
                                    tester++; 
                     }
                     if (tester == 0 )
                     return false;
                   
                     for(int i =0; i != catalog.size(); i++)
                     {
                             
                             if (itemNum == catalog[i].number())
                             catalog[i].setPicture(picpoint);
                     }
                     }
                     break;      
              }
        }
        // TODO:  Implement this function
    
        return true;  // This line is here just so this function compiles in the
                      // skeleton.
    }
    Reply With Quote Reply With Quote

Similar Threads

  1. Shall we try another project?
    By Umang in forum Chit Chat and Hangout
    Replies: 1
    Last Post: 07-26-2010, 05:06 AM
  2. Developers - Share your Fav Project
    By cancer10 in forum Chit Chat and Hangout
    Replies: 0
    Last Post: 07-14-2008, 10:15 PM