#include #include using namespace std; // upper and lower limits for diagnosis const double CHILD_UPPER = 4800000.0; const double CHILD_LOWER = 4600000.0; const double MALE_UPPER = 6100000.0; const double MALE_LOWER = 4700000.0; const double FEMALE_UPPER = 4200000.0; const double FEMALE_LOWER = 5400000.0; // if we have to make changes all the numbers are right here in one place void diagnoseAdult(string, string, double);// function prototypes only need types void diagnoseLimits(string name, double rbcs, double lower, double upper); // parameter names are optional void printInputData(string name, string gender, double age, double rbcs); int main(){ string inputFilename; cout << "Enter subject's filename" << endl; cin >> inputFilename; // read filename from user fstream in; string name, gender; int age, rbcCountTmp; double rbcCount; in.open(inputFilename.c_str()); // open file if(!in){ // Must ERROR check cout << "Error in opening file: " << inputFilename << endl; cout << "Exiting...." << endl; return -1; } in >> name; // read data in >> gender; in >> age; in >> rbcCountTmp; in.close(); // MUST close file rbcCount = rbcCountTmp * 100000.0; // convert to millions of RBCs per micro litre printInputData(name, gender, age, rbcCount); //let's write what we read, to inform user. It also helps with debugging! if(age >= 18){ diagnoseAdult(name, gender, rbcCount); // diagnose adults } else { diagnoseLimits(name, rbcCount, CHILD_LOWER, CHILD_UPPER); // check child limits } return 0; } /* Check if male or female and call diagnoseLimits with appropriate limits */ void diagnoseAdult(string name, string gender, double rbcs){ if(gender == "male"){ diagnoseLimits(name, rbcs, MALE_LOWER, MALE_UPPER);// check limits for adult male } else { diagnoseLimits(name, rbcs, FEMALE_LOWER, FEMALE_UPPER);// check limits for adult female } } /* Check whether rbcs is within lower and upper limits and print appropriate diagnosis message for name. */ void diagnoseLimits(string name, double rbcs, double lower, double upper){ if (rbcs < lower){ cout << name << " has anemia" << endl; // uh oh, too few } else if (rbcs > upper){ cout << name << " has polycythemia" << endl; // uh oh, too many } else { cout << name << " has a normal RBC count" << endl; // just right! } cout << endl; return; } /* Pretty print subject's information */ void printInputData(string name, string gender, double age, double rbcs){ cout << "---------------------------------------------------------------------------" << endl; cout << "Subject name: " << name << endl; cout << " gender: " << gender << endl; cout << " age: " << age << endl; cout << " has: " << rbcs << " Red Blood Cells per micro litre of blood" << endl << endl; }