I am trying to get a program that gets a list from a txt files
outputs it. then inserts a node into the list and the list
grows dynamically. Everytime I insert into the list the
list is filled with the same values following the insertion
point. For this example i used a data.txt file. The way the program works is the user chooses wher to put the node in the list. So
if I choose three I would be putting the node between Eric
and Mary. The program puts the node there but changes Mary
to eric doe, and Alicia to Eric doe. How do I make it just insert
and make the list have 6 members by just moving the list
down. Also the delte funciton finds the name and the last name
and delets that node from the list. Can you please help.
data.txt
john doe 30
mary doe 21
Eric doe 20
Mary doe 33
Alicia doe 50
#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++)
{
for(rev; rev<list.size;rev--)
list.person[rev]=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--;
}