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

c++ 5. A file called \"people.txt\" contains many thousands of lines of data tha

ID: 3705268 • Letter: C

Question

c++

5. A file called "people.txt" contains many thousands of lines of data that look just like this 19911213 Douglas Davies 70 170NJ 19420117 Cory DeMilt 68 175 NY 19620606 Heironymous McTaggart 72 231 LA 19591106 Ian Williams 74 320 FL 19700620 Dante She11duck 64 168 ?? The data on each line are a person's birth date, their first and last name, their height in inches, their weight in pounds, and the state that they live in I. Write a complete C++ program, even showing the correct #includes, that 2. Create another file containing the data only for people who live on the West 3. Count and print the total number of people in the original file reads this file and performs the following tasks: coast their state w be one of CA, OR, or WA nal file

Explanation / Answer

#include <iostream>

#include <vector>

#include <fstream>

#include <sstream>

#include <iomanip>

using namespace std;

// 199111213 Douglas Davies 70 170 NJ

// 194220117 Cory Demilt 68 175 NY

// 194220117 Wilson Demilt 68 175 CA

// 196220606 Heironymous McTaggart 72 231 LA

// 19700620 Dante ShellDuck 64 168 CT

// 196220606 James McTaggart 72 231 OR

// 19700620 Mack ShellDuck 64 168 WA

class Person{

private:

string dateOfBirth;

string firstName;

string lastName;

int heightInches;

int weightPounds;

string stateLocation;

public:

Person(string dateOfBirth, string firstName, string lastName, int heightInches, int weightPounds, string stateLocation);

string getStateLocation();

friend void writeWestCoastPerson( vector<Person> &Personlist, string targetFilename);

void displayPersonDetails();

friend void displayShortestperson(vector<Person> &list);

};

string Person::getStateLocation(){

return stateLocation;

}

Person::Person(string dateOfBirth, string firstName, string lastName, int heightInches, int weightPounds, string stateLocation){

this->dateOfBirth = dateOfBirth;

this->firstName = firstName;

this->lastName = lastName;

this->heightInches = heightInches;

this->weightPounds = weightPounds;

this->stateLocation =stateLocation;

}

void Person::displayPersonDetails(){

  

cout << "DOB:" << setw(15) <<dateOfBirth << " FirstName: " << setw(15) << firstName << " LastName: "

<< setw(15) << lastName

<< " Height " << setw(15) << heightInches << " Weight: "

<< setw(15) << weightPounds << " State : "<< setw(15) << stateLocation <<endl;

  

  

}

void writeWestCoastPerson( vector<Person> &Personlist, string targetFilename){

string output_file = targetFilename ;

ofstream ofsInputFile(output_file.c_str());

if(ofsInputFile.fail()){

cout << "Error while writing file" << endl;

return;

}

if (ofsInputFile.is_open ()){

vector<Person>::iterator it;

for(it = Personlist.begin(); it != Personlist.end(); it++ ) {

// found nth element..print and break.

if (it->getStateLocation() == "CA" || it->getStateLocation() == "OR" || it->getStateLocation() == "WA"){

ofsInputFile <<it->dateOfBirth<<" "<<it->firstName<<" "<<it->lastName<<" "<<it->heightInches<<" "<<it->weightPounds<<" "<<it->stateLocation<<endl;

}

}

}

  

ofsInputFile.close();

}

void displayShortestperson(vector<Person> &Personlist){

vector<Person>::iterator it;

vector<Person>::iterator itShortestPerson;

itShortestPerson = Personlist.begin();

for(it = Personlist.begin(); it != Personlist.end(); it++ ) {

// found nth element..print and break.

if (itShortestPerson->heightInches > it->heightInches){

itShortestPerson = it;

}

}

  

cout << "shortest Person is " << itShortestPerson->firstName << " "<< itShortestPerson->lastName <<endl;

}

int main(){

vector <Person> list;

//input is Person.txt

string input_file = "Person.txt";

ifstream ifsInputFile(input_file.c_str());

if (ifsInputFile.is_open ()){

string strLine;

while( !ifsInputFile.eof() && getline (ifsInputFile, strLine)){

stringstream ssWordsBuf (strLine);

string strWord;

string dob, firstname, lastname, state;

int height, weight;

ssWordsBuf >> dob >> firstname >> lastname >> height >> weight >> state;

list.push_back(Person(dob, firstname, lastname,height, weight, state));

}

}

ifsInputFile.close();

cout << "listing All the person list" <<endl;

vector<Person>::iterator it;

for(it = list.begin(); it != list.end(); it++ ) {

// found nth element..print and break.

it->displayPersonDetails();

}

string targetFilename = "WestCoastperson.txt";

writeWestCoastPerson( list, targetFilename);

cout << "WestCoastPerson are written to file " << targetFilename <<endl;

cout << "Count of person" << list.size() <<endl;

displayShortestperson(list);

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote