(C++ language, IDE Visual Studio 2013, Book: \"starting out with >>> C++ from co
ID: 670366 • Letter: #
Question
(C++ language, IDE Visual Studio 2013, Book: "starting out with >>> C++ from control structures through objects, Eight Edition", question on page 1024, number 17. Phone book vector)
*IMPORTANT - for every .cpp file there must be a .h file except for the driver with the int main in it.*
- Write a class named PhoneBookEntry that has members for a person's name and phone number. Then write a program that creates at least FIVE PhoneBookEntry objects and stores them in a vector. After the objects are stored in the vector, use a loop to display the contents of each object in the vector.
/*Sample Driver Output*/
Creating five entries in the phone book...
Entry added.
------------------------
Entry added.
------------------------
Entry added.
------------------------
Entry added.
------------------------
Here are the entries:
-----------------------
Name: Sally Smith
Phone number: 313-123-4567
-----------------------
Name: Jason Jones
Phone number: 415-123-4567
-----------------------
Name: Tommy Madison
Phone number: 516-123-4567
-----------------------
Explanation / Answer
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class PhoneBookEntry{
public:
string name;
string phoneno;
};
int main()
{
vector<PhoneBookEntry> listPBE = new vector<PhoneBookEntry>();
PhoneBookEntry temp;
for(int i = 0 ; i < 5 ; i ++){
temp = new PhoneBookEntry();
cin>>temp.name;
cin>>temp.phoneno;
listPBE.push_back (temp);
cout<<"Entry added"<<endl;
}
for(int i = 0 ; i < 5 ; i ++){
cout<<"Name: "<<listPBE[i].name;
cout<<"PhoneNo: "<<listPBE[i].phoneno;
cout<<endl;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.