In: Computer Science
How do you use header files on a program?
I need to separate my program into header files/need to use header files for this program.Their needs to be 2-3 files one of which is the menu. thanks!
#include
#include
#include
using namespace std;
const int maxrecs = 5;
struct Teletype
{
string name;
string phoneNo;
Teletype *nextaddr;
};
void display(Teletype *);
void populate(Teletype *);
void modify(Teletype *head, string name);
void insertAtMid(Teletype *, string, string);
void deleteAtMid(Teletype *, string);
int find(Teletype *, string);
bool is_phone_no(string);
int main()
{
Teletype *list, *current;
string name;
string phoneNo;
int menu;
list = new Teletype; //new reserves space and returns the adress to the list
current = list;
for (int i = 0; i < maxrecs - 1; i++)
{
populate(current);
current->nextaddr = new Teletype;
current = current->nextaddr;
}
populate(current);
current->nextaddr = NULL;
cout << "The contents of the linked list is: " <<
endl;
display(list);
cout << "\nPlease select a number from the menu: "
<< endl;
cout << "(1)Insert new Structure on the linked list."
<< endl;
cout << "(2)Modify an existing structure in the Linked list."
<< endl;
cout << "(3)Delete an existing structure in the Linked list."
<< endl;
cout << "(4)Find an existing Structure from the Linked list."
<< endl;
cout << "(5)Exit the program." << endl;
cin >> menu;
switch (menu)
{
case 1: cout << "Enter Name and number" << endl;
cin >> name >> phoneNo;
insertAtMid(list, name, phoneNo);
cout << "The contents of the link list after inserting: "
<< endl;
display(list);
break;
case 2: cout << "\nName of the person you need to modify:
";
cin >> name;
modify(list, name);
display(list);
break;
case 3: cout << endl;
cout << "Enter Name " << endl;
cin >> name;
deleteAtMid(list, name);
cout << endl;
cout << "The contents of the linked list after deleting is: "
<< endl;
display(list);
break;
case 4: cout << endl<< endl;
cout << "Enter a name (last, first): ";
getline(cin, name); // getline because of the namespace
if (!find(list, name))
{
cout << "The name is not in the current phone list" <<
endl;
}
break;
case 5: break;
}
return 0;
}
void insertAtMid(Teletype *head, string name, string
phone)
{
Teletype *tmp = new Teletype();
tmp->name = name;
tmp->phoneNo = phone;
tmp->nextaddr = head->nextaddr;
head->nextaddr = tmp;
}
void deleteAtMid(Teletype *head, string name)
{
while (head->nextaddr != NULL)
{
if (head->nextaddr->name == name)
{
Teletype *del = head->nextaddr;
head->nextaddr = head->nextaddr->nextaddr;
delete del;
}
head = head->nextaddr;
}
}
void display(Teletype *contents)
{
while (contents != NULL)
{
cout << endl
<< setw(30) << contents->name << setw(20)
<< contents->phoneNo;
contents = contents->nextaddr;
}
}
int find(Teletype *contents, string name)
{
int found = 0;
while (contents != NULL)
{
if (contents->name == name)
{
found = 1;
cout << setw(30) << contents->name << setw(20)
<< contents->phoneNo;
break;
}
else
{
contents = contents->nextaddr;
}
}
cout << endl;
return found;
}
void modify(Teletype *head, string name)
{
string phone;
while (head->nextaddr != NULL)
{
if (head->nextaddr->name == name)
{
cout << "Enter new name and phoneNumber: ";
cin >> name >> phone;
head->nextaddr->name = name;
head->nextaddr->phoneNo = phone;
return;
}
head = head->nextaddr;
}
cout << "No suchrecord found\n";
}
bool is_phone_no(string phoneNo)
{
for (auto it = phoneNo.begin(); it != phoneNo.end(); it++)
{
if (*it > '9' || *it < '0')
return false;
}
return true;
}
void populate(Teletype *record)
{
cout << "Enter a name: ";
getline(cin, record->name);
string phoneNo;
while (true)
{
cout << "Enter the phone number: ";
getline(cin, phoneNo);
try
{
if (!is_phone_no(phoneNo))
{
throw "Exception caught! Invalid phone number\n";
}
record->phoneNo = phoneNo;
break;
}
catch (const char *e)
{
cout << e;
}
}
return;
}
####################################### Teletype.cpp ####################################### #include "Teletype.h" void insertAtMid(Teletype *head, string name, string phone) { Teletype *tmp = new Teletype(); tmp->name = name; tmp->phoneNo = phone; tmp->nextaddr = head->nextaddr; head->nextaddr = tmp; } void deleteAtMid(Teletype *head, string name) { while (head->nextaddr != NULL) { if (head->nextaddr->name == name) { Teletype *del = head->nextaddr; head->nextaddr = head->nextaddr->nextaddr; delete del; } head = head->nextaddr; } } void display(Teletype *contents) { while (contents != NULL) { cout << endl << setw(30) << contents->name << setw(20) << contents->phoneNo; contents = contents->nextaddr; } } int find(Teletype *contents, string name) { int found = 0; while (contents != NULL) { if (contents->name == name) { found = 1; cout << setw(30) << contents->name << setw(20) << contents->phoneNo; break; } else { contents = contents->nextaddr; } } cout << endl; return found; } void modify(Teletype *head, string name) { string phone; while (head->nextaddr != NULL) { if (head->nextaddr->name == name) { cout << "Enter new name and phoneNumber: "; cin >> name >> phone; head->nextaddr->name = name; head->nextaddr->phoneNo = phone; return; } head = head->nextaddr; } cout << "No suchrecord found\n"; } bool is_phone_no(string phoneNo) { for (auto it = phoneNo.begin(); it != phoneNo.end(); it++) { if (*it > '9' || *it < '0') return false; } return true; } void populate(Teletype *record) { cout << "Enter a name: "; getline(cin, record->name); string phoneNo; while (true) { cout << "Enter the phone number: "; getline(cin, phoneNo); try { if (!is_phone_no(phoneNo)) { throw "Exception caught! Invalid phone number\n"; } record->phoneNo = phoneNo; break; } catch (const char *e) { cout << e; } } return; } ####################################### Teletype.h ####################################### #include <iostream> #include <cstdlib> #include <iomanip> using namespace std; const int maxrecs = 5; struct Teletype { string name; string phoneNo; Teletype *nextaddr; }; void display(Teletype *); void populate(Teletype *); void modify(Teletype *head, string name); void insertAtMid(Teletype *, string, string); void deleteAtMid(Teletype *, string); int find(Teletype *, string); bool is_phone_no(string); ####################################### main.cpp ####################################### #include "Teletype.h" int main() { Teletype *list, *current; string name; string phoneNo; int menu; list = new Teletype; //new reserves space and returns the adress to the list current = list; for (int i = 0; i < maxrecs - 1; i++) { populate(current); current->nextaddr = new Teletype; current = current->nextaddr; } populate(current); current->nextaddr = NULL; cout << "The contents of the linked list is: " << endl; display(list); cout << "\nPlease select a number from the menu: " << endl; cout << "(1)Insert new Structure on the linked list." << endl; cout << "(2)Modify an existing structure in the Linked list." << endl; cout << "(3)Delete an existing structure in the Linked list." << endl; cout << "(4)Find an existing Structure from the Linked list." << endl; cout << "(5)Exit the program." << endl; cin >> menu; switch (menu) { case 1: cout << "Enter Name and number" << endl; cin >> name >> phoneNo; insertAtMid(list, name, phoneNo); cout << "The contents of the link list after inserting: " << endl; display(list); break; case 2: cout << "\nName of the person you need to modify: "; cin >> name; modify(list, name); display(list); break; case 3: cout << endl; cout << "Enter Name " << endl; cin >> name; deleteAtMid(list, name); cout << endl; cout << "The contents of the linked list after deleting is: " << endl; display(list); break; case 4: cout << endl<< endl; cout << "Enter a name (last, first): "; getline(cin, name); // getline because of the namespace if (!find(list, name)) { cout << "The name is not in the current phone list" << endl; } break; case 5: break; } return 0; } Hi. Please find the answer above.. In case of any doubts, you may ask in comments. You may upvote the answer if you feel i did a good work!