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...
#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);
}