The code I have so far is below (C++): WitPerson.cpp #include <string> #include
ID: 3917591 • Letter: T
Question
The code I have so far is below (C++):
WitPerson.cpp
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
#include "WitPerson.h"
//Initialization
WitPerson::WitPerson() {
WitPerson::Name = "Default";
WitPerson::Addr = "Default";
WitPerson::Wnum = "0";
WitPerson::Category = "Unknown";
WitPerson::Role = "Unknown";
}
//Constructor
WitPerson::WitPerson(string name, string addr, string wNum, string role) {
WitPerson::Name = name;
WitPerson::Addr = addr;
WitPerson::Wnum = wNum;
WitPerson::Role = role;
}
//Copy Constructor
WitPerson::WitPerson(const WitPerson &person) {
Name = person.Name;
Addr = person.Addr;
Wnum = person.Wnum;
Category = person.Category;
Role = person.Role;
}
//Destructor
WitPerson::~WitPerson(void) {
//delete this;
}
//stream insertion operator
ostream& operator<< (ostream& strm, const WitPerson& obj) {
strm << "The name is : " << obj.Name << endl;
strm << "The address is : " << obj.Addr << endl;
strm << "The W - Number is : " << obj.Wnum << endl;
strm << "The Role is : " << obj.Role << endl;
return strm;
}
//stream extraction operator
istream& operator >> (istream& in, WitPerson&c)
{
cout << "Enter Name " << endl;
in >> c.Name;
cout << "Enter Address " << endl;
in >> c.Addr;
cout << "Enter W-Number " << endl;
in >> c.Wnum;
cout << "Enter Role " << endl;
in >> c.Role;
return in;
}
ofstream& operator<< (ofstream& ofs, const WitPerson &src)
{
ofs << src.Name << '/' << src.Addr << '/' << '/' << src.Wnum << '/' << src.Role << endl;
return ofs;
}//end ofstream
ifstream& operator >> (ifstream& ifs, WitPerson &src)
{
string record;
while (getline(ifs, record))
{
int index = record.find('/');
src.Name = record.substr();
}
return ifs;
}//end ifstream
//initialize overloaded operator
WitPerson& WitPerson::operator=(const WitPerson &Person) {
if (this != &Person) {
Name = Person.Name;
Addr = Person.Addr;
Wnum = Person.Wnum;
Category = Person.Category;
Role = Person.Role;
}
return *this;
}
WitPerson.h
#ifndef WITPERSON_H
#define WITPERSON_H
class WitPerson {
friend ostream& operator<< (ostream&, const WitPerson&);
friend istream& operator >> (istream&, WitPerson&);
friend ofstream& operator<< (ofstream&, const WitPerson &);
friend ifstream& operator >> (ifstream&, WitPerson &);
public:
WitPerson();
WitPerson(string name, string addr, string wNum, string role);
~WitPerson(void);
string getName(void) const { return Name; };
string getAddr(void) const { return Addr; };
string getWnum(void) const { return Wnum; };
string getCategory(void) const { return Category; };
string getRole(void) const { return Role; };
private:
WitPerson(const WitPerson &person);
WitPerson& operator=(const WitPerson&);
string Name;
string Addr;
string Wnum;
string Category;
string Role;
};
#endif
Source.cpp
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
#include "WitPerson.h"
void addUser(WitPerson people[], int index, string role);
void User() { cout << endl; }
int main() {
string name, address, wNumber, role;
WitPerson *people = new WitPerson[8];
vector<string> dest;
//Manual
int size = 0;
int user_choice;
do {
cout << "Enter 1 to add a student, 2 to add a faculty member, 3 to enter a staff member, 4 to display the present list, or 0 to quit: " << endl;
cin >> user_choice;
cin.ignore();
switch (user_choice) {
case 1:
addUser(people, size, role);
size++;
break;
case 2:
addUser(people, size, role);
size++;
break;
case 3:
addUser(people, size, role);
size++;
break;
case 4:
cout << "Here is the present list:" << endl;
for (int i = 0; i < size; i++) {
cout << "Entry #" << i + 1 << endl;
cout << "Name: " << people[i].getName() << endl;
cout << "Address: " << people[i].getAddr() << endl;
cout << "W-Number: " << people[i].getWnum() << endl;
cout << "Role: " << people[i].getRole() << endl << endl;
}
break;
default:
cout << "Here is the present list:" << endl;
for (int i = 0; i < size; i++) {
cout << "Entry #" << i + 1 << endl;
cout << "Name: " << people[i].getName() << endl;
cout << "Address: " << people[i].getAddr() << endl;
cout << "W-Number: " << people[i].getWnum() << endl;
cout << "Role: " << people[i].getRole() << endl << endl;
}
return 0;
} //switch end
} while (size < 8); //do-while loop end
cout << "Here is the present list:" << endl;
for (int i = 0; i < size; i++) {
cout << "Entry #" << i + 1 << endl;
cout << "Name: " << people[i].getName() << endl;
cout << "Address: " << people[i].getAddr() << endl;
cout << "W-Number: " << people[i].getWnum() << endl;
cout << "Role: " << people[i].getRole() << endl << endl;
}
return 0;
}
void addUser(WitPerson people[], int index, string role) {
/*string name;
string address;
string wNumber;
cin >> name;
cin >> address;
cin >> wNumber;*/
string user_info;
cout << "Please enter your name, address, W-Number, and Role seperated by commas: ";
getline(cin, user_info);
cin.ignore();
//tokenize string
string token;
size_t commaPos;
int counter = 0;
while (user_info.size() != 0)
{
commaPos = user_info.find(",");
if (commaPos != string::npos)
{
token = user_info.substr(0, commaPos);
user_info = user_info.substr(commaPos + 1);
counter++;
if (counter == 1)
{
cin >> people[counter].getName();
counter++;
}//end 1st nested if
else if (counter == 2)
{
cin >> people[counter].getAddr();
counter++;
}//end 2nd nested if
else if (counter == 3)
{
cin >> people[counter].getWnum();
counter++;
}//end 3rd nested if
else if (counter == 4)
{
cin >> people[counter].getRole();
counter++;
}//end 4th nested if
} //end whole if statement
else
{
token = user_info;
user_info = "";
}
//WitPerson person = new WitPerson(c1, c2, c3, role);
//people[index] = *p;
} //end while loop
}
Explanation / Answer
here is your files : ---------->>>>>>>>
WitPerson.h : ------------>>>>>>>>
#ifndef WITPERSON_H
#define WITPERSON_H
class WitPerson {
friend ostream& operator<< (ostream&, const WitPerson&);
friend istream& operator >> (istream&, WitPerson&);
friend ofstream& operator<< (ofstream&, const WitPerson &);
friend ifstream& operator >> (ifstream&, WitPerson &);
public:
WitPerson();
WitPerson(string name, string addr, string wNum, string role);
WitPerson(const WitPerson &person);
WitPerson& operator=(const WitPerson&);
~WitPerson(void);
string getName(void) const { return Name; };
string getAddr(void) const { return Addr; };
string getWnum(void) const { return Wnum; };
string getCategory(void) const { return Category; };
string getRole(void) const { return Role; };
private:
string Name;
string Addr;
string Wnum;
string Category;
string Role;
};
#endif?
WitPerson.cpp : --------->>>>>>>
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
#include "WitPerson.h"
//Initialization
WitPerson::WitPerson() {
WitPerson::Name = "Default";
WitPerson::Addr = "Default";
WitPerson::Wnum = "0";
WitPerson::Category = "Unknown";
WitPerson::Role = "Unknown";
}
//Constructor
WitPerson::WitPerson(string name, string addr, string wNum, string role) {
WitPerson::Name = name;
WitPerson::Addr = addr;
WitPerson::Wnum = wNum;
WitPerson::Role = role;
}
//Copy Constructor
WitPerson::WitPerson(const WitPerson &person) {
Name = person.Name;
Addr = person.Addr;
Wnum = person.Wnum;
Category = person.Category;
Role = person.Role;
}
//Destructor
WitPerson::~WitPerson(void) {
//delete this;
}
//stream insertion operator
ostream& operator<< (ostream& strm, const WitPerson& obj) {
strm << "The name is : " << obj.Name << endl;
strm << "The address is : " << obj.Addr << endl;
strm << "The W - Number is : " << obj.Wnum << endl;
strm << "The Role is : " << obj.Role << endl;
return strm;
}
//stream extraction operator
istream& operator >> (istream& in, WitPerson&c)
{
cout << "Enter Name " << endl;
in >> c.Name;
cout << "Enter Address " << endl;
in >> c.Addr;
cout << "Enter W-Number " << endl;
in >> c.Wnum;
cout << "Enter Role " << endl;
in >> c.Role;
return in;
}
ofstream& operator<< (ofstream& ofs, const WitPerson &src)
{
ofs << src.Name << '/' << src.Addr << '/' << '/' << src.Wnum << '/' << src.Role << endl;
return ofs;
}//end ofstream
ifstream& operator >> (ifstream& ifs, WitPerson &src)
{
string record;
while (getline(ifs, record))
{
int index = record.find('/');
src.Name = record.substr();
}
return ifs;
}//end ifstream
//initialize overloaded operator
WitPerson& WitPerson::operator=(const WitPerson &Person) {
if (this != &Person) {
Name = Person.Name;
Addr = Person.Addr;
Wnum = Person.Wnum;
Category = Person.Category;
Role = Person.Role;
}
return *this;
}
source.cpp : ------------>>>>>>>>>>
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
#include "WitPerson.cpp"
void addUser(vector<WitPerson> &people,string category);
void User() { cout << endl; }
int main() {
vector<WitPerson> people;
vector<string> dest;
//Manual
int user_choice;
int i = 0;
do {
cout << "Enter 1 to add a student, 2 to add a faculty member, 3 to enter a staff member, 4 to display the present list, or 0 to quit: " << endl;
cin >> user_choice;
cin.ignore();
switch (user_choice) {
case 1:
addUser(people,"Student");
break;
case 2:
addUser(people,"Faculty");
break;
case 3:
addUser(people,"Staff");
break;
case 4:
cout << "Here is the present list:" << endl;
i = 0;
for (auto it = people.begin(); it != people.end(); it++,i++) {
cout << "Entry #" << i + 1 << endl;
cout << "Name: " << (*it).getName() << endl;
cout << "Address: " << (*it).getAddr() << endl;
cout << "W-Number: " << (*it).getWnum() << endl;
cout << "Role: " << (*it).getRole() << endl << endl;
}
break;
default:
cout << "Here is the present list:" << endl;
i = 0;
for (auto it = people.begin(); it != people.end(); it++,i++) {
cout << "Entry #" << i + 1 << endl;
cout << "Name: " << (*it).getName() << endl;
cout << "Address: " << (*it).getAddr() << endl;
cout << "W-Number: " << (*it).getWnum() << endl;
cout << "Role: " << (*it).getRole() << endl << endl;
}
return 0;
} //switch end
} while (people.size() < 8); //do-while loop end
cout << "Here is the present list:" << endl;
i = 0;
for (auto it = people.begin(); it != people.end(); it++,i++) {
cout << "Entry #" << i + 1 << endl;
cout << "Name: " << (*it).getName() << endl;
cout << "Address: " << (*it).getAddr() << endl;
cout << "W-Number: " << (*it).getWnum() << endl;
cout << "Role: " << (*it).getRole() << endl << endl;
}
return 0;
}
void addUser(vector<WitPerson> &people,string role) {
/*string name;
string address;
string wNumber;
cin >> name;
cin >> address;
cin >> wNumber;*/
string user_info;
cout << "Please enter your name, address, and W-Number seperated by commas: ";
getline(cin, user_info);
cin.ignore();
//tokenize string
string token;
size_t commaPos;
string name,addr,wnum;
int counter = 1;
user_info += ',';
while (user_info.size() != 0)
{
commaPos = user_info.find(",");
if (commaPos != string::npos)
{
token = user_info.substr(0, commaPos);
user_info = user_info.substr(commaPos + 1);
if (counter == 1)
{
name = token;
counter++;
}//end 1st nested if
else if (counter == 2)
{
addr = token;
counter++;
}//end 2nd nested if
else if (counter == 3)
{
wnum = token;
counter++;
}//end 3rd nested if
} //end whole if statement
else
{
token = user_info;
user_info = "";
}
//WitPerson person = new WitPerson(c1, c2, c3, role);
//people[index] = *p;
} //end while loop
people.push_back(WitPerson(name,addr,wnum,role));
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.