1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
|
/*
blongho, 2017-07-23
Problem from http://www.cplusplus.com/forum/beginner/219213/
Trying to find a fix
*/
// PersonalInfo.h
#ifndef PERSONALINFO_H
#define PERSONALINFO_H
#include <iostream>
#include <string>
class PersonalInfo
{
private:
std::string firstName;
std::string lastName;
unsigned age;
std::string phoneNum;
public:
PersonalInfo(){}
PersonalInfo(std::string fnameIn, std::string lnameIn, unsigned ageIn, std::string phoneIn)
:firstName{fnameIn}, lastName{lnameIn}, age{ageIn}, phoneNum{phoneIn}
{}
//set functions
void setFirstName(std::string nameIn);
void setLastName(std::string nameIn);
void setAge(unsigned ageIn);
void setTelephone(std::string phoneIn);
//get functions
std::string getFirstName() const;
std::string getLastName() const;
unsigned getAge() const;
std::string getTelephone() const ;
};
#endif /* PERSONALINFO_H */
// PersonalInfo.cpp
#include "PersonalInfo.h"
void PersonalInfo::setFirstName(std::string nameIn)
{
firstName = nameIn;
}
void PersonalInfo::setLastName(std::string nameIn)
{
lastName = nameIn;
}
void PersonalInfo::setAge(unsigned ageIn)
{
age = ageIn;
}
void PersonalInfo::setTelephone(std::string phoneIn)
{
phoneNum = phoneIn;
}
std::string PersonalInfo::getFirstName() const
{
return firstName;
}
std::string PersonalInfo::getLastName() const
{
return lastName;
}
unsigned PersonalInfo::getAge() const
{
return age;
}
std::string PersonalInfo::getTelephone() const
{
return phoneNum;
}
// Employer.h
#ifndef EMPLOYER_H
#define EMPLOYER_H
#include "PersonalInfo.h"
class Employer
{
private:
std::string email;
PersonalInfo pers;
public:
Employer(){}
Employer(std::string fnameIn, std::string lnameIn, unsigned ageIn, std::string phoneIn)
:pers(fnameIn, lnameIn, ageIn, phoneIn){}
// take note of this one! It will be used in Person
Employer(std::string fnameIn, std::string lnameIn, unsigned ageIn, std::string phoneIn, std::string mail)
:pers(fnameIn, lnameIn, ageIn, phoneIn), email{ mail } {}
//set functions
/*
if you must have all the functions from personalInfo here, it is easy e.g
void setFirstName(string fname){pers.setFirstName(fname);}
strint getFirstName(){return pers.getFirstName();}
see how I did it in Person.cpp
*/
void setEmail(std::string emailIn);
//get functions
std::string getEmail()const;
};
#endif /* EMPLOYER_H */
// Employer.cpp
#include "Employer.h"
void Employer::setEmail(std::string emailIn)
{
email = emailIn;
}
std::string Employer::getEmail()const
{
return email;
}
// Person.h
#ifndef PERSON_H
#define PERSON_H
#include "PersonalInfo.h"
#include "Employer.h"
class Person
{
private:
PersonalInfo info;
Employer employer;
public:
Person(){}
Person(std::string fnameIn, std::string lnameIn, unsigned ageIn, std::string phoneIn)
:info(fnameIn, lnameIn, ageIn, phoneIn){}
// constructor to get info for employer
Person(std::string fnameIn, std::string lnameIn, unsigned ageIn, std::string phoneIn, std::string email)
:employer(fnameIn, lnameIn, ageIn, phoneIn, email) {}
//set functions
void setFirstName(std::string nameIn);
void setLastName(std::string nameIn);
void setAge(unsigned ageIn);
void setTelephone(std::string phoneIn);
void setEmail(std::string emailIn);
//get functions
std::string getFirstName()const { return info.getFirstName(); }
std::string getLastName()const { return info.getLastName(); }
unsigned getAge() const { return info.getAge(); }
std::string getTelephone()const { return info.getTelephone(); }
std::string getEmail()const { return employer.getEmail(); }
};
std::ostream &operator<<(std::ostream &os, const Person &person);
#endif /* PERSON_H */
// Person.cpp
#include "Person.h"
void Person::setFirstName(std::string nameIn)
{
info.setFirstName(nameIn);
}
void Person::setLastName(std::string nameIn)
{
info.setLastName(nameIn);
}
void Person::setAge(unsigned ageIn)
{
info.setAge(ageIn);
}
void Person::setTelephone(std::string phoneIn)
{
info.setTelephone(phoneIn);
}
void Person::setEmail(std::string emailIn)
{
employer.setEmail(emailIn);
}
// overload Person object so it will be easy to print
std::ostream & operator<<(std::ostream & os, const Person & person)
{
os << "First Name : " << person.getFirstName() << std::endl
<< "Last Name : " << person.getLastName() << std::endl
<< "Age : " << person.getAge() << std::endl
<< "Telephone : " << person.getTelephone() << std::endl;
// if person doesnt have an email, print empty line
if (person.getEmail() == "") {
os << "";
}
else {
os << "Email : " << person.getEmail() << std::endl;
}
os<< std::endl;
return os;
}
//>"You should have a .cpp file and a .h file for each class you create."
// make another class for records (People, I call it)
#ifndef PEOPLE_H
#define PEOPLE_H
#include "Person.h"
#include <vector>
class People
{
private:
std::vector<Person> persons;
public:
People(): persons{}{}
~People() {};
int getPeopleCount() const { return persons.size(); }
void addPersons();
void showPersons();
void menu();
};
#endif // !PEOPLE_H
// People.cpp
#include "People.h"
using std::cout;
using std::endl;
using std::cin;
using std::getline;
using std::vector;
using std::string;
void People::addPersons()
{
int entries = 0;
cout << "How many records do you want to input?: ";
cin >> entries;
// value of entries must be atleast 10: For demo, i will use two
while (entries < 2) {
cout << "You must input at least 10 records. Please input a bigger value.\n";
cin >> entries;
}
cin.get();
string temp;
Person tmpPerson;
for (int i = 0; i< entries; i++){
cout << endl << "Enter data for person #" << 1 + i << endl;
cout << "What is the first name? ";
getline(cin, temp);
tmpPerson.setFirstName(temp);
cout << "What is the last name? ";
getline(cin, temp);
tmpPerson.setLastName(temp);
cout << "What is the age? ";
getline(cin, temp);
tmpPerson.setAge(stoi(temp)); // convert string to int
cout << "What is the telephone number? ";
getline(cin, temp);
tmpPerson.setTelephone(temp);
cout << "What is your email? ";
getline(cin, temp);
tmpPerson.setEmail(temp);
persons.push_back(tmpPerson);
cout << endl;
}
}
void People::showPersons()
{
if (getPeopleCount() == 0) {
cout << "There are no entries yet!" << endl;
}
else {
cout << "Person on record : " << getPeopleCount() << endl;
cout << "------------------------------------" << endl;
for (auto &person : persons){
cout << person << endl;
}
}
}
void People::menu()
{
bool again = true;
do {
int choice = 0;
cout << "---Main Menu---\n";
cout << "1. Input information\n";
cout << "2. Display records\n";
cout << "3. Exit\n";
cout << "Your choice : ";
cin >> choice;
cin.get();
switch (choice){
case 1:
addPersons();
again = true;
break;
case 2:
showPersons();
again = true;
break;
case 3:
cout << "Exiting program..." << endl;
again = false;
break;
}
} while (again);
}
// main.cpp
#include "People.h"
int main()
{
People people;
people.menu();
return 0;
}
|