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...
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.
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:...
Files I need to be edited: I wrote these files and I put them in a...
Files I need to be edited: I wrote these files and I put them in a folder labeled Project 7. I am using this as a study tool for a personal project of mine. //main.cpp #include <iostream> #include "staticarray.h" using namespace std; int main( ) {    StaticArray a;    cout << "Printing empty array -- next line should be blank\n";    a.print();    /*    // Loop to append 100 through 110 to a and check return value    // Should print "Couldn't append 110"...
i need an example of a program that uses muliple .h and .cpp files in c++...
i need an example of a program that uses muliple .h and .cpp files in c++ if possible.
* What does it mean when header files are described as making a program “modular?” What...
* What does it mean when header files are described as making a program “modular?” What are the advantages of this? * You are testing a program that is designed to take in a pair of float values as input. The intended range is from -50.50 to 100.25. What are 10 test cases you would use to test this program, bearing in mind each test case is a set of two numbers? Briefly justify your choices.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT