hw7_fncts.cpp

/* Programmer: Jennifer Leopold

Date: March 4, 2016

File: hw7_functs.cpp

Purpose: Function definitions used in program that simulates

a healthcare clinic where patients come in with a

complaint, and are prescribed a remedy and charged

for the visit.

*/

#include <iostream>

#include "hw7_functs.h"

using namespace std;

char getYNInput(const string prompt)

{

char input;

bool invalidInput;

do

{

cout << prompt;

cin >> input;

input = toupper(input);

invalidInput = (input != 'Y') && (input != 'N');

if (invalidInput)

cout << "Sweetie, you must enter 'Y' or 'N'!\n";

} while (invalidInput);

return(input);

}

string choosePatientName()

{

int index = rand() % NUM_NAMES;

return(PATIENT_NAMES[index]);

}

string chooseAilment()

{

int index = rand() % NUM_AILMENTS;

return(AILMENTS[index]);

}

string chooseRemedy()

{

int index = rand() % NUM_REMEDIES;

return(REMEDIES[index]);

}

int chooseBillAmount()

{

int amount = (rand() % (MAX_BILL_AMOUNT-MIN_BILL_AMOUNT+1)) +

MIN_BILL_AMOUNT;

return(amount);

}

patient makeRandomPatient()

{

patient p;

p.name = choosePatientName();

p.ailment = chooseAilment();

p.remedy = chooseRemedy();

p.bill = chooseBillAmount();

return(p);

}

void printPatient(const patient p)

{

cout << "Patient " << p.name

<< " is here complaining of " << p.ailment << ".\n";

cout << "Cletus suggests using some "

<< p.remedy << " for that. That will be $"

<< p.bill << ".\n";

return;

}

void sortPatientArray(patient p[], const int numPatients)

{

int positionOfMax;

patient temp, maxValue;

for (int i = 0; i < numPatients-1; i++)

{

// Assume max from position i to end of array

// is the patient at position i

maxValue = p[i];

positionOfMax = i;

// See if you can find a patient with a lexigraphically

// larger name from position i+1 to the end of the array

for (int j = i+1; j < numPatients; j++)

{

if (p[j].name > maxValue.name)

{

maxValue = p[j];

positionOfMax = j;

}

}

// Swap the patient at positionOfMax with the

// patient at position i

temp = p[i];

p[i] = maxValue;

p[positionOfMax] = temp;

}

return;

}

void printPatientArray(const patient p[],

const int numPatients)

{

cout << "\nList of patients seen:\n";

cout << std::left

<< std::setw(NAME_LENGTH)

<< "Patient"

<< std::setw(AILMENT_LENGTH) << "Complaint"

<< std::setw(REMEDY_LENGTH) << "Remedy"

<< "Charge\n";

cout << "----------------------------------------------"

<< "--------------\n";

for (int i = 0; i < numPatients; i++)

{

cout << std::left

<< std::setw(NAME_LENGTH)

<< p[i].name

<< std::setw(AILMENT_LENGTH) << p[i].ailment

<< std::setw(REMEDY_LENGTH) << p[i].remedy

<< "$" << p[i].bill << endl;

}

return;

}