I have to write an addressbook in c++ and these are the requirements for the add
ID: 3764618 • Letter: I
Question
I have to write an addressbook in c++ and these are the requirements for the addressbook.
// File: data.h
struct data
{
char name[25];
char address[80];
int yearofbirth;
char telno[15];
struct data * next;
};
// File: llist.h
class llist
{
private:
record * start;
char filename[16];
int readfile();
int writefile();
record * reverse(record * );
void cleanup();
public:
llist();
llist(char[]);
~llist();
int addRecord(char[], char[], int, char[]);
int printRecord(char[]);
int modifyRecord(char[], char[], char[]);
void printAll();
int deleteRecord(char[]);
void reverse();
};
// File: llist.cpp
Contains the function definitions for all the class member functions, including the constructor and destructor. The constructor will initialize the class variables. The destructor will deallocate the entire linked list.
Requirements:
-You must define the two following constructors function but you may use either one in your main function
llist mylist; // Instance of class llist
llist mylist( "proj2.txt" );
-You cannot change the class definition without consent
-You may create any other functions you like or add to the menu in the interface.
-You cannot use any of the C I/O functions. Use the C++ versions instead.
-No global variables may be used.
-Name may have spaces and addresses may have multiple lines (use getline).
-Use ifndef directives for the C++ header files
-deleteRecord will delete all records, including duplicates based on the name.
-addRecord will create a record even if it already exists with the same name
-addRecord must add the record to the end of the list
-modifyRecord will modify all records with the same name
-printRecord will print on the screen information for all records with the name specified
-printAll will print the entire address book on the screen
-A make file must be used.
-Implement the reverse method using recursion and add an option to the menu
This will reverse the list without printing it.
-Overload the << operator so it replaces the calling of the printAll method in the user-interface. It should NOT call the printAll method.
-Implement debug mode using preprocessor directives, instead of command line arguments.
Explanation / Answer
#include <bits/stdc++->h>
using namespace std;
llist::llist(){
start = NULL;
}
llist::llist(char[] ch){
ifstream infile;
infile.open(ch);
char s_1[25]; char s_2[80];
int i; char s_3[15];
while (!ch.eof){
infile >> s_1 >> s_2;
infile >> i;
infile >> s_3;
addRecord(s_1,s_2,i,s_3);
}
}
llist::llist(){
start = NULL;
}
llist::~llist(){
start = NULL;
}
int llist::addRecord(char ch_1[],char ch_2[],int i,char ch_3[]){
record* r = new record(ch_1,ch_2,i,ch_3);
r->next = start;
start = r;
return 1;
}
int llist::printRecord(char[] ch){
record* r = start;
while (r != NULL){
if (r->name == ch)
cout << r->name << " " << r->address << " " << r->yearofbirth << " " << r->telno << endl;
r = r->next;
}
return 1;
}
void printAll(){
record* r = start;
while (r != NULL){
cout << r->name << " " << r->address << " " << r->yearofbirth << " " << r->telno << endl;
r = r->next;
}
}
int llist::deleteRecord(char[] ch){
record* curr = start;
record* prev = NULL;
while (curr != NULL){
if (curr->name == ch){
prev->next = curr->next;
return 1;
}
prev = curr;
curr = curr->next;
}
}