Here is the hole code:
Code:
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string.h>
#include <fstream>
using namespace std;
class Patient
{
public:
static int count;
int m_ID;
int m_DOB;
char m_PreMed[128];
char m_Address[128];
char m_Name[128];
Patient* pNext;
};
Patient* pHead=0;
int Patient::count=0;
// add function is outside the class(not member function)
void add(Patient* pNDS, int& refcount)
{
//add new object
pNDS->pNext=pHead;
pHead=pNDS;
//fill in its member data
// Improvised part..WORKS!!
cout<<"Enter the Patients name (use underscores for spaces): ";
char namebuffer[128];
cin>>namebuffer;
strncpy(pNDS->m_Name,namebuffer,128);
pNDS->m_Name[127]='\0';
cout<<"\nEnter the Patients ID: ";
int PatientID;
cin>>PatientID;
pNDS->m_ID=PatientID;
cout<<"\nEnter the Patients year of birth: ";
int PatientDOB;
cin>>PatientDOB;
pNDS->m_DOB=PatientDOB;
cout<<"\nEnter the Patients address: ";
char addressbuffer[128];
cin>>addressbuffer;
strncpy(pNDS->m_Address,addressbuffer,128);
pNDS->m_Address[127]='\0';
cout<<"\nEnter Patients previous medications: ";
char premedbuffer[128];
cin>>premedbuffer;
strncpy(pNDS->m_PreMed,premedbuffer,128);
pNDS->m_PreMed[127]='\0';
refcount++;
}
// Save to file ()
inline void save(Patient* pNDS)
{
Patient* pL=pHead;
FILE *fp;
fp = fopen("D:\junk.txt", "w+");
while(pL)
{
fprintf(fp, "%s\t",pNDS->m_Name);
fprintf(fp, "%d\t",pNDS->m_ID);
fprintf(fp, "%d\n",pNDS->m_DOB);
fprintf(fp, "%s\t",pNDS->m_Address);
fprintf(fp, "%s\t",pNDS->m_PreMed);
pL=pL->pNext;
}
fclose(fp);
}
int main()
{
Patient objpat;
Patient::count=0;
//menu simulation
char cChoice;
do
{
cout<<"\tChoose the Operation by selecting the appropriate number:- "<<endl;
cout<<"1) Create new Patient file.\n2) View All "<<Patient::count<<" entries.\n3) Quit."<<endl;
int msg;
cin>>msg;
switch(msg)
{
case 1:
{
add(& objpat, Patient::count);
break;
}
case 2:
{
break;
}
case 3:
save(& objpat);
cChoice='Q';
break;
default:
cout<<"Invalid number";
}
}while(cChoice!='Q');
system("PAUSE");
return 0;
}
Please it has been bugging me for so long.............Thanks for your time.