In this programming assignment, you’ll identify the number of potential customer
ID: 3781521 • Letter: I
Question
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 userentered 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 userspecified 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
userspecified 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 userspecified
yearly income.
The main should now look like the following code:
intmain(intargc,char*argv[]){
vectorpeople;
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="< 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
Heres the code that needs to be modified
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
class Item {
public:
void SetName(string nm)
{ name = nm; };
void SetQuantity(int qnty)
{ quantity = qnty; };
virtual void Print()
{ cout << name << " " << quantity << endl; };
virtual ~Item()
{ return; };
protected:
string name;
int quantity;
};
class Produce : public Item { // Derived from Item class
public:
void SetExpiration(string expir)
{ expiration = expir; };
void Print()
{ cout << name << " x" << quantity
<< " (Expires: " << expiration << ")"
<< endl;
};
private:
string expiration;
};
// Print all items in the inventory
void PrintInventory(vector<Item*> inventory);
// Dialogue to create a new item, then add that item to the inventory
vector<Item*> AddItemToInventory(vector<Item*> inventory);
// Dialogue to update the quantity of an item, then update that item in the inventory
vector<Item*> UpdateItemQtyInInventory(vector<Item*> inventory);
// Dialogue to remove a specific item, then remove that specific item from the inventory
vector<Item*> RemoveItemFromInventory(vector<Item*> inventory);
int main() {
vector<Item*> inventory;
string usrInptOptn = "default";
while (true) {
// Get user choice
cout << " Enter (p)rint, (a)dd, (u)pdate, (r)emove, or (q)uit: ";
getline(cin, usrInptOptn);
// Process user choice
if (usrInptOptn.size() == 0) {
continue;
} else if (usrInptOptn.at(0) == 'p') {
PrintInventory(inventory);
} else if (usrInptOptn.at(0) == 'a') {
inventory = AddItemToInventory(inventory);
} else if (usrInptOptn.at(0) == 'u') {
inventory = UpdateItemQtyInInventory(inventory);
} else if (usrInptOptn.at(0) == 'r') {
inventory = RemoveItemFromInventory(inventory);
} else if (usrInptOptn.at(0) == 'q') {
cout << " Good bye." << endl;
break;
}
}
return 0;
}
void PrintInventory(vector<Item*> inventory) {
unsigned int i = 0;
if (inventory.size() == 0) {
cout << "No items to print." << endl;
} else {
for (i=0; i<inventory.size(); ++i) {
cout << i << " - ";
inventory.at(i)->Print();
}
}
return;
}
vector<Item*> AddItemToInventory(vector<Item*> inventory) {
Produce* prdc;
string usrInptName = "";
string usrInptQntyStr = "";
istringstream inSS;
int usrInptQnty = 0;
string usrInptExpr = "";
cout << "Enter name of new produce: ";
getline(cin, usrInptName);
cout << "Enter quantity: ";
getline(cin, usrInptQntyStr);
inSS.str(usrInptQntyStr);
inSS >> usrInptQnty;
inSS.clear();
cout << "Enter expiration date: ";
getline(cin, usrInptExpr);
prdc = new Produce;
prdc->SetName(usrInptName);
prdc->SetQuantity(usrInptQnty);
prdc->SetExpiration(usrInptExpr);
inventory.push_back(prdc);
return inventory;
}
vector<Item*> UpdateItemQtyInInventory(vector<Item*> inventory) {
string usrIndexChoiceStr = "";
unsigned int usrIndexChoice = 0;
istringstream inSS;
string usrInptQntyStr = "";
int usrInptQnty = 0;
if (inventory.size() == 0) {
cout << "No items to update." << endl;
} else {
PrintInventory(inventory);
do {
cout << "Update which item #: ";
getline(cin, usrIndexChoiceStr);
inSS.str(usrIndexChoiceStr);
inSS >> usrIndexChoice;
inSS.clear();
} while ( !(usrIndexChoice < inventory.size()) );
cout << "Enter new quantity: ";
getline(cin, usrInptQntyStr);
inSS.str(usrInptQntyStr);
inSS >> usrInptQnty;
inSS.clear();
inventory.at(usrIndexChoice)->SetQuantity(usrInptQnty);
}
return inventory;
}
vector<Item*> RemoveItemFromInventory(vector<Item*> inventory) {
istringstream inSS;
string usrIndexChoiceStr = "";
unsigned int usrIndexChoice = 0;
string usrInptQntyStr = "";
if (inventory.size() == 0) {
cout << "No items to remove." << endl;
} else {
PrintInventory(inventory);
do {
cout << "Remove which item #: ";
getline(cin, usrIndexChoiceStr);
inSS.str(usrIndexChoiceStr);
inSS >> usrIndexChoice;
inSS.clear();
} while ( !(usrIndexChoice < inventory.size()) );
inventory.erase( inventory.begin() + usrIndexChoice );
}
return inventory;
}
Explanation / Answer
person.cpp
#include <iostream>
#include <string>
#include "person.h"
using namespace std;
// 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, string g, int i) {
this->age = a;
this->gender = g;
this->yearlyIncome = i;
return;
}
// Get the age of a Person.
int Person::GetAge() {
return this->age;
}
// Get the gender of a Person
string Person::GetGender() {
return this->gender;
}
// Get the Yearly income of a Person
int Person::GetYearlyIncome() {
return this->yearlyIncome;
}
person.h
#ifndef PERSON_H
#define PERSON_H
#include <string>
using namespace std;
// Define a Person class, including age, gender, and yearlyIncome.
class Person {
public:
Person();
void Print();
void SetData(int a, string g, int i);
int GetAge();
string GetGender();
int GetYearlyIncome();
private:
int age;
string gender;
int yearlyIncome;
};
#endif // !PERSON_H
main.cpp
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <iomanip>
#include <istream>
#include <sstream>
#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<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, tmpGender, 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& yILowerRange, int& yIUpperRange) {
cout<<" Enter lower range of age: ";
cin >> ageLowerRange;
cout << "Enter upper range of age: ";
cin >> ageUpperRange;
cout << " Enter gender (male, female or any): ";
cin >> gender;
cout << " Enter lower range of yearly income: ";
cin >> yILowerRange;
cout << "Enter upper range of yearly income: ";
cin >> yIUpperRange;
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 a given gender
vector<Person> GetPeopleWithSpecificGender(vector<Person> ppl, string gender) {
unsigned int i = 0;
if (gender == "any") {
return ppl;
}
vector<Person> pplWithGender;
string g = "any";
for (i = 0; i < ppl.size(); ++i) {
g = ppl.at(i).GetGender();
if (g == gender ) {
pplWithGender.push_back(ppl.at(i));
}
}
return pplWithGender;
}
// Return people within the given Yearly Income range
vector<Person> GetPeopleInIncomeRange(vector<Person> ppl, int yILowerRange, int yIUpperRange) {
unsigned int i = 0;
vector<Person> pplInIncomeRange;
int income = 0;
for (i = 0; i < ppl.size(); ++i) {
income = ppl.at(i).GetYearlyIncome();
if ((income >= yILowerRange) && (income <= yIUpperRange)) {
pplInIncomeRange.push_back(ppl.at(i));
}
}
return pplInIncomeRange;
}
int main(int argc, char* argv[]) {
vector<Person> ptntlCstmrs;
bool hadError = false;
int ageLowerRange = 0;
int ageUpperRange = 0;
string gender = "any";
int yILowerRange = 0;
int yIUpperRange = 0;
hadError = ReadPeopleFromFile(argc, argv, ptntlCstmrs);
if( hadError ) {
return 1; // indicates error
}
GetUserInput(ageLowerRange, ageUpperRange, gender, yILowerRange, yIUpperRange);
ptntlCstmrs = GetPeopleInAgeRange(ptntlCstmrs, ageLowerRange, ageUpperRange);
ptntlCstmrs = GetPeopleWithSpecificGender(ptntlCstmrs, gender);
ptntlCstmrs = GetPeopleInIncomeRange(ptntlCstmrs, yILowerRange, yIUpperRange);
cout << " Number of potential customers = "<<ptntlCstmrs.size() << endl;
return 0;
}
dev_people.txt
20 male 25000
25 male 45000
23 male 30000
16 male 7000
30 male 55000
22 female 27000
26 female 44000
21 female 37000
18 female 17000
29 female 62000
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.