LA FUNCION CAPTURAR ES LA DEL PROBLEMA, justo abajo de la generacion del health number, en esa parte al escribir el archivo, gracias.
Código:
#ifndef PATIENT_H
#define PATIENT_H
class Patient
{
public:
void capture();
void show();
void deleteP();
void searchP();
void edit();
private:
char healthNumber[30];
char name[30];
char ailing[30];
char treatment[30];
};
#endif // PATIENT_H
Código:
#include <iostream>
#include <fstream>
#include <string.h>
#include <string>
#include "Patient.h"
#include <cstring>
using namespace std;
int main(){
int op;
Patient patient;
int z=1;
cout<<"---------------------------------------PATIENT INFO----------------------------------------"<<endl;
cout<<"PLEASE TYPE EVERYTHING WITH CAPITAL LETTERS, OTHERWISE YOU MAY HAVE PROBLEMS FINDING A FILE"<<endl<<endl;
while(z!=0){
cout<<"What do you want to do?"<<endl;
cout<<"\t 1)Capture"<<endl;
cout<<"\t 2)Show"<<endl;
cout<<"\t 3)Search"<<endl;
cout<<"\t 4)Delete"<<endl;
cout<<"\t 5)Edit"<<endl;
cin>>op;
switch(op)
{
case 1:
patient.capture();
break;
case 2:
patient.show();
break;
case 3:
patient.searchP();
break;
case 4:
patient.deleteP();
break;
case 5:
patient.edit();
break;
default:
cout<<"Your option was invalid, type a valid number."<<endl;
}
}
return 0;
}
Código:
#include "Patient.h"
#include <iostream>
#include <string>
#include <string.h>
#include <fstream>
using namespace std;
void Patient::capture()
{
int hN,n,a,t;
cout<<"Name: ";
cin.ignore();
cin.getline(name,30);
cout<<"Ailing: ";
cin.getline(ailing,30);
cout<<"Treatmeant: ";
cin.getline(treatment,30);
//--------------HEALTH NUMBER GENERATOR----------------
char *ptr;
ptr = strtok(name," ");
string aux = ptr;
string letter = aux.substr(0,1);
string add = letter;
cout<<"Health Number: ";
while(ptr != NULL)
{
ptr = strtok(NULL, " ");
if(ptr == NULL)break;
string aux = ptr;
letter = aux.substr(0,1);
add = add + letter;
}
char *aux1 = new char[add.length() + 1];
strcpy(aux1, add.c_str());
delete [] aux1;
//-----------------------------------------------------
cout<<add<<endl;
ofstream write;
write.open("Patients.txt",ios::app);
hN = strlen(aux1);
n = strlen(name);
a = strlen(ailing);
t = strlen(treatment);
write.write((char *)&hN,sizeof(hN));
write.write((char *)&aux1,hN);
write.write((char *)&n,sizeof(int));
write.write((char *)&name,n);
write.write((char *)&a,sizeof(int));
write.write((char *)&ailing,a);
write.write((char *)&t,sizeof(int));
write.write((char *)&treatment,t);
write.close();
}
void Patient::show()
{
int hN,n,a,t;
ifstream read;
read.open("Patients.txt", ios::in);
if (!read.good())
{
cout<<"\n\n\tFile not found."<<endl;
}
else
{
while(!read.eof()){
read.read((char *)&hN,sizeof(int));
read.read((char *)&healthNumber,hN);
healthNumber[hN]='\0';
read.read((char *)&n,sizeof(int));
read.read((char *)&name,n);
name[n]='\0';
read.read((char *)&a,sizeof(int));
read.read((char *)&ailing,a);
ailing[a]='\0';
read.read((char *)&t,sizeof(int));
read.read((char *)&treatment,t);
treatment[t]='\0';
if(read.eof()){break;}
cout<<endl;
cout<<"___________________________________"<<endl;
cout<<"Health Number: "<<healthNumber<<endl;
cout<<"Name: "<<name<<endl;
cout<<"Ailing: "<<ailing<<endl;
cout<<"Treatment: "<<treatment<<endl;
cout<<"___________________________________"<<endl;
}read.close();
}
}
void Patient::searchP()
{
int hN,n,a,t;
string id;
ifstream read ("Patients.txt");
bool found = false;
cout<<"Type de health number [ID] that you want to find: ";
cin>>id;
if (!read.good())
{
cout<<"\n\n\tFile not found."<<endl;
}
else
{
while(!read.eof() && found == false){
read.read((char *)&hN,sizeof(int));
read.read((char *)&healthNumber,hN);
healthNumber[hN]='\0';
read.read((char *)&n,sizeof(int));
read.read((char *)&name,n);
name[n]='\0';
read.read((char *)&a,sizeof(int));
read.read((char *)&ailing,a);
ailing[a]='\0';
read.read((char *)&t,sizeof(int));
read.read((char *)&treatment,t);
treatment[t]='\0';
if(healthNumber == id){
cout<<"______________FOUND_______________"<<endl;
cout<<"Health Number: "<<healthNumber<<endl;
cout<<"Name: "<<name<<endl;
cout<<"Ailing: "<<ailing<<endl;
cout<<"Treatment: "<<treatment<<endl;
cout<<"___________________________________"<<endl;
found = true;
}
if(read.eof()){cout<<"/t Health number not found."<<endl; break;}
}
read.close();
}
}
void Patient::deleteP()
{
string word;
cout<<"Type the idProfile that you want to find: "<<endl;
cin>>word;
int hN,n,a,t;
ifstream read;
read.open("User.txt",ios::in);
ofstream aux;
aux.open("temp.txt",ios::out);
bool found = false;
if (!read.good())
{
cout<<"\n\n\tFile not found."<<endl;
}
else
{
while(!read.eof()){
found = false;
hN = strlen(healthNumber);
n = strlen(name);
a = strlen(ailing);
t = strlen(treatment);
read.read((char *)&hN,sizeof(int));
read.read((char *)&healthNumber,hN);
healthNumber[hN]='\0';
read.read((char *)&n,sizeof(int));
read.read((char *)&name,n);
name[n]='\0';
read.read((char *)&a,sizeof(int));
read.read((char *)&ailing,a);
ailing[a]='\0';
read.read((char *)&t,sizeof(int));
read.read((char *)&ailing,t);
treatment[t]='\0';
if(word == name){
cout<<"______________DELETED_______________"<<endl;
cout<<"Health number: "<<healthNumber<<endl;
cout<<"Name: "<<name<<endl;
cout<<"Ailing: "<<ailing<<endl;
cout<<"Treatment: "<<treatment<<endl;
cout<<"___________________________________"<<endl;
found = true;
}
else if(found == false){
if(read.eof()){cout<<"File not found."<<endl; break;}
hN = strlen(healthNumber);
n = strlen(name);
a = strlen(ailing);
t = strlen(treatment);
aux.write((char *)&hN,sizeof(int));
aux.write((char *)&healthNumber,hN);
aux.write((char *)&n,sizeof(int));
aux.write((char *)&name,n);
aux.write((char *)&a,sizeof(int));
aux.write((char *)&ailing,a);
aux.write((char *)&t,sizeof(int));
aux.write((char *)&treatment,t);
}
}
read.close();
aux.close();
remove("User.txt");
rename("temp.txt","User.txt");
}
}
void Patient::edit()
{
string word;
cout<<"Type the idProfile that you want to find: "<<endl;
cin>>word;
int hN,n,a,t;
ifstream read;
read.open("User.txt",ios::in);
ofstream aux;
aux.open("temp.txt",ios::out);
bool found = false;
if (!read.good())
{
cout<<"\n\n\tFile not found."<<endl;
}
else
{
while(!read.eof()){
found = false;
hN = strlen(healthNumber);
n = strlen(name);
a = strlen(ailing);
t = strlen(treatment);
read.read((char *)&hN,sizeof(int));
read.read((char *)&healthNumber,hN);
healthNumber[hN]='\0';
read.read((char *)&n,sizeof(int));
read.read((char *)&name,n);
name[n]='\0';
read.read((char *)&a,sizeof(int));
read.read((char *)&ailing,a);
ailing[a]='\0';
read.read((char *)&t,sizeof(int));
read.read((char *)&ailing,t);
treatment[t]='\0';
if(word == name){
cout<<"______________DELETED_______________"<<endl;
cout<<"Health number: "<<healthNumber<<endl;
cout<<"Name: "<<name<<endl;
cout<<"Ailing: "<<ailing<<endl;
cout<<"Treatment: "<<treatment<<endl;
cout<<"___________________________________"<<endl;
found = true;
}
else if(found == false){
if(read.eof()){cout<<"File not found."<<endl; break;}
hN = strlen(healthNumber);
n = strlen(name);
a = strlen(ailing);
t = strlen(treatment);
aux.write((char *)&hN,sizeof(int));
aux.write((char *)&healthNumber,hN);
aux.write((char *)&n,sizeof(int));
aux.write((char *)&name,n);
aux.write((char *)&a,sizeof(int));
aux.write((char *)&ailing,a);
aux.write((char *)&t,sizeof(int));
aux.write((char *)&treatment,t);
}
}
read.close();
aux.close();
remove("User.txt");
rename("temp.txt","User.txt");
}
}