Can anyone help me find what is causing a build error in the code?

Asked

Viewed 50 times

1

I went to solve exercise 1022 of Urionlinejudge and received a Compilation error, but the code works perfectly in Code::Blocks. Please inform me of the error. Link to the problem.

#include <stdio.h>

void simplificacao(int *a, int *b){
    for(int j=*b;j>0;j--){
        if(*a%j==0 && *b%j==0){
            *a/=j;
            *b/=j;
        }
    }
}

int main(){
    int x, N1, D1, N2, D2, i, N3, D3;
    char t1, op, t2;
    scanf("%d", &x);
    for(i=0;i<x;i++){
        scanf("%d %c %d %c %d %c %d", &N1, &t1, &D1, &op, &N2, &t2, &D2);
        if(op == '+'){
            N3 = (N1*D2 + N2*D1);
            D3 = (D1*D2);
            printf("%d/%d = ", N3, D3);
            simplificacao(&N3,  &D3);
            printf("%d/%d\n", N3, D3);
        }
        else if(op == '-'){
            N3 = (N1*D2 - N2*D1);
            D3 = (D1*D2);
            printf("%d/%d = ", N3, D3);
            simplificacao(&N3, &D3);
            printf("%d/%d\n", N3, D3);
        }
        else if(op == '*'){
            N3 = (N1*N2);
            D3 = (D1*D2);
            printf("%d/%d = ", N3, D3);
            simplificacao(&N3, &D3);
            printf("%d/%d\n", N3, D3);
        }
        else if(op == '/'){
            N3 = (N1*D2);
            D3 = (N2*D1);
            printf("%d/%d = ", N3, D3);
            simplificacao(&N3, &D3);
            printf("%d/%d\n", N3, D3);
        }
    }
    return 0;
}
  • If you’re using the compiler gcc with Codeblocks, try the parameters gcc -std=c89 -pedantic ... to have the same behavior as online.

1 answer

0


Your code is correct for recent versions of C since C99.

In C89 (apparently the C that is used on the site in question), it is not possible to use the variable statement within the for

// válido em C99, C11, C17
for(int j=*b;j>0;j--){
    // ...
}

Changes the j out of the for and adds a disfellowshipped block if you want to keep the Scope limited

// válido em C89 (C90), C99, C11, C17
{
    int j;
    for(j = *b; j > 0; j--) {
        // ...
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.