Question

Written in C++ language Write a program that keeps track of a telephone directory. The program...

Written in C++ language

Write a program that keeps track of a telephone directory. The program should create a
structure, Person, with the following data:


struct Person
{
string firstName;
string lastName;
string phoneNumber;
};
The program should then create a vector of Person structs, called directory. Your program
should have functions to:
- Read the vector data from a data file “telephoneData.txt” into the vector directory.
- Print the details of all Persons in the vector.

You may use the following “telephoneData.txt” file:


Tom Garcia 855-433-2076
Nancy James 202-872-1010
Bill Meyer 120-343-5623
Jack Didier 352-654-1983


Your program should print something like this:
Name Telephone
-----------------------------
Tom Garcia 855-433-2076
Nancy James 202-872-1010
Bill Meyer 120-343-5623
Jack Didier 352-654-1983

Please comment and show output.

Code must have functions also read prompt and print

0 0
Add a comment Improve this question Transcribed image text
Answer #1

IF YOU HAVE ANY DOUBTS COMMENT BELOW I WILL BE THERE TO HELP YOU

ANSWER:

CODE:

#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

struct Person
{
string firstName;
string lastName;
string phoneNumber;
};

int main() {
vector<struct Person> directory;
  
/*
Creating file:"telephonedata.txt"
If you already have file in your current directory then remove write operation of FILE
*/
/*File Writing Begin*/
ofstream outputFile;
outputFile.open("telephoneData.txt");
if (!outputFile) {
cerr << "Unable to create file in the Directory";
exit(1);
}
outputFile<<"Tom"<<" "<<"Garcia"<<" "<< "855-433-2076"<<endl;
outputFile<<"Nancy"<<" "<<"James"<<" "<<"202-872-1010"<<endl;
outputFile<<"Bill"<<" "<<"Meyer"<<" "<<"120-343-5623"<<endl;
outputFile<<"Jack"<<" "<<"Didier"<<" "<<"352-654-1983"<<endl;
outputFile.close();
/*FIle writing end*/
  
/*FIle reading begin*/
ifstream inputFile;
inputFile.open("telephoneData.txt");

if (!inputFile) {
cerr << "File Not found in the Directory";
exit(1);
}
while(inputFile)
{
struct Person temp; /*Temporary struct person object to store data from file*/
inputFile >> temp.firstName>>temp.lastName>>temp.phoneNumber;

directory.push_back(temp); /*String struct Person in Vector Directory*/
}
inputFile.close();
/*File reading end*/

/*Console output from vector begin*/
cout<<"Name "<<" Telephone"<<endl;
cout<<"--------------------------"<<endl;
/*Using Iterator to access data from vector*/
for(std::vector<struct Person>::iterator it = directory.begin() ; it != directory.end(); ++it)
{
struct Person temp=*it; /*Receving struct Person through Iterator from Vector directory*/
cout<<temp.firstName<<" "<<temp.lastName<<" "<<temp.phoneNumber<<endl; /*Console Output*/
}
/*Console Input from Vector end*/
return 0;
}

HOPE IT HELPS YOU

RATE THUMBSUP PLEASE

Add a comment
Know the answer?
Add Answer to:
Written in C++ language Write a program that keeps track of a telephone directory. The program...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Must be written in C++ Display results and comments Write a program that sorts a vector...

    Must be written in C++ Display results and comments Write a program that sorts a vector of names alphabetically using the selection sort and then searches the vector for a specific name using binary search. To do that, you need to write three functions: 1. void selSort (vector <string> & v): sorts the vector using selection sort 2. void display (const vector <string> & v): displays the vector contents 3. int binSearch (const vector <string> & v, string key): searches...

  • Write a simple telephone directory program in C++ that looks up phone numbers in a file...

    Write a simple telephone directory program in C++ that looks up phone numbers in a file containing a list of names and phone numbers. The user should be prompted to enter a first name and last name, and the program then outputs the corresponding number, or indicates that the name isn't in the directory. After each lookup, the program should ask the user whether they want to look up another number, and then either repeat the process or exit the...

  • in c++ Program 1 Write a program that sorts a vector of names alphabetically using the...

    in c++ Program 1 Write a program that sorts a vector of names alphabetically using the selection sort and then searches the vector for a specific name using binary search. To do that, you need to write three functions I. void selSort (vector string &v: sorts the vector using selection sort 2. void display (const vector <string & v): displays the vector contents . int binSearch (const vector <ing& v, string key): searches the vector for a key returns the...

  • Write a c++ program as follows. Print the list of runners and times as example below....

    Write a c++ program as follows. Print the list of runners and times as example below. Then print the name of the fastest runner and his/her time (in hours and minutes). Also, find the second fastest runner. Print the name and his/her time (in hours and minutes). The program should have a method that takes as input an array of integers and returns the index corresponding to the person with the lowest time. The program should apply this method to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT