Question: IN C++ 7.16 Lab: Patient Class - Array of Patient objects (overload operators) Reuse the Patient class with the following modifications: Overload the stream insertion

IN C++

7.16 Lab: Patient Class - Array of Patient objects (overload operators)

Reuse the Patient class with the following modifications:

  • Overload the stream insertion operator (<<) and use it to display reports and write data to file.
  • Overload the relational operator (<) and use it to sort the array in ascending order by age (insertion sort)

This program will create an array of 100 Patient objects and it will read data from an input file (patient.txt) into this array. It sorts the array in ascending order by age. Then it will display on the screen (upon request) the following:

  • 1. The names of the underweight patients.
  • 2. The names of the overweight patients.
  • 3. The names of the obese patients.
  • 4. The names of the patients with a normal weight status

Finally, it writes to another file (patientReport.txt) a table as shown below:

Weight Status Report Victor Smith 19 years 71.00 inches 150 pounds Normal Mary Johnson 22 years 67.00 inches 135 pounds Normal . . . ==================== === ====== ====== ============= Number of patients: 5 

Assume that a name has at most 20 characters (for formatting). Write several small functions (stand-alone functions). Each function should solve a specific part of the problem.

On each line in the input file there are four items: age, height, weight, and name, as shown below:

25 66 120 Jane North 64 72 251 Tim South 

Prompt the user to enter the name of the input file. Generate the name of the output file by adding the word "Report" to the input file's name.

  • If the input file name is patient.txt, the output file name will be patientReport.txt
  • If the input file name is newPatients.txt, the output file name will be newPatientsReport.txt

Display the output file's name as shown below:

Report saved in: patientReport.txt 

If the user enters the incorrect name for the input file, display the following message and terminate the program:

Input file: patients.txt not found! 

Example:

Please enter the input file's name: patient.txt Display "Underweight"[Y/N]? y Showing patients with the "Underweight" status: Paul West 31 years 71.00 inches 122 pounds Underweight Laura King 42 years 67.00 inches 115 pounds Underweight Display "Overweight"[Y/N]? n Display "Obese"[Y/N]? n Display "Normal"[Y/N]? kfd gjs hgj Report saved in: patientReport.txt 

Note: Anything different from 'y' or 'Y' is considered "No".

int main():

/* CIS 22B: Create and process an array of Patient objects Name: IDE: */

#include "Patient.h" #include #include #include #include using namespace std;

const int MAX_SIZE = 100;

/* Write your code here: declare the function you are going to call in this program */

int main() { Patient patArr[MAX_SIZE]; int size = 0;

string fileName; cout << "Please enter the input file's name: "; cin >> fileName; cout << endl; /* Write your code here: function calls */ return 0; }

/* Write your code here: function definitions */

/* OUTPUT:

*/

Patient.h:

/* Specification file for the Patient class - Overload the stream insertion operator (<<) - Overload the relational operator (<) */

#ifndef PATIENT_H #define PATIENT_H #include

using std:: string;

class Patient { private: string name; double height; int weight; int age; public: // constructors Patient(); //Default constructor Patient(string nm, int yrs, double hgt, int wgt); //Overloaded constructor // setters void setName(string nm) { name = nm; } void setHeight(double hgt){ height = hgt; } void setWeight(int wgt){ weight = wgt; } void setAge(int yrs) { age = yrs; } //getters string getName() { return name; } double getHeight() { return height; } int getWeight() { return weight; } int getAge() { return age; } // other functions: declare display and weightStatus void display(); string weightStatus(); //overloaded operators bool operator < (const Patient &); //friends friend ostream &operator << (ostream &, Patient &); };

#endif

Patient.cpp:

/* Implementation file for the Patient class. */

#include "Patient.h" #include #include #include using namespace std;

/******* This is the default constructor; it sets everything to 0 or "". */ Patient::Patient() { name = ""; age = 0; weight = 0; height = 0.0; }

/******* This is an overloaded constructor. It sets the variables according to the parameters. */ /******* This is an overloaded constructor. It sets the variables according to the parameters. */ Patient::Patient( string nm, int yrs, double hgt, int wgt) { name = nm; height = hgt; weight = wgt; age = yrs; }

/******* This function displays the member variables in a neat format. */ // NOTE: Use this function for testing your code // Use the same format for overloading the stream insertion operator // When done, you will not need this function anymore but you could // keep it in your program void Patient::display() const { cout << setprecision(2) << fixed; cout << left << setw(20) << name << " " << right << setw(3) << age << " years " << setw(5) << height << " inches " << setw(3) << weight << " pounds " << weightStatus() << endl; }

/******* This function calculates the BMI using the following formula: BMI = (weight in pounds * 703) / (height in inches)^2 Then, it returns a string reflecting the weight status according to the BMI: <18.5 = underweight 18.5 - 24.9 = normal 25 - 29.9 = overweight >=30 = obese */ string Patient::weightStatus() { double bmi; string stat = ""; if (height > 0) { bmi = (weight * 703) / (height * height); } if(bmi < 18.5){ stat = "Underweight"; } else if(bmi > 18.4 && bmi < 25.0) { stat = "Normal"; } else if(bmi > 24.9 && bmi < 30) { stat = "Overweight"; } else if( bmi >= 30 ) {stat = "Obese"; } else{ stat = ""; } return stat; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!