Question

In: Computer Science

How do you use header files on a program? I need to separate my program into...

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;
}

Solutions

Expert Solution

#######################################
        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!


Related Solutions

You are to write a class named Rectangle. You must use separate files for the header...
You are to write a class named Rectangle. You must use separate files for the header (Rectangle.h) and implementation (Rectangle.cpp) just like you did for the Distance class lab and Deck/Card program. You have been provided the declaration for a class named Point. Assume this class has been implemented. You are just using this class, NOT implementing it. We have also provided a main function that will use the Point and Rectangle classes along with the output of this main...
Complete the following task in C++. Separate your class into header and cpp files. You can...
Complete the following task in C++. Separate your class into header and cpp files. You can only useiostream, string and sstream. Create a main.cpp file to test your code thoroughly. Given : commodity.h and commodity.cpp here -> https://www.chegg.com/homework-help/questions-and-answers/31-commodity-request-presented-two-diagrams-depicting-commodity-request-classes-form-basic-q39578118?trackid=uF_YZqoK Create : locomotive.h, locomotive.cpp, main.cpp Locomotive Class Hierarchy Presented here will be a class diagram depicting the nature of the class hierarchy formed between a parent locomotive class and its children, the two kinds of specic trains operated. The relationship is a...
Complete the following task in C++. Separate your class into header and cpp files. You can...
Complete the following task in C++. Separate your class into header and cpp files. You can only use iostream, string and sstream. Create a main.cpp file to test your code thoroughly. Given : commodity.h and commodity.cpp here -> https://www.chegg.com/homework-help/questions-and-answers/31-commodity-request-presented-two-diagrams-depicting-commodity-request-classes-form-basic-q39578118?trackid=uF_YZqoK Given : locomotive.h, locomotive.cpp, main.cpp -> https://www.chegg.com/homework-help/questions-and-answers/complete-following-task-c--separate-class-header-cpp-files-useiostream-string-sstream-crea-q39733428 Create : DieselElectric.cpp DieselElectric.h DieselElectric dieselElectric -fuelSupply:int --------------------------- +getSupply():int +setSupply(s:int):void +dieselElectric(f:int) +~dieselElectric() +generateID():string +calculateRange():double The class variables are as follows: fuelSuppply: The fuel supply of the train in terms of a number of kilolitres...
Please provide step by step on how to create the header files for this project. I...
Please provide step by step on how to create the header files for this project. I received code for it but I am getting an error and I know it has to do with my header files. Please provide instructions on how to create these files along with the full code in C++ for the following: Write a C++ program to simulate a service desk. This service desk should be able to service customers that can have one of three...
hi i need to do a C++ program. You are going to practice the use of...
hi i need to do a C++ program. You are going to practice the use of array by implementing the interface of Shuttle Puzzle. Here is an example of how you play the Shuttle Puzzle. Say that you start with a board with 7 holes and there are 3 black and 3 white marbles on the board in this configuration: W W W . B B B The dot (.) represents the empty hole withouth any marble. The objective of...
how can I get my program to read one of 4 text files (chosen/ inputted by...
how can I get my program to read one of 4 text files (chosen/ inputted by user, game1.txt, game2, game3, or game4.txt). and place its information into variables, one of which is an array. I sort of understand this, but I don't know how to make my program know which parts of the textfile go into which variables. my 4 textfiles are in the format: the list of e4-bc need to be entered into my array. B 35 e4 e5...
Please do the math by hand, do not use a program, I need to see the...
Please do the math by hand, do not use a program, I need to see the procedure, the answer itself is less important. Comparison of peak expiratory flow rate (PEFR) before and after a walk on a cold winter's day for a random sample of 9 asthmatics. Use the following data to determine if the patients conditioned changed after a walk. Present your results and make some interpretations. Subject Before After 1 312 300 2 242 201 3 340 232...
I need a scholarship essay for my nursing program. please and thank you.
I need a scholarship essay for my nursing program. please and thank you.
I am Writing a C-Program to read and write files. but none of my code is...
I am Writing a C-Program to read and write files. but none of my code is working like it should be. Please fix all code and supply output response. Please try to use existing code and code in comments. But if needed change any code that needs to be changed. Thank you in advance //agelink.c //maintains list of agents //uses linked list #include <stdio.h> #include <stdlib.h> #define TRUE 1 void listall(void); void newname(void); void rfile(void); void wfile(void); struct personnel {...
WRITE ONLY THE TO DO'S   in FINAL program IN C++ (necessary cpp and header files are...
WRITE ONLY THE TO DO'S   in FINAL program IN C++ (necessary cpp and header files are witten below) "KINGSOM.h " program below: #ifndef WESTEROS_kINGDOM_H_INCLUDED #define WESTEROS_KINGDOM_H_INCLUDED #include <iostream> namespace westeros { class Kingdom{         public:                 char m_name[32];                 int m_population; };         void display(Kingdom&);                      } #endif } Kingdom.cpp Program below: #include <iostream> #include "kingdom.h" using namespace std; namespace westeros {         void display(Kingdom& pKingdom) {                 cout << pKingdom.m_name << ", population " << pKingdom.m_population << endl;                                                               FINAL:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT