Pointers/Link List

I am having trouble with the insert function of this project, we are to use pointers to insert a dash and middle initial into the name where there is a dash. I got stuck at this point of the assignment and i need help here the code.

#include <iostream>
#include <string>
using namespace std;
class name_list
{
public:
name_list::name_list();
void name_list::readName();
void name_list::printname();
void name_list::insert();

private:
struct list_node
{
char letter;
list_node *next;
};
list_node *insert1, *insert2, *first, *last, *current, *prev;
};
int main()
{
name_list data;
data.readName();
data.printname();
data.insert();
data.printname();
system("pause");
return 0;
}

void name_list::readName()
{
char letter = 0;
cout<<"Enter the letter, * stops input ";
while (letter !='*')
{
current= new list_node;
cin>>current->letter;


current->letter=letter;
current->next = 0;
prev->next=current;
prev=current;
cout<<" Enter the letter, * stops input ";
cin>>letter;
}
}


void name_list::printname()

{
//print the list
current=first->next;
while(current!=0)
{
cout<<current->letter<<" "<<endl;
current=current->next;
}
}
//PROBLEM AREA
void name_list::insert()
{
int position=0,i;
char initial;
cout<<"Enter middle initial ";
cin>>initial;
//SEARCH FOR THE POSITION OF THE DASH
for(i=0;i<=count;++i)
{
if(letter =='-')
{
position = i;
}
}
//SHIFT EVERYTHING BY 2
for(i=count+2;i>position;--i)
{
name[i]=name[i-2];
}

name[position+1] = initial;
name[position+2] = '-';
count = count + 2;


}

name_list::name_list()
{
first=new list_node;
first->next=0;
last=first;
}
Last edited on
Topic archived. No new replies allowed.