简体   繁体   English

此功能使.exe崩溃,我在做什么错?

[英]This function makes the .exe crash, what am I doing wrong?

I have 2 structures: medic and patient . 我有2种结构: 医护病人 I have to read data from 2 different .txt docs and then show it in the console. 我必须从2个不同的.txt文档中读取数据,然后将其显示在控制台中。
I'm using CodeBlocks. 我正在使用CodeBlocks。 When I tried to debug this, I found out this happens right after citireM is executed. 当我尝试调试它时,我发现在执行citireM之后立即发生这种情况。 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 medics.txt

3
    007 J.J. Jouleau; medic stomatolog;
    32 Michael Bush; medic chirurg;
    88 Ceva Nume Lung Aici; medic neidentificat;

patients.txt 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

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. 我刚刚为您编写了一个atoi类型的函数,因此您不必在项目中包含其他内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM