jeudi 2 mars 2017

C++ program jumping to end of function without using goto

The following code is ought to read some input and construct a matrix and a vector using it. The problem with it occurs at the second do-while block. Whether the program enters the first if(isalpha(x)) or the else, after the execution of the if-blocks is jump to the end of the function. Can anybody point out why?

FILE *fin = fopen("dfain.txt","r");

void fast_forward_newline(){
    char c;
    while(c != '\n')
        fscanf(fin,"%c",&c);
}

void construct_structures(int ***a, int **final_states, int &stari, int &litere){
    char x;
    int i = 0;
    *final_states = (int *)malloc(arrayMAX*sizeof(int));
    do{
        fscanf(fin,"%c",&x);
        if(isdigit(x)){
            *(*final_states + i) = x - '0';
            ++i;
        }
    }while(x != '\n');
    char maxi2 = 'a';
    int cnt = 0, maxi = INT_MIN;
    do{
        if(feof(fin))
            break;
        fscanf(fin,"%c",&x);
        if(isalpha(x)){
            if(maxi2 <= x)
                ++cnt;
        }
        else{
            if(isdigit(x))
                if(x - '0' > maxi)
                    maxi = x - '0';
        }
    }while(1);

    stari = maxi;
    litere = cnt;
    *a = (int **)malloc(stari * sizeof(int *));
    for(int i=0;i<stari;++i)
        *(*a+i) = (int *)calloc(litere,sizeof(int));
    fseek(fin,0,SEEK_SET);
    fast_forward_newline();

    do{
        int d1 = -1, d2 = -1;
        char alfa;
        fscanf(fin,"%c",&x);
        if(feof(fin));
            break;
        if(isdigit(x)){
            if(d1 == -1 && d1 == d2)
                d1 = x - '0';
            else
                if(d1 != -1)
                    d2 = x - '0';
        }
        else
            if(isalpha(x))
                alfa = x;
        *(*(*a+d1)+(x-'a')) = d2; // tot alfabetul de litere mici
    }while(1);
}

The input is as follows:

4 5
0 a 2
0 b 1
1 b 2
2 c 3
3 a 3
3 b 4
3 c 5

Aucun commentaire:

Enregistrer un commentaire