linear linked list help

hi guys, so im trying to implement a linear linked list in my program, and in the process of doing so seemed to have run into a seg fault issue. i was hoping i could get some guidance on were i went wrong with my list

here is my .h file
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
#include <iostream>
#include <cctype>
#include <cstring>

using namespace std;

struct node;
class Posting;
class list;

class Posting            //class
{
public:
	Posting();       //constructor
	~Posting();          //destructor

	void Create_Posting( bool &check);  //create a new posting of this title.
	void display_post();
	bool is_type();  //is the posting a job, housing, item for sale, or free stuff

	char *type;
	friend class list;
private:
	node * head;
	float cost;
	char *description;
	char *title;
	float sqfeet;
	float rooms;
	float rent;
	char *location;
	char *email;
	int size;	
	float pay;
	float bathrooms;
	
};
struct node
{
	Posting data;
	node * next;

};
class list
{
public:
	list();
	~list();

	void Add(char title[]);
	void display_all();
	int count;
	void display_type(); //only display those postings that match a type
	friend class Posting;
private:	
	char *search_type;			
	int size;	
	node * head;
};


and my functions to create the 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
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
void Posting::Create_Posting(bool &check)   // function to create the posting
{
	char temp[1000];    //temp char array to hold user input before creating array 
	char display('y');	//char to run the display function if they so choose
	list list;
	node * current = head; // creating temp pointer for list

	cin.ignore(1000,'\n');
	cout << "create a title: " << endl;
	cin.getline(temp, 1000);                // prompt and input of post title
	if (title)
		delete [] title;
	head -> data.title = new char [strlen(temp) + 1];
	strcpy( head -> data.title, temp);
	cin.ignore(1000, '\n');

	cout << "add a description: " <<  endl;		
	cin.getline(temp, 1000);                // prompt and input of post title
	if(description)
		delete [] description;
	head -> data.description = new char [strlen(temp) + 1];
	strcpy( head -> data.description, temp);
	cin.ignore(1000, '\n');


	cout << "please provide your email: " << endl;   	//and email
	cin.getline(temp, 1000);
	if(email)
		delete [] email;
	head -> data.email = new char [strlen(temp) + 1];
	strcpy( head -> data.email, temp);
	cin.ignore(1000, '\n');



	if (strcmp (type, "job")== 0)   // if the type of post is "job" then run this if statement to get additional info
	{	
		cout << "What is the wage the position offers(dollars per hour): " << endl;
		cin >> pay;			

		cout << "Location: " << endl;
		cin.getline (temp, 1000);
		if(location)
			delete [] location;
		head -> data.location = new char [strlen(temp) + 1];
		strcpy( head -> data.location, temp);
		cin.ignore(1000, '\n');
		head -> next = NULL;
	}

	else if (strcmp (type, "housing") == 0)        // if its "housing" run this if
	{
		cout << "how many sq feet is the property? : " << endl;
		cin >> sqfeet;

		cout << "how many rooms? :" << endl;
		cin >> rooms;

		cout << "how many bathrooms?: " << endl;
		cin >> bathrooms;

		cout << "how much is the rent?: " << endl;
		cin >> rent;

		cout << "What is the location of the property?: " << endl;
		cin.getline (temp, 1000);
		if(location)
			delete [] location;
		head -> data.location = new char [strlen(temp) + 1];
		strcpy( head -> data.location, temp);
		cin.ignore(1000, '\n');
		head -> next = NULL;

	}

	else if (strcmp (type, "sale") == 0)  	//if its a "for sale" item run this one
	{

		cout << "what is the cost of the item: " << endl;
		cin >> cost;	
		head -> next = NULL;
	}	


	cout << "you have successfully created a post!"  << endl;
	list.count++;

	cout << "\n\nwould you like to view the post you created?(y/n): " << endl;
	cin >> display;

	if(display == 'y' || display == 'Y')
		display_post();

}

bool Posting::is_type() // function to get type of post
{
	node * current = head;
	char temp[1000];
	bool toret = false;

	cout << "What type of post are you creating? (job, housing, sale,or free): " << endl;
	cin.getline(temp, 1000);
	if(type)
		delete [] type;
	head -> data.type = new char [strlen(temp) + 1];
	strcpy( head -> data.type, temp);
	cin.ignore (1000, '\n');

	if (strcmp (type, "job") == 0 || strcmp (type, "housing") == 0 || strcmp (type, "sale") == 0 || strcmp (type, "free") == 0)   // if user input matches one of the predetermined post types the the bool is true
	{
		toret = true;
	}

	return toret;
}

and here is my function to display the 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
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
void list::display_type()
{
	Posting post;
	node * current = head;

	cout << "which type of post do you want to search for? (job, housing, sale, free): " << endl;
	cin.getline(post.type, 20);
	cin.ignore(20, '\n');

	if (head)
	{
		for (int i(0); i < count && current; i++)
		{

			if (current)
			{
				if( strcmp (current -> data.type, search_type) == 0)
				{
					cout << current -> data.type << endl;
					cout << current -> data.title << endl;
					cout << current -> data.description << endl;
					cout << current -> data.email << endl;
					if (strcmp (search_type, "job") == 0)
					{
						cout << "Wage: $" << current -> data.pay << "Per hour " << endl;
						cout << "Location at: " << current -> data.location << endl;
					}
					if(strcmp (search_type, "housing") == 0)
					{
						cout << "Square feet: " << current -> data.sqfeet << endl;
						cout << "Bedrooms: " << current -> data.rooms <<endl;
						cout << "Bathrooms: " << current -> data.bathrooms << endl;
						cout << "Rent: $" << current -> data.rent << endl;
						cout << "Address: " << current -> data.location << endl;
					}
					if (strcmp (current -> data.type, "sale") == 0)
					{
						cout << "$" << current -> data.cost << endl;
					}
				}
			}
			i++;
			current = current -> next;
		}
	}

}

void list::display_all()
{
	node * current = head;

	for (int i(0); i < count && current; i++)
	{

		cout << "Post Type: " << current -> data.type << endl;
		cout << "Title: " << current -> data.title << endl;
		cout << "Description: " << current -> data.description << endl;
		cout << "Email: " << current -> data.email << endl;


		if (strcmp (search_type, "job") == 0)
		{
			cout << "Wage: $" << current -> data.pay << "Per hour " << endl;
			cout << "Location at: " << current -> data.location << endl;
			cout << '\n';
		}
		if(strcmp (search_type, "housing") == 0)
		{
			cout << "Square feet: " << current -> data.sqfeet << endl
				<< "Bedrooms : " << current -> data.rooms << endl
				<< "Bathrooms: " << current -> data.bathrooms << endl
				<< "Rent: $" << current -> data.rent << endl
				<< "Address: " << current -> data.location << endl
				<< '\n';
		}

		if (strcmp (search_type, "sale") == 0)
		{
			cout << "$" << current -> data.cost << endl;
			cout << '\n';
		}
		current = current -> next;
	}
}
Topic archived. No new replies allowed.