Reputation: 11
I have 2 structures: medic and patient. I have to read data from 2 different .txt docs and then show it in the console.
I'm using CodeBlocks. When I tried to debug this, I found out this happens right after citireM is executed. I asked my teacher about it and did some googling but to no avail.
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
struct date
{
int d,m,y;
};
struct medic
{
int cod;
char name[50],specs[50];
};
struct patient
{
int cod;
date bd;
char name[50],adress[50];
};
struct consultatie
{
int codp,codm;
date dc;
char diag[100];
};
void citireM(medic M[], int &n)
{
int i; char * p; char l[50];
ifstream f("medics.txt");
f>>n;
for(i=0;i<n;i++)
{
strcpy(l,"");
f>>M[i].cod;
f.getline(l,50);
p=strtok(l,";");
strcpy(M[i].name,p);
p=strtok(NULL,";");
strcpy(M[i].specs,p);
}
}
void citireP(patient P[], int &n)
{
char * p; char l[50];
ifstream ff("patients.txt");
ff>>n;
for(int i=0;i<n;i++)
{
ff>>P[i].cod;
strcpy(l,"");
ff.getline(l,50);
p=strtok(l,";");
strcpy(P[i].name,p);
p=strtok(NULL,";");
strcpy(P[i].adress,p);
ff>>P[i].bd.d>>P[i].bd.m>>P[i].bd.y;
}
}
void printM(medic M[], int n)
{
for (int i=0;i<n;i++)
cout<<M[i].cod<<" "<<M[i].name<<" "<<M[i].specs;
}
void printP(patient P[], int n)
{
int i;
for (i=0;i<n;i++)
cout<<P[i].cod<<" "<<P[i].name<<" "<<P[i].adress<<" "<<P[i].bd.d<<"/"<<P[i].bd.m<<"/"<<P[i].bd.y;
}
int main()
{
medic m[30];
patient p[300];
int nm,np;
citireM(m,nm);
citireP(p,np);
printM(m,nm);
printP(p,np);
return 0;
}
medics.txt
3 007 J.J. Jouleau; medic stomatolog; 32 Michael Bush; medic chirurg; 88 Ceva Nume Lung Aici; medic neidentificat;
patients.txt
2 321 Insert Name Here; Timisoara, judetul Timis; 2 5 1991 123 Insert Some Other Name Here; Nu se stie unde traieste; 1 6 1654
Upvotes: 1
Views: 87
Reputation: 153
I am trying to keep your function to as close to the same as your. There are much cleaner ways to do this though. This also does not take into account errors with your input files, but I just wanted to get you on the right track.
int char_to_int(char* src)
{
int res = 0;
for(int i = 0; src[i] != '\0'; ++i) {
res = res * 10 + src[i] - '0';
}
return res;
}
void citireP(patient P[], int &n)
{
const int BUFFER_SIZE = 256;
char* p; char l[BUFFER_SIZE];
char* date;
ifstream ff("patients.txt",ios_base::skipws);
ff >> n;
char input[3][BUFFER_SIZE];
for(int i = 0; i<n; i++) {
ff >> P[i].cod;
strcpy(l, "");
ff.getline(l,BUFFER_SIZE);
p = strtok(l, ";");
strcpy(P[i].name, p);
p = strtok(NULL, ";");
strcpy(P[i].adress, p);
p = strtok(NULL, ";");
date = strtok(p, " ");
P[i].bd.d = char_to_int(date);
date = strtok(NULL, " ");
P[i].bd.m = char_to_int(date);
date = strtok(NULL, " ");
P[i].bd.y = char_to_int(date);
}
}
edit...
I just wrote a atoi type function for you so you dont have include extra things in your project.
Upvotes: 1