Weird symbols being printed when printing string value

There are some weird symbols being printed while printing the desc string. I dont know what the problem is. Please review these code and let me know what i am doing wrong.
The code I am using to insert data into a Linked List.
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
 void insert(string &name,string &desc,string &cat,string &date){
  int count=0;
  nn = new node;
  if (head==NULL){
  count++;
  nn->count=count;
  nn->hb_name= name;
  nn->hb_desc= desc;
  nn->category= cat;
  nn->date= date;
  nn->next=NULL;
  head=nn;
  }
  else{
    temp = head; // The temp points to the first node
    while ( temp->next != NULL)
      temp = temp->next;
  temp1=temp; //temp1 is the last node
  temp1->next = new node;  // Creates a node at the end of the list
  temp =temp1->next; // Points to that node
   count++;
  nn->count=count;
  temp->hb_name= name;
  temp->hb_desc= desc;
  temp->category= cat;
  temp->date= date;
  temp->next = NULL;
  free(temp1);
  }
}

I am using the following code to print the above stuff
1
2
3
4
5
6
7
8
9
10
11
12
void print()
{
    temp=head;
    while(temp!=NULL)
    {
        cout<<temp->count<<" \n\n"<<temp->hb_name<<endl;
        cout<<"Category: \n\n "<<temp->category<<endl;
        cout<<"Due Date:\n\n "<<temp->date;
        cout<<"Description: \n\n"<<temp->hb_desc<<endl;
        temp=temp->next;
    }
    }

Last edited on
1. What is on line 28? Do not mix totally unrelated systems (new&delete vs malloc&free).

2. Why don't you have custom constructor for the class node?
Repeating all that "assign to members" distracts from the real issues.

3. Are the head and temp global variables? Why the temp isn't a local variable?

4. Two allocations and one deallocation. Why?

5. Your code indentation could be clearer.
Topic archived. No new replies allowed.