// the code is not deleting the patients when we input the name of the patient #
ID: 674329 • Letter: #
Question
// the code is not deleting the patients when we input the name of the patient
#include <iostream>
#include <string>
using namespace std;
struct Node
{
string Name;
int id;
int age;
char smoker;
char HBP;
char HFD;
int Points;
Node *ptr;
};
Node *startPtr = NULL;
int count;
void add(Node **root)
{
Node *p = new Node;
int agePoints,smkPoints,hbpPoints,hfdPoints;
int count=0;
cout << " Please enter the name of the patient : ";
cin >> p->Name;
cout << " Please enter the ID : ";
cin >> p->id;
cout << " Please enter the age of the patient : ";
cin >> p->age;
cout << " Is he/she a smoker (y/n) : ";
cin >> p->smoker;
cout << " Does he/she have high blood pressure? (y/n) : ";
cin >> p->HBP;
cout << " Does he/she have a high fat diet? (y/n) : ";
cin >> p->HFD;
cout << endl;
p->ptr = NULL;
if (p-> smoker =='Y'||p->smoker == 'y')
count++;
if (p-> age < 18)
{
agePoints = 2;
}
else if (p->age > 18 && p->age <30)
{
agePoints = 3;
}
else
{
agePoints = 4;
}
if (p-> smoker =='Y'||p->smoker == 'y')
{
p->smoker = 'Y';
smkPoints = 4;
}
else
{
p->smoker = 'N';
smkPoints = 0;
}
if (p->HBP == 'Y' || p->HBP == 'y')
{
p->HBP = 'Y';
hbpPoints = 4;
}
else
{
p->HBP = 'N';
hbpPoints = 0;
}
if (p->HFD == 'Y' || p->HFD == 'y')
{
p->HFD = 'Y';
hfdPoints = 1;
}
else
{
p->HFD = 'N';
hfdPoints = 0;
}
p->Points = agePoints + smkPoints + hbpPoints + hfdPoints;
if(*root == NULL)
*root = p;
else
{
Node *temp = *root;
while(temp->ptr != NULL)
{
temp =temp->ptr;
}
temp->ptr = p;
}
}
void displayPatient(Node *p)
{
cout<<"Patient id: "<<p->id<<endl;
cout<<"Patient Name: "<<p->Name<<endl;
cout<<"Age: "<<p->age<<endl;
cout<<"Smoker: "<<p->smoker<<endl;
cout<<"Suffers from High Blood Pressure: "<<p->HBP<<endl;
cout<<"Takes High Fat Diet: "<<p->HFD<<endl;
cout<<"Total points of the patient: "<<p->Points<<endl<<endl;
}
void displayAllPatients(Node *root)
{
Node *temp = root;
while(temp != NULL)
{
displayPatient(temp);
temp = temp->ptr;
}
}
void dispsmoker(Node *root)
{
Node *temp = root;
while(temp != NULL)
{
if(temp->smoker=='Y')
{
displayPatient(temp);
}
temp = temp->ptr;
}
}
void dispHBP(Node *root)
{
Node *temp = root;
while(temp != NULL)
{
if(temp->HBP=='Y')
{
displayPatient(temp);
}
temp = temp->ptr;
}
}
void dispHFD(Node *root)
{
Node *temp = root;
while(temp != NULL)
{
if(temp->HFD=='Y')
{
displayPatient(temp);
}
temp = temp->ptr;
}
}
void deleteNode(Node *root)
{
Node *p;
Node *temp;
string name;
cout<<"Enter the name of the patient to be deleted: ";
cin>>name;
Node *current = startPtr;
Node *prev = NULL;
while(current != NULL && current->Name != name)
{
prev = current;
current = current->ptr;
}
if(current != NULL && current->Name == name)
{
if(prev == NULL)
startPtr = startPtr->ptr;
else
prev->ptr = current->ptr;
}
else
cout << "The patient is not available"<< endl <<endl;
}
void smokers(Node *root)
{
Node *temp = root;
int count = 0;
while(temp != NULL)
{
if(temp->smoker == 'Y')
count++;
temp = temp->ptr;
}
cout<<"Total number of smokers"<<count<<endl;
}
int main()
{
int choice,id;
Node *root = NULL;
while(1)
{
cout<<"Patient Menu ";
cout<<"============================================= ";
cout<<"1. Insert a new record ";
cout<<"2. List of all patients ";
cout<<"3. List smoker patients ";
cout<<"4. List HBP patients ";
cout<<"5. List HFD patients ";
cout<<"6. Delete a patient record by given name ";
cout<<"7. Display total number of smokers ";
cout<<"8. Exit the program ";
cout<<"============================================= ";
cout<<"Enter your selection: ";
cin>>choice;
switch(choice)
{
case 1:
add(&root);
break;
case 2:
cout<<"List of patients: ";
cout<<"Listers"<<endl;
displayAllPatients(root);
break;
case 3:
cout<<"List of smoker patients: ";
cout<<"Listers"<<endl;
dispsmoker(root);
break;
case 4:
cout<<"List of HBP patients: ";
dispHBP(root);
break;
case 5:
cout<<"List of HFD patients: ";
dispHFD(root);
break;
case 6:
deleteNode(root);
break;
case 7:
cout<<"Total number of smokers ";
smokers(root);
break;
case 8:
exit(1);
default: cout<<"Invalid choice.";
}
}
system("pause");
return 0;
}
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
struct Node
{
string Name;
int id;
int age;
char smoker;
char HBP;
char HFD;
int Points;
Node *ptr;
};
Node *startPtr = NULL;
int count;
void add(Node **root)
{
Node *p = new Node;
int agePoints,smkPoints,hbpPoints,hfdPoints;
int count=0;
cout << " Please enter the name of the patient : ";
cin >> p->Name;
cout << " Please enter the ID : ";
cin >> p->id;
cout << " Please enter the age of the patient : ";
cin >> p->age;
cout << " Is he/she a smoker (y/n) : ";
cin >> p->smoker;
cout << " Does he/she have high blood pressure? (y/n) : ";
cin >> p->HBP;
cout << " Does he/she have a high fat diet? (y/n) : ";
cin >> p->HFD;
cout << endl;
p->ptr = NULL;
if (p-> smoker =='Y'||p->smoker == 'y')
count++;
if (p-> age < 18)
{
agePoints = 2;
}
else if (p->age > 18 && p->age <30)
{
agePoints = 3;
}
else
{
agePoints = 4;
}
if (p-> smoker =='Y'||p->smoker == 'y')
{
p->smoker = 'Y';
smkPoints = 4;
}
else
{
p->smoker = 'N';
smkPoints = 0;
}
if (p->HBP == 'Y' || p->HBP == 'y')
{
p->HBP = 'Y';
hbpPoints = 4;
}
else
{
p->HBP = 'N';
hbpPoints = 0;
}
if (p->HFD == 'Y' || p->HFD == 'y')
{
p->HFD = 'Y';
hfdPoints = 1;
}
else
{
p->HFD = 'N';
hfdPoints = 0;
}
p->Points = agePoints + smkPoints + hbpPoints + hfdPoints;
if(*root == NULL)
*root = p;
else
{
Node *temp = *root;
while(temp->ptr != NULL)
{
temp =temp->ptr;
}
temp->ptr = p;
}
}
void displayPatient(Node *p)
{
cout<<"Patient id: "<<p->id<<endl;
cout<<"Patient Name: "<<p->Name<<endl;
cout<<"Age: "<<p->age<<endl;
cout<<"Smoker: "<<p->smoker<<endl;
cout<<"Suffers from High Blood Pressure: "<<p->HBP<<endl;
cout<<"Takes High Fat Diet: "<<p->HFD<<endl;
cout<<"Total points of the patient: "<<p->Points<<endl<<endl;
}
void displayAllPatients(Node *root)
{
Node *temp = root;
while(temp != NULL)
{
displayPatient(temp);
temp = temp->ptr;
}
}
void dispsmoker(Node *root)
{
Node *temp = root;
while(temp != NULL)
{
if(temp->smoker=='Y')
{
displayPatient(temp);
}
temp = temp->ptr;
}
}
void dispHBP(Node *root)
{
Node *temp = root;
while(temp != NULL)
{
if(temp->HBP=='Y')
{
displayPatient(temp);
}
temp = temp->ptr;
}
}
void dispHFD(Node *root)
{
Node *temp = root;
while(temp != NULL)
{
if(temp->HFD=='Y')
{
displayPatient(temp);
}
temp = temp->ptr;
}
}
void deleteNode(Node **root)
{
Node *p;
Node *temp;
string name;
cout<<"Enter the name of the patient to be deleted: ";
cin>>name;
Node *current = *root;
Node *prev = NULL;
while(current != NULL && current->Name != name)
{
prev = current;
current = current->ptr;
}
if(current != NULL && current->Name == name)
{
if(prev == NULL)
{
*root = current->ptr;
}
else
prev->ptr = current->ptr;
delete current;
cout << "Patient deleted" << endl << endl;
}
else
cout << "The patient is not available"<< endl <<endl;
}
void smokers(Node *root)
{
Node *temp = root;
int count = 0;
while(temp != NULL)
{
if(temp->smoker == 'Y')
count++;
temp = temp->ptr;
}
cout<<"Total number of smokers"<<count<<endl;
}
int main()
{
int choice,id;
Node *root = NULL;
while(1)
{
cout<<"Patient Menu ";
cout<<"============================================= ";
cout<<"1. Insert a new record ";
cout<<"2. List of all patients ";
cout<<"3. List smoker patients ";
cout<<"4. List HBP patients ";
cout<<"5. List HFD patients ";
cout<<"6. Delete a patient record by given name ";
cout<<"7. Display total number of smokers ";
cout<<"8. Exit the program ";
cout<<"============================================= ";
cout<<"Enter your selection: ";
cin>>choice;
switch(choice)
{
case 1:
add(&root);
break;
case 2:
cout<<"List of patients: ";
cout<<"Listers"<<endl;
displayAllPatients(root);
break;
case 3:
cout<<"List of smoker patients: ";
cout<<"Listers"<<endl;
dispsmoker(root);
break;
case 4:
cout<<"List of HBP patients: ";
dispHBP(root);
break;
case 5:
cout<<"List of HFD patients: ";
dispHFD(root);
break;
case 6:
deleteNode(&root);
break;
case 7:
cout<<"Total number of smokers ";
smokers(root);
break;
case 8:
exit(1);
default: cout<<"Invalid choice.";
}
}
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.