making general list error

hey I compiled this program.but i got an error "segmentation dumped".Can anyone plz explain me the error in this


#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int ns;
int l;
string k[1000];
int count=0;


class genlistnode
{
// friend class genlist;
public:
int flag;
string atom;
genlistnode *next,*down;

};

genlistnode* makelist();

int main()
{
string s;
cout<<"enter the string:";
getline (cin,s);
cout<<s<<endl;
int l=s.length();
string temp;
for (int i=0;i<l;i++)
{
if(s[i]==' ')
{
k[count]=temp;
temp="";
count=count+1;
}
else
{

temp=temp+s[i];
}
}
k[count]=temp;
count++;
cout<<"\n"<<count<<"\n";
for (int i=0;i<count;i++)
{
cout<<k[i]<<"\n";
}
genlistnode* result;
result=makelist();
}

int q=0;
genlistnode* makelist()
{
genlistnode *head;
genlistnode *p;
cout<<"\n"<<head;
head=NULL;

while(q!=count)
{

if(k[q]=="(")
{
if(head!=NULL){
cout<<k[q]<<count<<"done \n";
genlistnode *l;
l->next=NULL;
p->next=l;
p=l;
l->flag=0;
l->down=makelist();
}
else
{
cout<<"Hi";
genlistnode *l;
p=l;
head=l;
head->next=NULL;
head->flag=0;
head->down=makelist();

}
}


if(k[q]==")")
return head;


if((k[q]!=")")&& (k[q]!="("))
{
if(head!=NULL)
{
genlistnode *l=new genlistnode;
l->flag=1;
l->atom=k[q];
l->down=NULL;
l->next=NULL;
p->next=l;
p=l;
}
else
{
genlistnode *l=new genlistnode;
l->flag=1;
l->atom=k[q];
l->down=NULL;
l->next=NULL;
head=l;
p=l;
}
}

q++;
}
}
It'll be easier to examine your code if you put it inside code tags and indent it properly.
@Divi: abhishekm is correct, please use code formatting to upload code. I have uploaded the code which I tried on my editor. I was not able to understand what the purpose of your program is

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
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int ns;
int l;
string k[1000];
int count=0;


class genlistnode
{
// friend class genlist;
public:
int flag;
string atom;
genlistnode *next,*down;
};

genlistnode* makelist();

int main()
{
    string s;
    cout<<"enter the string:";
    getline (cin,s);
    cout<<s<<endl;
    int l=s.length();
    string temp;
    for (int i=0;i<l;i++)
    {
        if(s[i]==' '){
           k[count]=temp;
           temp="";
           count=count+1;	
        }
        else{
           temp=temp+s[i];	
        }	
    }	
    k[count]=temp;
    count++;
    cout<<"\n"<<count<<"\n";
    for (int i=0;i<count;i++){
        cout<<k[i]<<"\n"; 
    }
    genlistnode* result;
    result=makelist();	
}

int q=0;
genlistnode* makelist()
{
    genlistnode *head;
    genlistnode *p;
    cout<<"\n"<<head;
    head=NULL;

    while(q!=count){     
        if(k[q]=="("){ 
            if(head!=NULL){
                cout<<k[q]<<count<<"done \n";
                genlistnode *l;
                l->next=NULL;
                p->next=l;
                p=l;
                l->flag=0;
                l->down=makelist(); 
            }
            else{
                cout<<"Hi";
                genlistnode *l;
                p=l;
                head=l; 
                head->next=NULL;
                head->flag=0;
                head->down=makelist(); 
            } 
        }


        if(k[q]==")"){
            return head;
        }
        
        if((k[q]!=")")&& (k[q]!="(")){
            if(head!=NULL){
                genlistnode *l=new genlistnode;
                l->flag=1;
                l->atom=k[q];
                l->down=NULL;
                l->next=NULL;
                p->next=l;
                p=l;
            }
            else{ 
                genlistnode *l=new genlistnode;
                l->flag=1;
                l->atom=k[q];
                l->down=NULL;
                l->next=NULL;
                head=l;
                p=l;
            }
        }
        q++;
    }
}


This is the output I get when I run your program.

enter the string:aabb
aabb

1
aabb

0033ECB1Press any key to continue . . .


Clearly state the purpose of the program and the output errors you are seeing.
@funprogrammer
This is the output I got
1
2
3
4
5
6
7
8
9
10
enter the string:( djhfn vjn )
( djhfn vjn )

4
(
djhfn
vjn
)

Segmentation fault (core dumped)



The purpose of the program is to make a parser
Some irregularities that I see are as follows:

58. cout<<"\n"<<head; // head is an uninitialized pointer. Applying the ofstream operator on a pointer to a class does not make sense to me.

1
2
3
4
         genlistnode *l;
                l->next=NULL;
                p->next=l;
                p=l;

again l is not pointing to anything so trying to access l->next will give a runtime error.

similarly for
1
2
3
4
5
6
7
8
9
else{
                cout<<"Hi";
                genlistnode *l; // not pointing to anything
                p=l;
                head=l; 
                head->next=NULL;
                head->flag=0;
                head->down=makelist(); 
            }
Topic archived. No new replies allowed.