remove function not working

Below is my .h file and the code below that is my function that I'm having troubles with. Its suppose to take in a users topic and see if that topic exists, if it does exist then find the keyword, commentcompare will find where that keyword is and delete the comment. However its not deleting anything and its returning temp is NULL.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class comment //adds a comment
{
    public:
    comment(char * create_comment);
    ~comment();
    int postcomment(char * add_comment);
    int DisplayAll(); //display all comments
    int Rate(int rate);
    bool commentcompare(char * comm);
    comment *next;
    
    
    private:

    char * a_comment;
    int rating;
    
    
};


struct topicnode //checks to see if the topic is correct one
{
    topicnode();
    ~topicnode();
    int addcomment(char *add_comment);//add comment to the end of LLL
    char *topicname;
    comment * head;
    
    topicnode *next;
    
};




class blog //class for the elements of blog entries and functions for it
{
    public:
    blog();
    ~blog();
    int Post(char *topic, char *add_comment); //finds the post and adds comment
    int Display(char *topic); //displays a particular thread
    int Display(); //displays all
    int rate(char *topic, char *keyword, int rating); //find topic and rate comment
    int remove(char *topic, char *keyword); //remove a topic
    int createtopic(char *topic); //create topic
    
    private:
    topicnode *head;

    
    
};





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
int blog::remove(char *topic, char*keyword)
{
    topicnode * current;
    comment *temp, *prev;
    
    if(!topic)
        return 0;
    for(current = head; current != NULL; current = current -> next)
    {
        if(strcmp(topic, current->topicname) == 0)
        {
            for(temp = current->head; temp != NULL; temp = temp -> next)
            {
                if(temp-> commentcompare(keyword) == true)
                    prev = temp;
                temp = temp -> next;

                
            }
            cout<<temp<<" has been successfully deleted."<<endl;

            delete temp;
            temp = NULL;
        }
    }
    
    
    
    return 1;
}
topicnode * current;
This means use the operator *. A common use of '*' is for multiplication. This means that current is never initialized and there for equates to NULL.

I'm also seeing this in other places, it is wise to usually put the '*' next to the variable. Have you also checked that topicnode::head is ever initialized? If it equals NULL then your code won't work and you'll receive a SEGFAULT message (Segmentation Fault). You need to make sure that topicnode::head is referencing valid memory before using it.
This means use the operator *. A common use of '*' is for multiplication. This means that current is never initialized and there for equates to NULL.


@snow:
??? In this context, the '*' operator is the operator used for declaring a pointer to an object. Also, current is initialized in the for loop on line 8. The position of the operator makes no difference, it is simply one of three operators based on its context (multiplication, dereference, or pointer declarator).

@OP:
Your for loop is incrementing 'temp' twice, meaning that you skip every second object. You should remove line 16, and probably break out of the loop when the topic has been found. You should also modify the prev object to make it point to the object after, otherwise it will be pointing to deallocated memory.
it is simply one of three operators based on its context (multiplication, dereference, or pointer declarator)


You're right, sorry about that. I don't know why I thought that it meant multiplication. My bad. That fact also would negate me saying that current is never initialized.
Topic archived. No new replies allowed.