Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include <iostream> #include <fstream> #include <vector> #include <string> using

ID: 3836654 • Letter: #

Question

#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;
}

Here is the data:

Classes/Streams Marketing software In this p rogramming assignment, you identify the number of potential customers for a business. The starter program outputs the number of potential customers in a u age range serentored given a file with people's data. Move the dass Person to the separate files: person, h and personucpp. Make sure you can stil with separate files. 2 During file reading, the program isntstonng the gender or yearty noome of the people, Instead, the defa values are being printed. F ix the program to correctly set the date read from The regions of the code that need to be ed are marked with: "FIXME Also set gender and yearly income" 3 Allow the user to select the potential customers gender: male". female' ar any. The program should now output potential customers with the user-spected gender and age. Update the GetUserinput tuncton to prompt the user and store the user's gender selection ate a function GetPeopleWithSpecificGender that returns only people with the user-pacitad gender. Debugging suggestion: Use a function to pnnt main's vector of Persons so that you can see who is in the vector after eachfunction cal. This techniqu may help debug the newly created function GetPeopleWithSpecificGender in addition to age and gender, alow the user to select the ower and upper range of a austomer's yearly income, Update the GetUserinput function to prompt the nd store th ser's specified range. Also, create a function GetPeoplelnincomeRange that retums only people with the user specifed yearly income. The ma should now look ike the folowing code: int rain (int argo, char argvti) ector person peopl ool had Error falt nt rRange 8; nt agruppertanee string gender int yILowerRange nt yIUpper Range a had Error ReadPeopl arg people); f( hadE return 1P indicates error GetUser Input(ageLowerRange, ageUpperRange, gender, yILonerRange, yIUpperRanga. eople people ageLowerRange, ageupperRange); eople GetPeopleNithSpecificGender (people, Eende eople a GetPeoplelnIncoreRange(people, yILowerRang yIUpper Range out "MnNumber of potential customers people size return 0 Here is an example program execution with people txt (user input is highlghted here for clarity): opening file people.txt. 20, gender nale, yearly incone 000 2s, gender yearly incone 4S008 e, yearly incone gend 8088 nale, yearly incone 16, gender 33, gend nale, yearly incone 000 gender 27000 yearly inco 26, gender female, yearly inc 44800 yearly income 37000 gender 18, gender 17000 yearly incore Age 29, gender ferale, yearly income 62000 Finished reading file Enter lower range of age: 24 Enter upper range of age: 30 Enter gender (trale, fenale, or any): any inter lower range of yearly incone: 43000 Enter upper range of yearly incare: sveee Number of pote al customers

Explanation / Answer

Completed Code:

Note: Pass the file name as command line argument, the file format should be as specified in the problem.

#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();

void setAge(int age);

void setGender(string gender);

void setIncome( int income);

string getGender();

int getYearlyIncome();

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) {

this->age = a;

return;

}

void Person::setGender(string gen) {  

this->gender = gen;

return;

}

string Person::getGender() {

return this->gender;

}

int Person::getYearlyIncome() {

return this->yearlyIncome;

}

void Person::setIncome(int income) {  

this->yearlyIncome = income;

return;

}

// Get the age of a Person.

int Person::GetAge() {

return this->age;

}

void Person::setAge(int age) {  

this->age = age;

return;

}

// 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;

}

// Return people within the given gender.

vector<Person> GetPeopleInAgeRange(vector<Person> ppl, string gender) {

unsigned int i = 0;

vector<Person> pplWithGender;

string gen;

for (i = 0; i < ppl.size(); ++i) {

gen = ppl.at(i).getGender();

if (gen==gender) {

pplWithGender.push_back(ppl.at(i));

}

}

return pplWithGender;

}

// Return people within the given gender.

vector<Person> GetPeopleInIncomeRange(vector<Person> ppl, int low, int high) {

unsigned int i = 0;

vector<Person> pplWithinsalaryRange;

int salary;

for (i = 0; i < ppl.size(); ++i) {

salary = ppl.at(i).getYearlyIncome();

if (salary>low && salary<high) {

pplWithinsalaryRange.push_back(ppl.at(i));

}

}

return pplWithinsalaryRange;

}

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

{

vector<Person> ptntlCstmrs;

vector<Person> peopleInRange;

vector<Person> peopleWithGender;

vector<Person> peopleWithIncomeRange;

bool hadError = false;

int ageLowerRange = 0;

int ageUpperRange = 0;

hadError = ReadPeopleFromFile(argc, argv, ptntlCstmrs);

if( hadError )

{

/*for visual studio console freeze, uncomment below line*/

//system("pause");

return 1; // indicates error

}

GetUserInput(ageLowerRange, ageUpperRange);

peopleInRange = GetPeopleInAgeRange(ptntlCstmrs, ageLowerRange, ageUpperRange);

//peopleWithGender = GetPeopleWithSpecificGender(ptntlCstmrs, "male");

peopleWithIncomeRange = GetPeopleInIncomeRange(ptntlCstmrs, 10000, 30000);

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

cout << " Number of potential customers with gender male= "<<peopleWithGender.size() << endl;

cout << " Number of potential customers with income range 10000 to 30000= "<<peopleWithIncomeRange.size() << endl;

/*for visual studio console freeze, uncomment below line*/

//system("pause");

return 0;

}