이 기능은 .exe를 충돌시킵니다. 제가 뭘 잘못하고 있습니까? (This function makes the .exe crash, what am I doing wrong?)


문제 설명

이 기능은 .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 DavidJason Ball)

참조 문서

  1. This function makes the .exe crash, what am I doing wrong? (CC BY‑SA 2.5/3.0/4.0)

#crash #C++ #codeblocks #C






관련 질문

WebView가 전체 활동을 죽이고 있습니다 --- 이것을 어떻게 디버깅 할 수 있습니까? (WebView killing the whole activity --- How can I debug this?)

연결 시도 시 Windows의 MySQL이 충돌함 (MySQL on Windows crashes on connection attempts)

휘발성 멤버는 멤버 함수에서 액세스할 수 없습니다. (volatile member can not be accessed by member function)

Ruby에서 Gosu로 텍스트를 인쇄할 수 없음(충돌) (Cannot print a text with Gosu in Ruby (crash))

이 기능은 .exe를 충돌시킵니다. 제가 뭘 잘못하고 있습니까? (This function makes the .exe crash, what am I doing wrong?)

PyAudio로 웨이브를 재생하는 Tkinter 버튼 호출 기능 - 충돌 (Tkinter button calling function to play wave with PyAudio -- crashes)

phonegap 카메라가 앱 충돌 (phonegap camera crashes the app)

대화 상자를 표시하려고 할 때 앱이 충돌함 (App crashed when try to display dialog box)

kaldi 설치 시 libmkl_tbb_thread.so sth 관련 문제 (A problem related to libmkl_tbb_thread.so sth when installing kaldi)

WordPress에서 Simple Jquery Click이 작동하지 않음 (Simple Jquery Click not working in WordPress)

새 줄이 발견되지 않으면 fget이 NULL을 반환하지 않습니다. (fgets dont return NULL when no new line found)

webview가 검은 색으로 바뀌고 응용 프로그램이 충돌합니다. (webview turns black and application crashes)







코멘트