Thread: Why doesn't working?

  1. #1
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154

    Why doesn't working?

    This is the class Forum
    Code:
    class Forum
    {
      char *name;
      int ID , subforumsnum , threadsnum;
      Forum **subforums  ;  //Array of indexes of subforums
      Forum *back ;
      Thread **threads;
    
    public:
         //--------------constructor-destructor etc.
    };
    This is the class Thread
    Code:
    class Thread
    {
     char *name , *creator_name;
     int ID,postsnum;
     bool locked,sticky;
     Forum *back;
     Post **posts;   //Array of indexes of posts included in thread class
    
    public:
         void answer_to_post ( int newID, char *name, int pID );
         //------------constructor-destructor etc.
    };
    And here is the function answer_to_post
    Code:
    void Thread :: answer_to_post ( int newID, char *cname, int pID )
    {
      char *newcontent;
      Post **temp;            //temporary array for posts
      
      cout<<"Type your answer to the post"<<endl;
      cin>>newcontent;
    
      temp = new Post *[postsnum+1];   //Expand the array by one more cell from the posts array
      for( int i=0 ; i<postsnum ; i++ )
           temp[i] = posts[i];                      //Copy the posts to temp
    
      delete[] posts;     //Delete the old posts array
    
      //Store the new post to the new position of temp array
      temp[postsnum] = new Post ( newID, cname, pID, newcontent );
    
      postsnum++;    //Increase the number of posts by one
      posts = new Post *[postsnum];    //Create new array same size as temp
    
      for( int i=0 ; i<postsnum ; i++ )
           posts[i] = temp[i];                   //Copy temp array to posts array
    
      delete[] temp;    //Delete temp array
    }
    The basic idea is to make a temporary array (temp) in order store indexes of posts classes, expand the posts array by one cell and store the new post into the last cell.
    In reality it is a "dynamic" array which stores post classes.

    I do not know why it does not working.
    After using it all cells had the same class in every cell.

    Thanks
    Last edited by ch4; 01-24-2008 at 09:21 AM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by ch4
    This is the class Forum
    Code:
    class Thread
    {
      char *name;
      int ID , subforumsnum , threadsnum;
      Forum **subforums  ;  //Array of indexes of subforums
      Forum *back ;
      Thread **threads;
    
    public:
         //--------------constructor-destructor etc.
    };
    You sure about that? Doesn't look like the Forum class to me.


    Code:
    char *newcontent;
    ...
    cout<<"Type your answer to the post"<<endl;
    cin>>newcontent;
    What do you expect that to do? You haven't allocated any space for newcontent so it's pointing to some random location where you're dumping the user's answer.

    Are you not allowed to use a vector to store your Posts? That could greatly simplify things.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154
    Thanks hk.
    There is no problem in allocating the newcontent, i've change the way of allocation.
    The function runs without problems (no warning,no errors...) but i can understand why every cell of the new potst array is filled with only one class.
    I can't see where the problem is?

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    There is no problem in allocating the newcontent, i've change the way of allocation.
    The function runs without problems (no warning,no errors...)
    Your code neither allocates nor releases the newcontent string. If you have it, don't just omit it.

    but i can understand why every cell of the new potst array is filled with only one class.
    How many do you want there to be, then?

    The only thing I can see is that you shouldn't allocate the array of Post pointers twice. Simple
    posts = temp; and not deleting temp should be enough.

    Another problem is, that unless you are not allowed to use C++ standard libraries, there's too much complexity (from manually managing memory). From design point of view all these classes are doing too much: providing high level functionality and doing the low level job of memory management. If these were separated (by using std::vector, std::string or similar classes - you can write your own if you need to), it would be much easier to follow the high-level logic of the high-level classes.

    Code:
    void Thread :: answer_to_post ( int newID, char *cname, int pID )
    {
      string newcontent;
      cout<<"Type your answer to the post"<<endl;
      getline(cin, newcontent); //or were you asking about this
      posts.push_back(Post(newID, cname, pID, newcontent ));
    }
    This is what the same code might look like. Note that there is no visible low-level code, and each line of code performs a clearly readable high-level operation.
    Last edited by anon; 01-24-2008 at 10:10 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154
    I am new in C++ so i 've asked your help.

    In reality this is a homework and i'm free to use every library and every function i like/need. I've asked to make a system which consists of forums - threads - posts - users etc. My first idea was a forum with subforums into double linked lists but i was afraid of the hard way of accessing them, so i prefer arrays with pointer to the classes.

    Thanks Anon
    Last edited by ch4; 01-24-2008 at 11:40 AM.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If you have such an assignment you can't be a complete beginner. As you can see there is a lot to gain from separating high-level and low-level code, especially if the language provides objects that do the low-level tasks for you.

    The above function wouldn't need a single change if posts was a std::list (doubly linked list) instead of std::vector. (But other code that took advantage of the random access of vectors might.)

    And if you really want to get more experience with low-level code (managing dynamic arrays), it would be possible to write your own array class and as long as it had the push_back function, the answer_to_post method would still be exactly the same.
    Last edited by anon; 01-24-2008 at 12:11 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 08:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM