Hi I am trying to get this program to work to insert a node into the list and move the list
down so the node will fit. I get the node to be inserted into the right place but the list
is copied as such.
if 1. joe
2. linda
3. harvey
4. stacey
5. klaus

if i insert a node into 2 the list will look like this
1.joe
2. node inserted
3. linda
4. linda
5. linda
6. linda


i need the list to be like the below after node is inserted.
1. joe
2. inserted list
3.linda
4.harvey
5. stacey
6. klaus


Can you please help me figure this out for the doinsert function.
and the dodelete function deletes the node that the user
specifies.

#include<iostream>
#include<conio.h>        //for clearscreen
#include<stdlib.h>      //for clearscreen
#include<string.h>      //allows string processing
#include<fstream>      //for files in c++
#include<assert.h>    //used to detect file errors

using namespace std;

struct Listnode
{
	char firstname[30];
	char lastname[30];
	int age;
};

struct Listtype
{
	Listnode* person[30];
	int size;
};

//prototypes
Listtype initialize(Listtype &list);

void fillList(Listtype & list);

void printList(Listtype & list);

void doInsert(Listtype &list);

void doDelete(Listtype &list);
int menu();

void main(){
	Listtype List;
	int choice;
		List=initialize(List);
	do{
		system("cls");        //clears screen
	    choice=menu();
		switch(choice)
		{
		    case 1: fillList(List);break;
			case 2: printList(List);break;
            case 3: doInsert(List);break;
			case 4: doDelete(List);break;
			case 5: break;
			default: cout<<"\nINVALID...1-5 ONLY!\n";
		    cin.ignore();
			cin.ignore();
					 
		}
	}while(choice !=5);

}

int menu()
{
	int choice;
     cout<<"\nMENU FOR LAB1\n";
	 cout<<"1.Fill the list from file data.txt\n"
		 <<"2.Print the list (include size of the list)\n"
		 <<"3.Insert a record into the list\n"
		 <<"4.Delete a record from the list\n"
		 <<"5.Quit"<<endl<<endl<<endl;

	 cout<<"ENTER YOUR CHOICE_____\n";
	 cin >>choice;
	 return choice;

}

Listtype initialize(Listtype & list)
	//precondition: List is undefined
	//postconditon: List is initialized to NULL pointers
{
	
	
	for(int i=0; i<30; i++)
	{
		list.person[i]=NULL;
	}
		list.size=0;
		return list;
}

void fillList(Listtype & list)
	//precondition: List has been initialized to NULL pointers
	//postcondition: List contains the firstname, lastname, age of people in file data.txt; list.size is the number of people in the file
{
	
	char firstname[30];
	char lastname[30];
    int age=0,i=0;

	
	ifstream infile("data.txt",ios::in);
	assert(!infile.fail());
	

	while(infile>>firstname>>lastname>>age)
	{
		//infile>>lastname;
		//infile>>age;
		list.person[i]=new Listnode;
		
		strcpy(list.person[i]->firstname,firstname);
		strcpy(list.person[i]->lastname,lastname);
		list.person[i]->age=age;
        i++;


	}
	list.size=i;
}

void printList(Listtype &list)
//precondition: list contains valid data
//postcondition: the contents and size of the list are output to screen
{
	system("cls");
	int i;
	for(i=0; i<list.size; i++)
		cout<<i+1<<" "<<list.person[i]->firstname<<" "<<
		list.person[i]->lastname<<"  "<<list.person[i]->age<<endl;
	cout<<"\nSize of list="<<list.size<<endl;
	
	cin.ignore();
	cin.ignore();
	return;
}

void doInsert(Listtype &list)
	//precondition: list has only the list from file data.txt
	//postcondtion: list is filled with user data and appended by adding a new list at a position specified by user
{
	system("cls");
	
	char firstname[30];
	char lastname[30];
	int age=0;
	int	pos=0;

	
	cout<<"input first\n";
	cin>>firstname;
	cout<<"last\n";
	cin>>lastname;
	cout<<"age\n";
	cin>>age;
	cout<<"pos\n";
	cin>>pos;
	list.size++;
	
	int rev=pos-1;
	



	for(pos; pos<list.size; pos++ )
	{
		
		list.person[pos+1]=list.person[pos];
		
	}	
	  

	
	for(pos; pos!=NULL; pos-- )
	{
		
		list.person[pos-1]=list.person[pos];
		
	}	

	list.person[rev]=new Listnode;
	strcpy(list.person[rev]->firstname,firstname);
	strcpy(list.person[rev]->lastname,lastname);
	list.person[rev]->age=age;
	
		
	cin.ignore();
	cin.ignore();

}

void doDelete (Listtype &list)
//precondition:list has data from the user and appended data
//postcondition:a list can be removed by specifing lastname, firstname

{

	char lastname[30], firstname[30];



	int i=0,remove;

	cout << "Enter the name of the record to delete: ";

	cin >> lastname >> firstname;

	

	for (i; i<list.size; i++)

	{


		if (strcmp(lastname, list.person[i]->lastname)!=0 && strcmp(firstname, list.person[i]->firstname)!=0) 
			

		{

			remove = i;

			cout << "position remove is executed" << endl;

			for (i; i<list.size; i++)

			{

				list.person[i]=list.person[i+1];

			}

			break;

		}

		else if (i=list.size)

		{

			cout << "NAME NOT IN LIST" << endl;

			cout << "Hit return to continue" << endl;

		}

	}

	delete list.person[remove];

	list.size--;

}

Recommended Answers

All 7 Replies

lines 171-176: what's the purpose of that loop? Just delete it.

commented: no help +0

lines 171-176: what's the purpose of that loop? Just delete it.

Removing that loop wont change anything. Sorry I was testing stuff and i left the loop it has no purpose. I need to know how can i insert into the list without copying the address of each node. For example
if I have harry, donny, john, chris, henry
and i want to add a node at postion 2 the first loop rearanges the pointers but
and the list looks like this: harrry, node inserted, donny, donny, donny, donny.

I need the list to look like harry, node inserted, donny, john, chris, henry.

Can you please help. I have been trying to figure this out for weeks.

I thought this website was for helping people I have not gotten a single help. I have been going over and over on this code and no help

I thought this website was for helping people I have not gotten a single help. I have been going over and over on this code and no help

It is for helping you. I just got home from work is why I have not tried to help you more.

Here is the correction to that insert function. Note that neither of the two loops you had work. In order to expand the array you have to start from the end of the array and work forward, decrementing the loop counter along the way.

Also you need to add some code that verifies that you don't attempt to insert more names then the array can hold. You have hardcoded the array size as 30, so you need to produce an error message if you attempt to insert a 31st name. There are better ways to do it so that you can enter as many names as you wish. One way is to allocate the number of pointers so that they can be expended when and if needed. Another way is to use a linked list, but you may or may not know that yet. A third way is to use either c++ <vector> or <list> class, but you many not know that yet either.

void doInsert(Listtype &list)
	//precondition: list has only the list from file data.txt
	//postcondtion: list is filled with user data and appended by adding a new list at a position specified by user
{
	system("cls");
	
	char firstname[30];
	char lastname[30];
	int age=0;
	int	pos=0;

	
	cout<<"input first\n";
	cin>>firstname;
	cout<<"last\n";
	cin>>lastname;
	cout<<"age\n";
	cin>>age;
	cout<<"pos\n";
	cin>>pos;
	list.size++;
	
	
    for(int i = list.size; i > pos; i-- )
	{
		
		list.person[i]=list.person[i-1];
		
	}	

	list.person[pos]=new Listnode;
	strcpy(list.person[pos]->firstname,firstname);
	strcpy(list.person[pos]->lastname,lastname);
	list.person[pos]->age=age;
	
		
	cin.ignore();
	cin.ignore();

}

Thank you for the help, I tried what you told me to count down instead of up and still puts the node in the wrong position. For example if you put it
in position 2 it would go in position 3.

Remember that the numbers start with 0, not 1. So if you enter 2 that is the third node -- 0, 1, 2

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.