#include
#include
#include
#include
using namespace std;
// Define a Person class, including age, gender, and yearlyIncome.
class Person {
public:
Person();
void Print();
void SetData(int a); // FIXME Also set gender and yearly income
void SetGender(string gender);
void SetIncome(int income);
int GetAge();
string GetGender();
int GetIncome();
private:
int age;
string gender;
int yearlyIncome;
};
// Constructor for the Person class.
Person::Person() {
age = 0;
gender = \"default\";
yearlyIncome = 0;
return;
}
// Print the Person class.
void Person::Print() {
cout << \"Age = \" << this->age
<< \", gender = \" << this->gender
<< \", yearly income = \" << this->yearlyIncome
<< endl;
return;
}
// Set the age, gender, and yearlyIncome of a Person.
void Person::SetData(int a) { // FIXME Also set gender and yearly income
this->age = a;
return;
}
void Person::SetGender(string gender){
this->gender = gender;
}
void Person::SetIncome(int income){
this->yearlyIncome = income;
}
// Get the age of a Person.
int Person::GetAge() {
return this->age;
}
string Person::GetGender(){
return this->gender;
}
int Person::GetIncome() {
return this->yearlyIncome;
}
// Get a filename from program arguments, then make a Person for each line in the file.
bool ReadPeopleFromFile(int argc, char* argv[], vector &people) {
Person tmpPrsn;
ifstream inFS;
int tmpAge = 0;
string tmpGender = \"\";
int tmpYI = 0;
if (argc != 2) {
cout << \"\ Usage: [EXECUTABLE FILE] [TEXT DATA FILE], e.g. myprog.exe
dev_people.txt\" << endl;
return true; // indicates error
}
cout << \"Opening file \" << argv[1] << \".\ \";
inFS.open(argv[1]); // Try to open file
if (!inFS.is_open()) {
cout << \"Could not open file \" << argv[1] << \".\ \";
return true; // indicates error
}
while (!inFS.eof()) {
inFS >> tmpAge;
inFS >> tmpGender;
inFS >> tmpYI;
tmpPrsn.SetData(tmpAge); // FIXME Also set gender and yearly income
tmpPrsn.SetGender(tmpGender);
tmpPrsn.SetIncome(tmpYI);
tmpPrsn.Print();
people.push_back(tmpPrsn);
}
inFS.close();
cout << \"Finished reading file.\" << endl;
return false; // indicates no error
}
// Ask user to enter age range.
void GetUserInput(int &ageLowerRange, int &ageUpperRange, string &gender, int
&yILowerIncome,int &yIHigherIncome) {
cout<<\"\ Enter lower range of age: \";
cin >> ageLowerRange;
cout << \"Enter upper range of age: \";
cin >> ageUpperRange;
cout << \"Enter the gender: \";
cin >> gender;
cout << \"Enter the lower range of yearlyIncome: \";
cin >> yILowerIncome;
cout << \"Enter the higher range of yearlyIncome: \";
cin >> yIHigherIncome;
return;
}
// Return people within the given age range.
vector GetPeopleInAgeRange(vector ppl, int lowerRange, int upperRange) {
unsigned int i = 0;
vector pplInAgeRange;
int age = 0;
for (i = 0; i < ppl.size(); ++i) {
age = ppl.at(i).GetAge();
if ((age >= lowerRange) && (age <= upperRange)) {
pplInAgeRange.push_back(ppl.at(i));
}
}
return pplInAgeRange;
}
// Return people with same Gender.
vector GetPeopleWithSpecificGender(vector ptn.
1. #include
#include
#include
#include
using namespace std;
// Define a Person class, including age, gender, and yearlyIncome.
class Person {
public:
Person();
void Print();
void SetData(int a); // FIXME Also set gender and yearly income
void SetGender(string gender);
void SetIncome(int income);
int GetAge();
string GetGender();
int GetIncome();
private:
int age;
string gender;
int yearlyIncome;
};
// Constructor for the Person class.
Person::Person() {
age = 0;
gender = "default";
yearlyIncome = 0;
return;
}
// Print the Person class.
void Person::Print() {
cout << "Age = " << this->age
<< ", gender = " << this->gender
<< ", yearly income = " << this->yearlyIncome
<< endl;
return;
2. }
// Set the age, gender, and yearlyIncome of a Person.
void Person::SetData(int a) { // FIXME Also set gender and yearly income
this->age = a;
return;
}
void Person::SetGender(string gender){
this->gender = gender;
}
void Person::SetIncome(int income){
this->yearlyIncome = income;
}
// Get the age of a Person.
int Person::GetAge() {
return this->age;
}
string Person::GetGender(){
return this->gender;
}
int Person::GetIncome() {
return this->yearlyIncome;
}
// Get a filename from program arguments, then make a Person for each line in the file.
bool ReadPeopleFromFile(int argc, char* argv[], vector &people) {
Person tmpPrsn;
ifstream inFS;
int tmpAge = 0;
string tmpGender = "";
int tmpYI = 0;
if (argc != 2) {
cout << " Usage: [EXECUTABLE FILE] [TEXT DATA FILE], e.g. myprog.exe
dev_people.txt" << endl;
return true; // indicates error
}
cout << "Opening file " << argv[1] << ". ";
inFS.open(argv[1]); // Try to open file
3. if (!inFS.is_open()) {
cout << "Could not open file " << argv[1] << ". ";
return true; // indicates error
}
while (!inFS.eof()) {
inFS >> tmpAge;
inFS >> tmpGender;
inFS >> tmpYI;
tmpPrsn.SetData(tmpAge); // FIXME Also set gender and yearly income
tmpPrsn.SetGender(tmpGender);
tmpPrsn.SetIncome(tmpYI);
tmpPrsn.Print();
people.push_back(tmpPrsn);
}
inFS.close();
cout << "Finished reading file." << endl;
return false; // indicates no error
}
// Ask user to enter age range.
void GetUserInput(int &ageLowerRange, int &ageUpperRange, string &gender, int
&yILowerIncome,int &yIHigherIncome) {
cout<<" Enter lower range of age: ";
cin >> ageLowerRange;
cout << "Enter upper range of age: ";
cin >> ageUpperRange;
cout << "Enter the gender: ";
cin >> gender;
cout << "Enter the lower range of yearlyIncome: ";
cin >> yILowerIncome;
cout << "Enter the higher range of yearlyIncome: ";
cin >> yIHigherIncome;
return;
}
// Return people within the given age range.
vector GetPeopleInAgeRange(vector ppl, int lowerRange, int upperRange) {
unsigned int i = 0;
4. vector pplInAgeRange;
int age = 0;
for (i = 0; i < ppl.size(); ++i) {
age = ppl.at(i).GetAge();
if ((age >= lowerRange) && (age <= upperRange)) {
pplInAgeRange.push_back(ppl.at(i));
}
}
return pplInAgeRange;
}
// Return people with same Gender.
vector GetPeopleWithSpecificGender(vector ptntlCstmrs,string gender){
vector pplWithSameGender;
string gndr;
for (int i=0;i GetPeopleInIncomeRange(vector ptntlCstmrs,int lowerRange, int higherRange){
vector pplInIncomeRange;
int range = 0;
for (int i=0;i= lowerRange) && (range <= higherRange)){
pplInIncomeRange.push_back(ptntlCstmrs.at(i));
}
}
return pplInIncomeRange;
}
int main(int argc, char* argv[]) {
vector ptntlCstmrs;
bool hadError = false;
int ageLowerRange = 0;
int ageUpperRange = 0;
string gender;
int yILowerIncome = 0;
int yIHigherIncome = 0;
hadError = ReadPeopleFromFile(argc, argv, ptntlCstmrs);
if( hadError ) {
return 1; // indicates error
}
GetUserInput(ageLowerRange, ageUpperRange, gender, yILowerIncome, yIHigherIncome );
5. ptntlCstmrs = GetPeopleInAgeRange(ptntlCstmrs, ageLowerRange, ageUpperRange);
ptntlCstmrs = GetPeopleWithSpecificGender(ptntlCstmrs,gender);
ptntlCstmrs = GetPeopleInIncomeRange(ptntlCstmrs,yILowerIncome,yIHigherIncome);
cout << " Number of potential customers = "<
Solution
HI Friend, I have separated code in seperate files.
Please let me know in case of any issue.
############# Person.h ##########
#include
using namespace std;
class Person {
public:
Person();
void Print();
void SetData(int a); // FIXME Also set gender and yearly income
void SetGender(string gender);
void SetIncome(int income);
int GetAge();
string GetGender();
int GetIncome();
private:
int age;
string gender;
int yearlyIncome;
};
############## Person.cpp #############
#include
#include "Person.h"
using namespace std;
// Constructor for the Person class.
Person::Person() {
age = 0;
gender = "default";
yearlyIncome = 0;
6. return;
}
// Print the Person class.
void Person::Print() {
cout << "Age = " << this->age
<< ", gender = " << this->gender
<< ", yearly income = " << this->yearlyIncome
<< endl;
return;
}
// Set the age, gender, and yearlyIncome of a Person.
void Person::SetData(int a) { // FIXME Also set gender and yearly income
this->age = a;
return;
}
void Person::SetGender(string gender){
this->gender = gender;
}
void Person::SetIncome(int income){
this->yearlyIncome = income;
}
// Get the age of a Person.
int Person::GetAge() {
return this->age;
}
string Person::GetGender(){
return this->gender;
}
int Person::GetIncome() {
return this->yearlyIncome;
}
############## PersonTest.cpp ###########
#include
#include
#include
#include
7. #include "Person.h"
using namespace std;
// Get a filename from program arguments, then make a Person for each line in the file.
bool ReadPeopleFromFile(int argc, char* argv[], vector &people) {
Person tmpPrsn;
ifstream inFS;
int tmpAge = 0;
string tmpGender = "";
int tmpYI = 0;
if (argc != 2) {
cout << " Usage: [EXECUTABLE FILE] [TEXT DATA FILE], e.g. myprog.exe
dev_people.txt" << endl;
return true; // indicates error
}
cout << "Opening file " << argv[1] << ". ";
inFS.open(argv[1]); // Try to open file
if (!inFS.is_open()) {
cout << "Could not open file " << argv[1] << ". ";
return true; // indicates error
}
while (!inFS.eof()) {
inFS >> tmpAge;
inFS >> tmpGender;
inFS >> tmpYI;
tmpPrsn.SetData(tmpAge); // FIXME Also set gender and yearly income
tmpPrsn.SetGender(tmpGender);
tmpPrsn.SetIncome(tmpYI);
tmpPrsn.Print();
people.push_back(tmpPrsn);
}
inFS.close();
cout << "Finished reading file." << endl;
return false; // indicates no error
}
// Ask user to enter age range.
void GetUserInput(int &ageLowerRange, int &ageUpperRange, string &gender, int
8. &yILowerIncome,int &yIHigherIncome) {
cout<<" Enter lower range of age: ";
cin >> ageLowerRange;
cout << "Enter upper range of age: ";
cin >> ageUpperRange;
cout << "Enter the gender: ";
cin >> gender;
cout << "Enter the lower range of yearlyIncome: ";
cin >> yILowerIncome;
cout << "Enter the higher range of yearlyIncome: ";
cin >> yIHigherIncome;
return;
}
// Return people within the given age range.
vector GetPeopleInAgeRange(vector ppl, int lowerRange, int upperRange) {
unsigned int i = 0;
vector pplInAgeRange;
int age = 0;
for (i = 0; i < ppl.size(); ++i) {
age = ppl.at(i).GetAge();
if ((age >= lowerRange) && (age <= upperRange)) {
pplInAgeRange.push_back(ppl.at(i));
}
}
return pplInAgeRange;
}
// Return people with same Gender.
vector GetPeopleWithSpecificGender(vector ptntlCstmrs,string gender){
vector pplWithSameGender;
string gndr;
for (int i=0;i GetPeopleInIncomeRange(vector ptntlCstmrs,int lowerRange, int higherRange){
vector pplInIncomeRange;
int range = 0;
for (int i=0;i= lowerRange) && (range <= higherRange)){
pplInIncomeRange.push_back(ptntlCstmrs.at(i));
}