hw8_fncts.cpp
// File: hw8fncts.cpp
// Purpose: This file contains the definitions of the functions
// for the program that makes a medical diagnosis from
// a user's complaint.
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <string>
#include "hw8.h"
using namespace std;
bool getHealthComplaint(string complaintWords[],
short &numWords,
string &whichBodyPart)
{
bool bodyPartMentioned; // false if complaint doesn't have
// leg, hand, nose, torso, or head
char complaint[MAX_COMPLAINT_WORDS *
(MAX_COMPLAINT_WORD_LENGTH + 1)];
string complaintStr; // complaint cstring as a string
string word; // a single word within the complaint
short j;
cout << "Tell me what your problem is. Specifically, ";
cout << "does it \ninvolve your head, torso, hand, nose, ";
cout << "or leg?\n";
cin.getline(complaint,
(MAX_COMPLAINT_WORDS *
(MAX_COMPLAINT_WORD_LENGTH + 1)) - 1);
// Translate all chars to lowercase
for (int i = 0; i < strlen(complaint); i++)
complaint[i] = tolower(complaint[i]);
// Make a string var so we can use string functions
complaintStr = complaint;
// See if body part mentioned in complaint
j = 0;
bodyPartMentioned = false;
whichBodyPart = "";
while ((!bodyPartMentioned) && (j < NUM_BODY_PARTS))
if (complaintStr.find(BODY_PARTS[j]) != std::string::npos)
{
bodyPartMentioned = true;
whichBodyPart = BODY_PARTS[j];
}
else j++;
// Make an array of words out of the complaint string;
// assume each word separated by a space
numWords = 0;
std::istringstream iss(complaintStr);
while(getline(iss, word, ' ') &&
(numWords < MAX_COMPLAINT_WORDS))
{
complaintWords[numWords] = word;
numWords++;
}
return(bodyPartMentioned);
}
void respondToComplaint(const string complaintWords[],
const short numWords)
{
int wordsToEcho = ((numWords/2) > MAX_WORDS_TO_RESPOND_TO)?
MAX_WORDS_TO_RESPOND_TO : (numWords/2);
cout << endl << "...So,";
for (short i = 1; i <= wordsToEcho; i++)
cout << " " << complaintWords[rand() % numWords];
cout << "? ";
return;
}
void decideDiagnosis(const string whichBodyPart)
{
ifstream fin;
char filename[20];
string diagnosis;
int numEntriesInFile, diagnosisNum, chosenDiagnosisNum;
strcpy(filename, whichBodyPart.c_str());
strcat(filename, ".dat");
fin.clear(); // reset connection
fin.open(filename);
if (!fin)
cout << "Cannot open " << filename << "!\n";
else
{
fin >> numEntriesInFile;
fin.ignore(NUM_CHARS_TO_IGNORE_UNTIL_NEWLINE, '\n');
chosenDiagnosisNum = rand() % numEntriesInFile;
// Read lines in diagnosis file until you reach
// the chosen line #
diagnosisNum = 0;
while (diagnosisNum != chosenDiagnosisNum)
{
getline(fin, diagnosis, '\n');
diagnosisNum++;
}
cout << "It is clear that you have "
<< diagnosis << ".\n";
fin.close();
}
return;
}
void recommendPrescription(const short numWords)
{
ifstream fin;
string prescriptionName;
int numEntriesInFile;
int prescriptionNum[NUM_RX_WORDS]; // random line #'s
// to be chosen from
// the Rx file
fin.clear(); // reset connection
fin.open(PRESCRIPTIONS_FILE);
if (!fin)
cout << "Cannot open " << PRESCRIPTIONS_FILE << "!\n";
else
{
// Figure out # entries in prescription file
numEntriesInFile = 0;
while (fin >> prescriptionName)
numEntriesInFile++;
fin.close();
// Choose random numbers corresponding to entries
// in the file
for (short i = 0; i < NUM_RX_WORDS; i++)
prescriptionNum[i] = (rand() % numEntriesInFile) + 1;
// Re-open the file so we can go read those entries
fin.clear(); // reset connection
fin.open(PRESCRIPTIONS_FILE);
if (!fin)
cout << "Cannot open " << PRESCRIPTIONS_FILE << "!\n";
else
{
cout << "Your prescription is to take "
<< numWords << " pill(s) a day of "
<< RX_DOSAGE << " mg. ";
for (short i = 1; i <= numEntriesInFile; i++)
{
fin >> prescriptionName;
// See if i is one of the randomly chosen lines
for (short j = 0; j < NUM_RX_WORDS; j++)
if (i == prescriptionNum[j])
cout << prescriptionName;
}
cout << endl;
fin.close();
}
}
return;
}
void decideOnSurgery()
{
ifstream fin;
string surgery;
int numEntriesInFile, surgeryNum, chosenSurgeryNum;
if (((rand() % 100) + 1) <= HOW_OFTEN_SURGERY_DONE)
{
fin.clear(); // reset connection
fin.open(SURGERIES_FILE);
if (!fin)
cout << "Cannot open " << SURGERIES_FILE << "!\n";
else
{
// Figure out # entries in file
numEntriesInFile = 0;
while (getline(fin, surgery, '\n'))
numEntriesInFile++;
fin.close();
// Choose random number corresponding to a line #
// in the file
chosenSurgeryNum = rand() % numEntriesInFile;
// Re-open the file so we can go read that entry
fin.clear(); // reset connection
fin.open(SURGERIES_FILE);
if (!fin)
cout << "Cannot open " << SURGERIES_FILE << "!\n";
else
{
// Read lines in surgery file until you reach
// the chosen line #
surgeryNum = 0;
while (surgeryNum != chosenSurgeryNum)
{
getline(fin, surgery, '\n');
surgeryNum++;
}
cout << "I would also recommend that you "
<< "undergo a " << surgery << endl;
fin.close();
}
}
}
return;
}