문제 설명
이 기능은 .exe를 충돌시킵니다. 제가 뭘 잘못하고 있습니까? (This function makes the .exe crash, what am I doing wrong?)
저에게는 의사와 환자라는 두 가지 구조가 있습니다. 2개의 다른 .txt 문서에서 데이터를 읽은 다음 콘솔에 표시해야 합니다. CodeBlocks를 사용하고 있습니다. 디버깅을 시도했을 때 이것은 citireM 직후에 발생한다는 것을 알았습니다. >가 실행됩니다. 선생님께 여쭤보고 구글링을 해보았지만 소용이 없었습니다.
#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 JJ Jouleau; 위생병; 32 마이클 부시; 메딕 처그; 88 Ceva Nume Lung Aici; 의료 신분증;
patients.txt
2 321 여기에 이름을 입력하십시오. 티미쇼아라, 주드툴 티미스; 2 5 1991 123 여기에 다른 이름을 삽입하십시오. Nu se stie unde traieste; 1 6 1654
참조 솔루션
방법 1:
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.
(by David、Jason Ball)