Can someone help me alter this code to accept input from the user, output any ch
ID: 3845135 • Letter: C
Question
Can someone help me alter this code to accept input from the user, output any changes to a separate text document, titled information.txt, and allow the user to do everything that the skeleton menu indicates? Any and all help is appreciated.
(language C++)
MAIN'''
#include <iostream>
#include <vector>
#include "Person.h"
using namespace std;
void menu();
int find(vector<Person>, string);
int main() {
// Container for holding all Person objects.
vector<Person> people;
menu();
// Struct for holding persons details.
PersonDetails pd1;
pd1.name = "Rj";
pd1.phone = "0101";
pd1.address = "Some street";
pd1.email = "rj@allday.com";
pd1.website = "rjthehero:p.com";
// New person with the above details.
Person p1(pd1);
// Add to the vector.
people.push_back(p1);
// Find a person by name.
cout << find(people, "Rj") << endl;
return 0;
}
void menu() {
//add a person to the text file where names/addresses/etc. are stored
cout << "1. Add Person" << endl;
//search the text file for a name and it will return all of the person's info
cout << "2. Search Directory" << endl;
//removes a person's entry from the text file along with all of their personal data
cout << "3. Remove Person" << endl;
//exits from the program
cout << "4. Exit" << endl;
}
// Finds a person and returns it index.
int find(vector<Person> pv1, string name)
{
for (unsigned int i = 0; i < pv1.size(); ++i)
{
if (pv1.at(i).get_name() == name)
{
return i;
}
}
return -1;
'''
PERSON.H'''
#ifndef PERSON_H_
#define PERSON_H_
#include <string>
using namespace std;
struct PersonDetails
{
string name, phone, address, email, website;
};
class Person
{
private:
string _name;
string _phone;
string _address;
string _email;
string _website;
public:
Person();
Person(PersonDetails p1);
virtual ~Person();
string get_name();
friend ostream & operator << (ostream &, Person &);
};
#endif /* PERSON_H_ */
'''
PERSON.CPP'''
#include "Person.h"
#include <iostream>
Person::Person()
{
// Init all
_name = "";
_phone = "";
_address = "";
_email = "";
_website = "";
}
Person::Person(PersonDetails pd1)
{
_name = pd1.name;
_phone = pd1.phone;
_address = pd1.address;
_email = pd1.email;
_website = pd1.website;
}
Person::~Person()
{}
string Person::get_name()
{
return this->_name;
}
ostream & operator << (ostream &os, Person &p1)
{
os << p1._name;
return os;
}
'''
Explanation / Answer
PROGRAM CODE:
Main.cpp
#include <iostream>
using namespace std;
int menu();
int find(vector<Person>, string);
//function to update the data to file every time
void writeToFile(ofstream& , vector<Person>);
int main() {
// Container for holding all Person objects.
vector<Person> people;
ofstream out("information.txt");
while(true)
{
int choice = menu();
PersonDetails p;
string name;
int index;
switch(choice)
{
case 1: cout<<"Enter name: ";
cin>>p.name;
cout<<" Enter phone: ";
cin>>p.phone;
cout<<" Enter address: ";
cin>>p.address;
cout<<" Enter email: ";
cin>>p.email;
cout<<" Enter website: ";
cin>>p.website;
Person person(p);
people.push_back(person);
writeToFile(out, people);
break;
case 2: cout<<" Enter name to search: ";
cin>>name;
index = find(people, name);
if(index<0)
cout<<" Person not found ";
else
cout<<endl<<people[index];
break;
case 3: cout<<" Enter name to remove: ";
cin>>name;
index = find(people, name);
if(index<0)
cout<<" Person not found ";
else
people.erase(remove(people.begin(), people.end(), index), people.end());
writeToFile(out, people);
break;
case 4: exit(0);
}
}
return 0;
}
int menu() {
int choice;
//add a person to the text file where names/addresses/etc. are stored
cout << "1. Add Person" << endl;
//search the text file for a name and it will return all of the person's info
cout << "2. Search Directory" << endl;
//removes a person's entry from the text file along with all of their personal data
cout << "3. Remove Person" << endl;
//exits from the program
cout << "4. Exit" << endl;
cout<<" Enter your choice: ";
cin>>choice;
return choice;
}
// Finds a person and returns it index.
int find(vector<Person> pv1, string name)
{
for (unsigned int i = 0; i < pv1.size(); ++i)
{
if (pv1.at(i).get_name() == name)
{
return i;
}
}
return -1;
}
void writeToFile(ofstream& out, vector<Person> people)
{
for(int i=0; i<people.size(); i++)
{
out<<people[i].name<<" "<<people[i].phone<<" "<<people[i].address<<" "<<people[i].email;
out<<" "<<people[i].website<<endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.