C++ Programming: So I received help with a code that takes a diabetics name, age
ID: 3776366 • Letter: C
Question
C++ Programming:
So I received help with a code that takes a diabetics name, age, food consumed, and the blood sugar (before and after) using linked lists and then copies the information to a file. How would I make this so that the Person class only holds name and age, but then an inherited class Diabetic takes the reigns and has name+age from Person and then adds the food, and blood sugar results? Is that even possible with linked lists? And how would I create an option on whether user would like to copy to a file instead of automatically copying it?
code:
Program code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Record
{
string name;
int age;
string food;
int after;
int before;
Record *next;
}*head;
class People {
public:
int count=0;
Record* create_node(string,int,string,int,int);
void insert();
void display();
People()
{
head = NULL;
}
};
int main()
{
cout << "patient details" << endl << endl;
head = NULL;
People p;
p.insert();
p.insert();
p.display();
return 0;
}
Record *People::create_node(string nam,int a,string foo,int af,int bef)
{
struct Record *newRecord, *s;
newRecord = new Record;
if (newRecord == NULL)
{
cout<<"Memory not allocated "<<endl;
return 0;
}
else
{
newRecord -> name = nam;
newRecord -> age = a;
newRecord -> food = foo;
newRecord -> after = af;
newRecord -> before = bef;
newRecord -> next = NULL;
return newRecord;
}
}
void People::insert()
{
string s_name;
int s_age;
string s_food;
int s_after;
int s_before;
Record *Record_ptr,*newRecord;
ofstream outfile;
outfile.open("Patient.txt");
cout<<"Enter patient details";
cin>>s_name>>s_age>>s_food>>s_after>>s_before;
outfile<<s_name<<s_age<<s_food<<s_after<<s_before;
outfile<<" ";
count++;
newRecord = create_node(s_name,s_age,s_food,s_after,s_before);
if (head == NULL)
{
head = newRecord;
head->next = NULL;
}
else
{
Record_ptr = head;
while(Record_ptr -> next)
Record_ptr = Record_ptr -> next;
Record_ptr -> next = newRecord;
}
outfile.close();
}
void People::display()
{
Record *Display_ptr;
Display_ptr = head;
while(Display_ptr != NULL)
{
cout << Display_ptr -> name << endl;
cout << Display_ptr -> age << endl;
cout << Display_ptr -> food << endl;
cout << Display_ptr -> after << endl;
cout << Display_ptr -> before << endl;
Display_ptr = Display_ptr -> next;
cout << endl;
}
cout<<" Number of patient in file:"<<count<<" ";
}
Explanation / Answer
<code>#include<stdio.h>
#include<conio.h>
#include<fstream.h>
#include<stdlib.h>
#include<string.h>
#include<iomanip.h>
class patient
{
public:
char name[20];
char blood_group[5];
int pat_id;
long int phn;
int a;
void patientdata();
void showdata();
};
void patient::patientdata()
{
cout<<" ******************************************************************************* ";
cout<<" Create New Patient Database ";
cout<<" ******************************************************************************* ";
cout<<" Enter Patient ID : ";
cin>>pat_id;
cout<<" Enter Patient Name : ";
cin>>name;
cout<<" Enter Patient Blood Group : ";
cin>>blood_group;
fflush(stdin);
cout<<" Enter Patient Phone : ";
cin>>phn;
}
void patient::showdata()
{
cout<<" Patient ID : "<<pat_id;
cout<<" Patient Name : "<<name;
cout<<" Blood Group : "<<blood_group;
cout<<" Patient Phone : "<<phn;
cout<<" ";
}
void main()
{
clrscr();
gotoxy(20,18);
cout<<" Program Developed by :-";
gotoxy(28,22);
cout<<" Me ";
gotoxy(25,28);
cout<<"Press Any Key to Continue";
getch();
clrscr();
char s;
patient obj;
z:
fstream f;
f.open("patient",ios::in|ios::out|ios::app|ios::ate|ios::binary);
cout<<" ============================================================================= ";
cout<<" Blood Group Automation ";
cout<<" ============================================================================= ";
cout<<" 1 . Enter Patient Database";
cout<<" 2 . View Patient Database";
cout<<" 3 . Modify Patient Database";
cout<<" 4 . Search Patient Database";
cout<<" 5 . Exit ";
int a;
cout<<" Enter your Choice:";
f.seekg(0);
cin>>a;
char x;
switch (a)
{
case 1:
clrscr();
fstream f;
f.open("patient",ios::in|ios::out|ios::ate|ios::app|ios::binary);
char ans;
obj.patientdata();
f.write((char *)&obj,sizeof(obj));
getch();
cout<<"Do You Want To Continue? y/n :";
ans=getchar();
if(ans=='Y' || ans=='y')
goto z;
else
break;
case 2:
{
cout<<" ";
fstream f;
f.open("patient",ios::in|ios::out|ios::ate|ios::app|ios::binary);
char ans;
f.seekg(0);
int ctr=0;
while(f.read((char *)&obj,sizeof(obj)) )
{
ctr=ctr+1;
if(ctr==8)
{
getchar();
clrscr();
ctr=0;
}
obj.showdata();
if(f.eof()==1)
{
break;
}
}
f.close();
cout<<"Do You Want To Continue? y/n :";
cin>>ans;
if(ans=='y'|| ans=='Y')
goto z;
else
{
exit(1);
}
}
break;
case 3:
{
clrscr();
fstream f;
patient obj;
char name[20];
char blood_group[5];
long int phn;
int pat_id;
f.open("patient",ios::in|ios::binary);
cout<<" ";
cout<<" ****************************************************************************** ";
cout<<" Modify Patient Database ";
cout<<" ****************************************************************************** ";
cout<<" Enter Patient Name :";
cin>>name;
do
{ f.read((char *)&obj,sizeof(obj));
if(f.eof()==1) {break;}
if(strcmp(obj.name,name)==0)
{
cout<<" Patient Name :"<<obj.name;
cout<<" Patient ID : "<<obj.pat_id;
cout<<" Blood Group :"<<obj.blood_group;
cout<<" Patient Phone :" <<obj.phn;
getchar();
cout<<endl;
cout<<endl;
cout<<" Enter New Database" ;
cout<<" Enter Patient name : ";
cin>>name;
cout<<" Enter Patient ID : ";
cin>>pat_id;
cout<<" Blood Group : ";
cin>>blood_group;
cout<<" Enter Patient Phone :";
cin>>phn;
cout<<" ";
strcpy(obj.name,name);
strcpy(obj.blood_group,blood_group);
obj.a=a;
int l=f.tellg();
f.close();
f.open("patient",ios::out|ios::binary|ios::ate);
f.seekg(l-sizeof(obj));
f.write((char *)&obj,sizeof(obj));
}
}while(f);
f.close();
cout<<"Do You Want To Continue?y/n :";
ans=getchar();
if(ans=='Y'||ans=='y')
{goto z;}
else
break;
}
case 4:
{
clrscr();
fstream f;
patient obj;
char name[20];
char blood_group[5];
long int phn;
int pat_id;
f.open("patient",ios::in|ios::binary);
cout<<" ";
cout<<" ****************************************************************************** ";
cout<<" Search Patient Database ";
cout<<" ****************************************************************************** ";
cout<<" Enter Patient Name :";
cin>>name;
do
{ f.read((char *)&obj,sizeof(obj));
if(f.eof()==1) {break;}
if(strcmp(obj.name,name)==0)
{
cout<<" Patient Name :"<<obj.name;
cout<<" Patient ID : "<<obj.pat_id;
cout<<" Blood Group :"<<obj.blood_group;
cout<<" Patient Phone :" <<obj.phn;
getchar();
cout<<endl;
strcpy(obj.name,name);
strcpy(obj.blood_group,blood_group);
obj.a=a;
int l=f.tellg();
f.close();
f.open("patient",ios::out|ios::binary|ios::ate);
f.seekg(l-sizeof(obj));
//f.write((char *)&obj,sizeof(obj));
}
else
{
cout<<" Patient Does Not Exist ";
}
}while(f);
f.close();
cout<<" Do You Want To Continue?y/n :";
ans=getchar();
if(ans=='Y'||ans=='y')
{goto z;}
else
break;
}
case 5:
{
exit(1);
}
default:
{
cout<<" Wrong input ";
}
cout<<" Do You Want To Continue?y/n :";
ans=getchar();
clrscr();
if(ans=='Y'||ans=='y')
{goto z;}
else
break;
}
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.