0

The problem:
Given a list of imployees, create a simple program that allows the user to insert, delete, display and search an employee. The prog. shall display 10 imployees as default. Also a list of menu should be display. The menu names are insert, delete, dsplay and search.

My questions are:
1. what syntax should i use to insert character that allows 'space'?
2. is there a syntax that can 'search' name within a list,,, what is it?

*ideally, it should delete the list or name with a 'current node'.
*the bold part of the prog. is my problem....'searchng to a list'.

i created this prog. guided by the notes given to me.

#include <iostream.h>
struct node
{
char name[50], search[50];
node *nxt;
};

node *strtPtr = NULL;
node *current;
int choice;

void insert()
{
node *emp,*emp1;
emp=new node;
cout<<"Name of Employee:\n";
cin>>emp->name;
emp->nxt=NULL;
if(strtPtr==NULL)
{
strtPtr=emp;
current=strtPtr;
}
else
{
emp1= strtPtr;
while(emp1->nxt != NULL)
{
emp1=emp1->nxt;
}
emp1->nxt = emp;
current=emp;
}
cout<<"\n\n";
}

void display()
{
node *emp;
emp=strtPtr;
cout<<"\nList of the Employees: \n";
if(emp==NULL)
cout<<"List is empty\a\a\n\n";
else
{
while(emp!=NULL)
{
cout<<emp->name;
if(emp==current)
cout<<" <---";
cout<<endl;
emp=emp->nxt;
}
cout<<"\nEnd of List\n\n\n";
}
}

void del()
{
node *emp, *emp1;
current=strtPtr;
if(strtPtr==NULL)
cout<<"The List is empty\a\a\n";
else
{
emp=strtPtr;
if(emp->nxt==NULL)
{
delete emp;
strtPtr=NULL;
}
else
{
while(emp->nxt!=NULL)
{
emp1=emp;
emp=emp->nxt;
}
delete emp;
emp1->nxt=NULL;
}
}
}

void search()
{
node *emp,*emp1;
emp=strtPtr;
if(emp==NULL)
{
cout<<"List is empty\a\a\n\n";
}
else
{
emp1=new node;
cout<<"Search (name): ";
cin>>emp1->search;
cout<<endl;
if(emp==emp1)
{
emp=current;
}
else
{
while(emp!=emp1)
{
emp=emp->nxt;
emp->nxt=emp1;
current=emp; 
} 
}
}
}

void main()
{
strtPtr=NULL;
do
{
cout<<"Select your option\n"
<<"[0] Exit\n"
<<"[1] Insert new employee\n"
<<"[2] Display list of employees\n"
<<"[3] Delete employee\n"
<<"[4] Search employee\n\n>>";
cin>>choice;
cout<<"\n";

switch(choice)
{
case 1: insert(); break;
case 2: display(); break;
case 3: del(); break;
case 4: search(); break;
}
}
while(choice != 0);
}

Sir/madam, i'm new in making c++ program...hope you can help me with this... thank you..God bless..

3
Contributors
4
Replies
5
Views
9 Years
Discussion Span
Last Post by mengmarc
0

>1. what syntax should i use to insert character that allows 'space'?
cin.getline:

cin.getline ( emp->name, 50 );

>2. is there a syntax that can 'search' name within a list,,, what is it?
It's called a loop:

node *it = strtPtr;

while ( it != 0 ) {
  if ( strcmp ( it->name, key ) == 0 )
    break;

  it = it->nxt;
}

if ( it != 0 )
  cout<<"Found '"<< it->name <<"'\n";

And if I may be so bold, is there a shortage of vowels on your system? nxt and strtPtr are exceptionally annoying to type and read.

0

1. what syntax should i use to insert character that allows 'space'?

U can declare ur variables using string or even character (eg) string Name; and if u gona accept it from the user u can achieve that by this getline(cin, Name) or if u just want to right u can also use getline() method
geline(Name);

2. is there a syntax that can 'search' name within a list,,, what is it?

Yes there is a way U can use a comparison of variables with boolean to search

0

Traicey, that's all well and good, if the OP's compiler supports the string class, which it may not as shown by the use of the pre-standard iostream.h header.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.