Program must be written in C++: I am looking for design help with how to create
ID: 3793908 • Letter: P
Question
Program must be written in C++:
I am looking for design help with how to create this program. I am currently stuck. Write a program that maintains a telephone directory. The Telephone directory keeps records of people’s names and the corresponding phone numbers.The program should read an unknown number of names and phone numbers from a file “data.txt” and store them in a vector of strings. Each line in the input file contains a first name, last name and phone number in the following format xxx-xxx-xxxx.
Explanation / Answer
#include<iostream>
#include<vector>
#include<fstream>
#include<string>
//to display input in format include below header file
#include<iomanip>
using namespace std;
int main()
{
ifstream in;
in.open("data.txt");
//declare vector of strings to hold first name , last name and phone number
vector<string> first_name;
vector<string> last_name;
vector<string> phone_number;
string s1, s2, s3;
//check input file is open, if not open, display error
if (!in)
{
cout << "Can't open input file" << endl;
return -1;
}
while (!in.eof())
{
in >> s1 >> s2 >> s3;
first_name.push_back(s1);
last_name.push_back(s2);
phone_number.push_back(s3);
}
//print result
for (int i = 0; i < first_name.size(); i++)
{
cout << "First Name " << "Last Name " << "Phone Number" << endl;
cout << setw(10)<<first_name[i] << " "<<setw(10) << last_name[i] <<" "<< setw(10) << phone_number[i] << endl;
}
}
-----------------------------------------------------------------------------------
//output
First Name Last Name Phone Number
Jonson skjkj 789-9890-578
First Name Last Name Phone Number
Samantha dkklklio 345-868-89
First Name Last Name Phone Number
James sa 689-9875-867
First Name Last Name Phone Number
Bella s 556-5679-097
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.