Problems with LLL

So my display_all function is only displaying the most recent entry. I fear that this means that I have not properly set up my linear linked list. Any help would be apprecitated.
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#include <string.h>
using namespace std;
//Program to create a journal entry, display that entry or to search for a
//journal entry. Program takes Journal entries from the user and either stores
//them. Then allows the user to search for an entry by subject or to display
//a journal entry.

struct node;
class list;
 


class list   //Class for the elements of a journal entry and the functions for it
{
      public:
             list();
             ~list();
             void insert();
             void display_all();
      private:
              node * head;
              char * date;  //variable for the date
              char * subject; //variable for the subject
              char * writing; //variable for the body of the entry
      
};

struct node  //structure to create a node containing the appropriate information
{
       list data;  //variable to hold the members of the entry struct
       node * next;//creates a pointer to the next node
       
};

list::list()
{
      head=NULL;
}

list::~list()
{
      head=NULL;
}
        
void list::display_all()
{
      
      node * current;
      current=head;
      while(current!=NULL)
      {
              cout<<head->data.date<<'\t'<<head->data.subject<<endl;
              cout<<head->data.writing<<endl;
              current=current->next;
      }
} 

       

void list::insert()  //Function to add a journal entry
{
      char temp[501]; //temporary variable
      node * current=head; //temporary pointer 
      cout<<"Please enter the date of the entry followed by the enter key"<<endl;
      cin.get(temp,501,'\n');
      head->data.date=new char[strlen(temp)+1];
      strcpy(head->data.date,temp);
      cin.ignore(100,'\n'); 
      
      cout<<"Please enter the subject of the entry followed by the enter key"<<endl;
      cin.get(temp,501,'\n');
      head->data.subject=new char[strlen(temp)+1];
      strcpy(head->data.subject,temp);
      cin.ignore(100,'\n'); 
      
      cout<<"Please type your entry followed by the enter ':' symbol"<<endl;
      cin.get(temp,501,'\n');
      head->data.writing=new char[strlen(temp)+1];
      strcpy(head->data.writing,temp);
      cin.ignore(1000,'\n'); 
      head->next=current;   //attaches the list                                                                               
}


                                   
     
void welcome()
{
     cout<<"Welcome to Robin's Electronic Journal"<<endl;
     cout<<"You can use this program to create a journal entry"<<endl;
     cout<<"to find a journal entry or to display all entries"<<endl<<endl;
}

     
int main()
{
     welcome();
     char response;
     list love; 
     
     do
     {
           cout<<"Enter a to add, d to display, or q to quit followed by the return key"<<endl;
           cin>>response;   
           cin.ignore(100,'\n');
           if(response=='a')
           {
                love.insert();
                cin.get();
           }
           else if(response=='d')
           {
                love.display_all();
                cin.get();
           } 
           else
                cout<<"Invalid entry please try again"<<endl;
     }while(response!='q'||response!='Q') ;  
                
     return 0;
}


Topic archived. No new replies allowed.