pointer using string.....

Pls.,, help me to find the error in ths program...
this program should accept white space,so i use string and getline...it doesn't have error detected but it doesn't come up with the desire output.
thank you in advance...

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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include<iostream>
#include<string>

using namespace std;
struct node
{
	string name;
	node *nxt;
};

node *start_ptr = NULL;
node *current;
int option;

void add()
{
	node *temp, *temp2;
	temp = new node;
	cout<<"Enter your friend's name: ";
	getline(cin, temp->name);
	temp->nxt = NULL;
	
	if(start_ptr==NULL)
	{
		start_ptr = temp;
		current = start_ptr;
	}
	else
	{
		temp2 = start_ptr;
		while(temp2->nxt != NULL)
		{
			temp2=temp2->nxt;
		}
		temp2->nxt = temp;
	}
}

void display_list()
{
	node *temp;
	temp = start_ptr;
	cout<<endl;
	if(temp==NULL)
		cout<<"LIST OF FRIENDS\n\nlist is empty";
	else
	{
		cout<<"LIST OF FRIENDS\n\n";
		while(temp != NULL)
		{	
			cout<<temp->name;
			if(temp==current)
				cout<<" -->current node";
			cout<<endl;
			temp=temp->nxt;
		}
		cout<<"End of List!\n";
	}
}

void delete_start_node()
{
	node *temp;
	temp = start_ptr;
	start_ptr = start_ptr->nxt;
	delete temp;
}

void delete_end_node()
{
	node *temp1, *temp2;
	if(start_ptr==NULL)
		cout<<"The list is empty\n";
	else
	{
		temp1=start_ptr;
		if(temp1->nxt==NULL)
		{
			delete temp1;
			start_ptr=NULL;
		}
		else
		{
			while(temp1->nxt != NULL)
			{
				temp2=temp1;
				temp1=temp1->nxt;
			}
			delete temp1;
			temp2->nxt=NULL;
		}
	}
}

void move_current_on()
{
	if(current->nxt==NULL)
		cout<<"You are at the end of the list";
	else
		current=current->nxt;
}

void move_current_back()
{
	if(current==start_ptr)
		cout<<"You are at the start of the list";
	else
	{
		node *previous;
		previous=start_ptr;
		while(previous->nxt != current)
		{
			previous=previous->nxt;
		}
		current=previous;
	}
}

void main()
{
	start_ptr=NULL;
	do
	{
		display_list();
		cout<<"\nSelect your option\n"
			<<"[0] Exit\n"
			<<"[1] Add friends to the list\n"
			<<"[2] Delete friends from the start of the list\n"
			<<"[3] Delete friends from the end of the list\n"
			<<"[4] Move current pointer on one friend\n"
			<<"[5] Move current pointer back one friend\n\n>>";
		cin>>option;

		switch(option)
		{
		case 1: add(); break;
		case 2: delete_start_node(); break;
		case 3: delete_end_node(); break;
		case 4: move_current_on(); break;
		case 5: move_current_back(); break;
		}
	}
	while(option != 0);
}
Hello, before using getline call cin.innore(1) if you used cin>> before it, other wise, it does not take two seperate string in line.
1
2
3
4
5
6
7
	cout<<"Enter your friend's name: ";

	cin.ignore(1);
	getline(cin,temp->name,'\n');                           

	temp->nxt = 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149

#include<iostream>
#include<string>

using namespace std;
struct node
{
	string name;
	node *nxt;
};

node *start_ptr = NULL;
node *current;
int option;

void add()
{
	node *temp, *temp2;
	temp = new node;
	cout<<"Enter your friend's name: ";

	cin.ignore(1);
	getline(cin,temp->name,'\n');

	temp->nxt = NULL;

	if(start_ptr==NULL)
	{
		start_ptr = temp;
		current = start_ptr;
	}
	else
	{
		temp2 = start_ptr;
		while(temp2->nxt != NULL)
		{
			temp2=temp2->nxt;
		}
		temp2->nxt = temp;
	}
}

void display_list()
{
	node *temp;
	temp = start_ptr;
	cout<<endl;
	if(temp==NULL)
		cout<<"LIST OF FRIENDS\n\nlist is empty";
	else
	{
		cout<<"LIST OF FRIENDS\n\n";
		while(temp != NULL)
		{	
			cout<<temp->name;
			if(temp==current)
				cout<<" -->current node";
			cout<<endl;
			temp=temp->nxt;
		}
		cout<<"End of List!\n";
	}
}

void delete_start_node()
{
	node *temp;
	temp = start_ptr;
	start_ptr = start_ptr->nxt;
	delete temp;
}

void delete_end_node()
{
	node *temp1, *temp2;
	if(start_ptr==NULL)
		cout<<"The list is empty\n";
	else
	{
		temp1=start_ptr;
		if(temp1->nxt==NULL)
		{
			delete temp1;
			start_ptr=NULL;
		}
		else
		{
			while(temp1->nxt != NULL)
			{
				temp2=temp1;
				temp1=temp1->nxt;
			}
			delete temp1;
			temp2->nxt=NULL;
		}
	}
}

void move_current_on()
{
	if(current->nxt==NULL)
		cout<<"You are at the end of the list";
	else
		current=current->nxt;
}

void move_current_back()
{
	if(current==start_ptr)
		cout<<"You are at the start of the list";
	else
	{
		node *previous;
		previous=start_ptr;
		while(previous->nxt != current)
		{
			previous=previous->nxt;
		}
		current=previous;
	}
}

void main()
{
	start_ptr=NULL;
	do
	{
		display_list();
		cout<<"\nSelect your option\n"
			<<"[0] Exit\n"
			<<"[1] Add friends to the list\n"
			<<"[2] Delete friends from the start of the list\n"
			<<"[3] Delete friends from the end of the list\n"
			<<"[4] Move current pointer on one friend\n"
			<<"[5] Move current pointer back one friend\n\n>>";
		cin>>option;

		switch(option)
		{
		case 1: add(); break;
		case 2: delete_start_node(); break;
		case 3: delete_end_node(); break;
		case 4: move_current_on(); break;
		case 5: move_current_back(); break;                                            
		}
	}
	while(option != 0);
}

Topic archived. No new replies allowed.