In this programming assignment youll identify the number of

In this programming assignment, you’ll identify the number of potential customers for a business. The starter program outputs the number of potential customers in a user­entered age range given a file with people’s data.

1. Move the class Person to the separate files: person.h and person.cpp. Make sure you can still compile with separate files.

2. During file reading, the program isn’t storing the gender or yearly income of the people. Instead, the default values are being printed. Fix the program to correctly set the data read from file.

The regions of the code that need to be fixed are marked with: “FIXME Also set gender and yearly income”

3. Allow the user to select the potential customer’s gender: “male”, “female”, or “any”. The program should now output only potential customers with the user­specified gender and age.

Update the GetUserInput function to prompt the user and store the user’s gender selection.

Also, create a function GetPeopleWithSpecificGender that returns only people with the user­specified gender.

Debugging suggestion: Use a function to print main’s vector of Persons so that you can see who is in the vector after each function call. This technique may help debug the newly created function GetPeopleWithSpecificGender.

4. In addition to age and gender, allow the user to select the lower and upper range of a customer’s yearly income.

Update the GetUserInput function to prompt the user and store the user’s specified range.
Also, create a function GetPeopleInIncomeRange that returns only people with the user­specified yearly income.

The main should now look like the following code:

intmain(intargc,char*argv[]){

vector<Person>people;

boolhadError=false;

intageLowerRange=0;

intageUpperRange=0;

stringgender=\"\";

intyILowerRange=0;

intyIUpperRange=0;

hadError=ReadPeopleFromFile(argc,argv,people);

if(hadError){

return1;//indicateserror

}

GetUserInput(ageLowerRange,ageUpperRange,gender,yILowerRange, yIUpperRange);

people=GetPeopleInAgeRange(people,ageLowerRange,ageUpperRange); people=GetPeopleWithSpecificGender(people,gender); people=GetPeopleInIncomeRange(people,yILowerRange,yIUpperRange);

cout<<\"\ Numberofpotentialcustomers=\"<<people.size()<<endl;

return0; }

Here is an example program execution with people.txt (user input is highlighted here for clarity):

Openingfilepeople.txt. Age=20,gender=male,yearlyincome=25000 Age=25,gender=male,yearlyincome=45000 Age=23,gender=male,yearlyincome=30000 Age=16,gender=male,yearlyincome=7000 Age=30,gender=male,yearlyincome=55000 Age=22,gender=female,yearlyincome=27000 Age=26,gender=female,yearlyincome=44000 Age=21,gender=female,yearlyincome=37000 Age=18,gender=female,yearlyincome=17000 Age=29,gender=female,yearlyincome=62000

Finishedreadingfile.

Enterlowerrangeofage:24

Enterupperrangeofage:30

Entergender(male,female,orany):any

Enterlowerrangeofyearlyincome:43000

Enterupperrangeofyearlyincome:57000

Numberofpotentialcustomers=3

This is the starter code for the assignment...

#include <iostream>

#include <fstream>

#include <vector>

#include <string>

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

int GetAge();

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;

}

// Get the age of a Person.

int Person::GetAge() {

return this->age;

}

// Get a filename from program arguments, then make a Person for each line in the file.

bool ReadPeopleFromFile(int argc, char* argv[], vector<Person> &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.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) {

cout<<\"\ Enter lower range of age: \";

cin >> ageLowerRange;

  

cout << \"Enter upper range of age: \";

cin >> ageUpperRange;

  

return;

}

// Return people within the given age range.

vector<Person> GetPeopleInAgeRange(vector<Person> ppl, int lowerRange, int upperRange) {

unsigned int i = 0;

  

vector<Person> 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;

}

int main(int argc, char* argv[]) {

vector<Person> ptntlCstmrs;

bool hadError = false;

int ageLowerRange = 0;

int ageUpperRange = 0;

  

hadError = ReadPeopleFromFile(argc, argv, ptntlCstmrs);

if( hadError ) {

return 1; // indicates error

}

  

GetUserInput(ageLowerRange, ageUpperRange);

ptntlCstmrs = GetPeopleInAgeRange(ptntlCstmrs, ageLowerRange, ageUpperRange);

  

// FIXME Add the function GetPeopleWithSpecificGender

//FIXME Addthefunction GetPeopleInIncomeRange

  

cout << \"\ Number of potential customers = \"<<ptntlCstmrs.size() << endl;

  

return 0;

}

---this is the data input that suppose to be used to complete the programming assignment

Solution

CODE:

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
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<Person> &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<Person> GetPeopleInAgeRange(vector<Person> ppl, int lowerRange, int upperRange) {
unsigned int i = 0;

vector<Person> 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<Person> GetPeopleWithSpecificGender(vector<Person> ptntlCstmrs,string gender){
vector<Person> pplWithSameGender;
string gndr;
for (int i=0;i<ptntlCstmrs.size();i++){
gndr = ptntlCstmrs.at(i).GetGender();
if (gndr.compare(gender) == 0){
pplWithSameGender.push_back(ptntlCstmrs.at(i));
}
}
return pplWithSameGender;
}

// Return people within the given income range.
vector<Person> GetPeopleInIncomeRange(vector<Person> ptntlCstmrs,int lowerRange, int higherRange){
vector<Person> pplInIncomeRange;
int range = 0;
for (int i=0;i<ptntlCstmrs.size();i++){
range = ptntlCstmrs.at(i).GetIncome();
cout << range << endl;
if ((range >= lowerRange) && (range <= higherRange)){
pplInIncomeRange.push_back(ptntlCstmrs.at(i));
}
}
return pplInIncomeRange;
}

int main(int argc, char* argv[]) {
vector<Person> 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 );
ptntlCstmrs = GetPeopleInAgeRange(ptntlCstmrs, ageLowerRange, ageUpperRange);
ptntlCstmrs = GetPeopleWithSpecificGender(ptntlCstmrs,gender);
ptntlCstmrs = GetPeopleInIncomeRange(ptntlCstmrs,yILowerIncome,yIHigherIncome);
cout << \"\ Number of potential customers = \"<<ptntlCstmrs.size() << endl;

return 0;
}

OUTPUT:

./a.out in.txt
Opening file in.txt.
Age = 20, gender = male, yearly income = 25000
Age = 25, gender = male, yearly income = 45000
Age = 23, gender = male, yearly income = 30000
Age = 16, gender = male, yearly income = 7000
Age = 30, gender = male, yearly income = 55000
Age = 22, gender = female, yearly income = 27000
Age = 26, gender = female, yearly income = 44000
Age = 21, gender = female, yearly income = 37000
Age = 18, gender = female, yearly income = 17000
Age = 29, gender = female, yearly income = 62000
Age = 29, gender = female, yearly income = 62000
Finished reading file.

Enter lower range of age: 16
Enter upper range of age: 30
Enter the gender: male
Enter the lower range of yearlyIncome: 8000
Enter the higher range of yearlyIncome: 20000

Number of potential customers = 0

In this programming assignment, you’ll identify the number of potential customers for a business. The starter program outputs the number of potential customers
In this programming assignment, you’ll identify the number of potential customers for a business. The starter program outputs the number of potential customers
In this programming assignment, you’ll identify the number of potential customers for a business. The starter program outputs the number of potential customers
In this programming assignment, you’ll identify the number of potential customers for a business. The starter program outputs the number of potential customers
In this programming assignment, you’ll identify the number of potential customers for a business. The starter program outputs the number of potential customers
In this programming assignment, you’ll identify the number of potential customers for a business. The starter program outputs the number of potential customers
In this programming assignment, you’ll identify the number of potential customers for a business. The starter program outputs the number of potential customers
In this programming assignment, you’ll identify the number of potential customers for a business. The starter program outputs the number of potential customers
In this programming assignment, you’ll identify the number of potential customers for a business. The starter program outputs the number of potential customers

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site