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

I can not get the search function to search patients by name, just finds nothing

ID: 3770267 • Letter: I

Question

I can not get the search function to search patients by name, just finds nothing. name is the combination of fisrt and last name.

souce code:

#include <iostream>
#include <string>
#include "Header.h"
using namespace std;

const float dayRate = 500;
void addSurgery(PatientAccount[], int);
void addMed(PatientAccount[], int);
void surgeryTypes();
void medicationTypes();
void initialInput(PatientAccount[]);
void menu(PatientAccount[], int);
void viewMenu();
void printDatabase(PatientAccount[], int);
void sort(PatientAccount[], int);
void sortSearch(PatientAccount[], int);

int main()
{
   PatientAccount patients[50];
   int numPat, numSurg, check;
   string first, second;
   int flag = 0;
   cout << "How many patients need to be inputted?" << endl;
   cin >> numPat;
   for (int i = 0; i < numPat; i++)
   {
       patients[i].days = 0;
       patients[i].totalCharges = 0;
       cout << "Please enter first name for patient " << i + 1 << endl;
       cin >> first;
       cout << "Please enter second name for patient " << i + 1 << endl;
       cin >> second;
       patients[i].name = first + " " + second;
       cout << "Please enter number of days in hospital for patient " << i + 1 << endl;
       cin >> patients[i].days;
       patients[i].totalCharges = patients[i].days * 500;
       cout << endl;
       addSurgery(patients, i);
       addMed(patients, i);
   }
   menu(patients, numPat);
   printDatabase(patients, numPat);
   return 0;
}

void addMed(PatientAccount patient[], int pos)
{
   Med meds;
   medicationTypes();
   int flag = 0;
   int numMed;
   while (flag == 0)
   {
       cout << "Please enter 1, 2, 3, 4, or 5 to record a medication" << endl;
       cout << "Enter -1 to exit or 0 to reprint medication menu" << endl;
       cin >> numMed;
       if (numMed == 1)
       {
           meds.antibiotics(patient, pos);
       }
       else if (numMed == 2)
       {
           meds.antinausea(patient, pos);
       }
       else if (numMed == 3)
       {
           meds.antiinflam(patient, pos);
       }
       else if (numMed == 4)
       {
           meds.lightPain(patient, pos);
       }
       else if (numMed == 5)
       {
           meds.strongPain(patient, pos);
       }
       else if (numMed == -1)
       {
           flag--;
       }
       else if (numMed == 0)
       {
           medicationTypes();
       }
       else
       {
           cout << "Please enter a valid medication number" << endl;
       }
   }
}

void addSurgery(PatientAccount patient[], int pos)
{
   Surgery surgery;
   surgeryTypes();
   int flag = 0;
   int numSurg;
   while (flag == 0)
   {
       cout << "Please enter 1, 2, 3, 4, or 5 to record a surgery" << endl;
       cout << "Enter -1 to exit or 0 to reprint surgery menu" << endl;
       cin >> numSurg;
       if (numSurg == 1)
       {
           surgery.tonsillectomy(patient, pos);
       }
       else if (numSurg == 2)
       {
           surgery.foot(patient, pos);
       }
       else if (numSurg == 3)
       {
           surgery.knee(patient, pos);
       }
       else if (numSurg == 4)
       {
           surgery.shoulder(patient, pos);
       }
       else if (numSurg == 5)
       {
           surgery.appendectomy(patient, pos);
       }
       else if (numSurg == -1)
       {
           flag--;
       }
       else if (numSurg == 0)
       {
           surgeryTypes();
       }
       else
       {
           cout << "Please enter a valid surgery number" << endl;
       }
   }
}

void surgeryTypes()
{
   cout << endl;
   cout << "Surgery Types and Rates (1-5)" << endl;
   cout << "1 - Tonsillectomy: $100.00" << endl;
   cout << "2 - Foot: $200.00" << endl;
   cout << "3 - Knee: $300.00" << endl;
   cout << "4 - Shoulder: $400.00" << endl;
   cout << "5 - Appendectomy: $500.00" << endl << endl;
}


void medicationTypes()
{
   cout << endl;
   cout << "Medication Types and Rates (1-5)" << endl;
   cout << "1 - Antibiotics: $10.00" << endl;
   cout << "2 - Anti-nausea: $20.00" << endl;
   cout << "3 - Anti-inflammatory: $30.00" << endl;
   cout << "4 - Light Pain: $40.00" << endl;
   cout << "5 - Strong Pain: $50.00" << endl << endl;
}

void initalInput(PatientAccount patient[])
{
   surgeryTypes();
   medicationTypes();
   patient[0].days = 5;
}

void viewMenu()
{
   cout << endl;
   cout << "______________________________________________________________" << endl;
   cout << "Patient Information Menu" << endl;
   cout << "______________________________________________________________" << endl;
   cout << "V - View Patient Fee Information Database" << endl;
   cout << "F - Find Patient Fee Information by Total Charge" << endl;
   cout << "S - Sort Patient Fee Information" << endl;
   cout << "A - Add Patient Fee" << endl;
   cout << "Q - Quit the Program" << endl;
   cout << "______________________________________________________________" << endl << endl;
}
void sortSearch(PatientAccount patients[], int num) // function used to get input from user to search name
{
   string name;
   bool loop = false;
   cout << "Please input the Patients name that you want to search (-1 for exit)" << endl;
   cin >> name;
   while (name != "-1"){
       for (int i = 0; i < num; i++){
           if (name == patients[i].name){
               cout << "Patient " << name << " owes $" << patients[i].totalCharges << "." << endl;
               cout << "Please input the Patients name that you want to search (-1 for exit)" << endl;
               cin >> name;
               loop = true;
           }
       }
       if (loop == false){
           cout << "The name you have input was not found" << endl;
           cout << "Please input the Patients name that you want to search (Enter -1 to return to the menu)" << endl;
           cin >> name;
       }
   }
   cout << "Returning to the menu!" << endl;
}

void sort(PatientAccount patients[], int elem)// function used to get input from user to sort total charges by Accending or Decending orded
{
   char choice;
   cout << "Please input sorting choices: A: ascending and D: decending: " << endl;
   cin >> choice;
   int seek;
   int minCount;
   int tempScore = elem + 1;
   int minValue;
   std::string minValue2;
   if (choice == 'A' || choice == 'a')
   {
       for (seek = 0; seek < (elem); seek++)
       {
           minCount = seek;
           minValue = patients[seek].totalCharges;
           minValue2 = patients[seek].name;
           for (int index = seek + 1; index < elem; index++)
           {
               if (patients[index].totalCharges > minValue)
               {
                   minValue = patients[index].totalCharges;
                   minValue2 = patients[index].name;
                   minCount = index;
               }
           }
           patients[minCount].name = patients[seek].name;
           patients[minCount].totalCharges = patients[seek].totalCharges;
           patients[seek].name = minValue2;
           patients[seek].totalCharges = minValue;
           cout << patients[seek].name << " " << patients[seek].totalCharges << endl;
       }
   }
   else if (choice == 'D' || choice == 'd')
   {
       for (seek = 0; seek < (elem); seek++)
       {
           minCount = seek;
           minValue = patients[seek].totalCharges;
           minValue2 = patients[seek].name;
           for (int index = seek + 1; index < elem; index++)
           {
               if (patients[index].totalCharges < minValue)
               {
                   minValue = patients[index].totalCharges;
                   minValue2 = patients[index].name;
                   minCount = index;
               }
           }
           patients[minCount].name = patients[seek].name;
           patients[minCount].totalCharges = patients[seek].totalCharges;
           patients[seek].name = minValue2;
           patients[seek].totalCharges = minValue;
           cout << patients[seek].name << " " << patients[seek].totalCharges << endl;
       }
   }
}

void menu(PatientAccount patients[], int numPat)
{
   char selection;
   int flag = 0;
   while (flag == 0)
   {
       viewMenu();
       cout << "Please enter selection" << endl;
       cin >> selection;
       if (toupper(selection) == 'V')
       {
           printDatabase(patients, numPat);
       }
       if (toupper(selection) == 'F')
       {
           //search database by name and output results
           sortSearch(patients, numPat);

       }
       if (toupper(selection) == 'S')
       {
           //sort database (allow them to enter if they want ascending or descending)
           sort(patients, numPat);

       }
       if (toupper(selection) == 'A')
       {
           //allow user to add days in hospital, medications, and/or surgeries
       }
       if (toupper(selection) == 'Q')
       {
           cout << "You have exited the program" << endl;
           flag++;
       }
       else
       {
           cout << "Please enter a proper menu input" << endl;
       }
   }
}

// Prints the Surgeries and Medicines Menus
void printDatabase(PatientAccount patients[], int num)
{
   cout << endl;
   cout << "==============================================================" << endl;
   cout << "Patient Database" << endl;
   cout << "==============================================================" << endl;
   for (int i = 0; i < num; i++)
   {
       cout << patients[i].name << " " << endl;
       cout << patients[i].days << " days spent in hospital--$" << patients[i].days * 500 << endl;
       cout << "==============================================================" << endl;
       cout << "Surgeries" << endl;
       cout << "==============================================================" << endl;
       if (patients[i].numTonsillectomy>0)
       {
           cout << "Tonsillectomy: " << patients[i].numTonsillectomy << "--$" << patients[i].numTonsillectomy * 100 << endl;
       }
       if (patients[i].numFoot>0)
       {
           cout << "Foot Surgery: " << patients[i].numFoot << "--$" << patients[i].numFoot * 200 << endl;
       }
       if (patients[i].numKnee>0)
       {
           cout << "Knee Surgery: " << patients[i].numKnee << "--$" << patients[i].numKnee * 300 << endl;
       }
       if (patients[i].numShoulder>0)
       {
           cout << "Shoulder Surgery: " << patients[i].numShoulder << "--$" << patients[i].numShoulder * 400 << endl;
       }
       if (patients[i].numAppendectomy>0)
       {
           cout << "Appendectormy: " << patients[i].numAppendectomy << "--$" << patients[i].numAppendectomy * 500 << endl;
       }
       cout << endl;
       cout << "==============================================================" << endl;
       cout << "Medications" << endl;
       cout << "==============================================================" << endl;
       if (patients[i].numAntibiotics>0)
       {
           cout << "Antibiotics: " << patients[i].numAntibiotics << "--$" << patients[i].numAntibiotics * 10 << endl;
       }
       if (patients[i].numAntinausea>0)
       {
           cout << "Anti-Nausea: " << patients[i].numAntinausea << "--$" << patients[i].numAntinausea * 20 << endl;
       }
       if (patients[i].numAntiinflam>0)
       {
           cout << "Anti-Inflammatory: " << patients[i].numAntiinflam << "--$" << patients[i].numAntiinflam * 30 << endl;
       }
       if (patients[i].numLightPain>0)
       {
           cout << "Light Pain: " << patients[i].numLightPain << "--$" << patients[i].numLightPain * 40 << endl;
       }
       if (patients[i].numStrongPain>0)
       {
           cout << "Strong Pain: " << patients[i].numStrongPain << "--$" << patients[i].numStrongPain * 50 << endl;
       }
       cout << "==============================================================" << endl;
       cout << "Total Charges: " << patients[i].totalCharges << endl << endl;
   }
}

Header.h file

#include <iostream>
using namespace std;
#ifndef header
#define header
//#include "SurgeryHead.h"

class PatientAccount
{
public:
   string name;
   int days;
   float totalCharges;
   int numTonsillectomy;
   int numFoot;
   int numKnee;
   int numShoulder;
   int numAppendectomy;
   int numAntibiotics;
   int numAntinausea;
   int numAntiinflam;
   int numLightPain;
   int numStrongPain;
   PatientAccount();
   void addDays(int);
   void addCharge(int);
   //other classes have access to this
};

PatientAccount::PatientAccount()
{
   numTonsillectomy = 0;
   numFoot = 0;
   numKnee = 0;
   numShoulder = 0;
   numAppendectomy = 0;
   numAntibiotics = 0;
   numAntinausea = 0;
   numAntiinflam = 0;
   numLightPain = 0;
   numStrongPain = 0;
}

void PatientAccount::addCharge(int type)
{
   if (type == 1)
   {

   }
}

class Surgery
{
public:
   void tonsillectomy(PatientAccount[], int);
   void foot(PatientAccount[], int);
   void knee(PatientAccount[], int);
   void shoulder(PatientAccount[], int);
   void appendectomy(PatientAccount[], int);
};

void Surgery::tonsillectomy(PatientAccount account[], int pos)
{
   account[pos].totalCharges = account[pos].totalCharges + 100;
   account[pos].numTonsillectomy++;
}

void Surgery::foot(PatientAccount account[], int pos)
{
   account[pos].totalCharges = account[pos].totalCharges + 200;
   account[pos].numFoot++;
}

void Surgery::knee(PatientAccount account[], int pos)
{
   account[pos].totalCharges = account[pos].totalCharges + 300;
   account[pos].numKnee++;
}

void Surgery::shoulder(PatientAccount account[], int pos)
{
   account[pos].totalCharges = account[pos].totalCharges + 400;
   account[pos].numShoulder++;
}

void Surgery::appendectomy(PatientAccount account[], int pos)
{
   account[pos].totalCharges = account[pos].totalCharges + 500;
   account[pos].numAppendectomy++;
}

class Med
{
public:
   void antibiotics(PatientAccount[], int);
   void antinausea(PatientAccount[], int);
   void antiinflam(PatientAccount[], int);
   void lightPain(PatientAccount[], int);
   void strongPain(PatientAccount[], int);
};

void Med::antibiotics(PatientAccount account[], int pos)
{
   account[pos].totalCharges = account[pos].totalCharges + 10;
   account[pos].numAntibiotics++;
}

void Med::antinausea(PatientAccount account[], int pos)
{
   account[pos].totalCharges = account[pos].totalCharges + 20;
   account[pos].numAntinausea++;
}

void Med::antiinflam(PatientAccount account[], int pos)
{
   account[pos].totalCharges = account[pos].totalCharges + 30;
   account[pos].numAntiinflam++;
}

void Med::lightPain(PatientAccount account[], int pos)
{
   account[pos].totalCharges = account[pos].totalCharges + 40;
   account[pos].numLightPain++;
}

void Med::strongPain(PatientAccount account[], int pos)
{
   account[pos].totalCharges = account[pos].totalCharges + 50;
   account[pos].numStrongPain++;
}
#endif

Explanation / Answer

/*******************************************************************************************

Hello, first let me congratulate by your code;

the trouble is due to the string to find the name just take the first name;

if you put jacobo cordova, the variable name just get jacobo;

to get the string with spaces we uses the function getline (cin, string);

i just modified the void sortSearch(PatientAccount patients[], int num);

*****************************************************************************************/

void sortSearch(PatientAccount patients[], int num) // function used to get input from user to search name
{
string name;
cout << "Please input the Patients name that you want to search (-1 for exit)" << endl;
cin.ignore( 1024, ' ' ) ; //clean the buffer
   getline(cin, name);   //capture the name with spaces
while (name != "-1"){
   bool loop = false;
for (int i = 0; i < num; i++){
   if (name == patients[i].name){
cout << "Patient " << name << " owes $" << patients[i].totalCharges << "." << endl;
               loop = true;
}
}
if (loop == false){
cout << "The name you have input was not found" << endl;
}
cout << "Please input the Patients name that you want to search (-1 for exit)" << endl;
getline(cin, name, ' ');
}
cout << "Returning to the menu!" << endl;
}

/*************************

if you have any question about it please leave me a comment

************************/

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