Execution error on switch

Asked

Viewed 62 times

1

#include <stdio.h>
#include <math.h>

int main ()
{
int n1, n2, operacao, soma, sub, mult;  
float div;  
    printf("Escolha a opecaracao que deseja realizar: "); 
    scanf("d", &operacao);

    switch(operacao)
    {       
        case 1: '+';
            printf("\nInsira N1: "); scanf("%d", &n1);
            printf("\nInsira N2: "); scanf("%d", &n2);
            soma = n1+n2;
            printf("\n%d + %d = %d", n1, n2, soma);
        break;  

        case 2: '-';
            printf("\n Insira N1: "); scanf("%d", &n1);
            printf("\n Insira N2: "); scanf("%d", &n2);
            sub = n1-n2;
            printf("\n %d - %d = %d", n1, n2, sub);
        break;

        case 3: '*';
            printf("\n Insira N1: "); scanf("%d", &n1);
            printf("\n Insira N2: "); scanf("%d", &n2);
            mult = n1*n2;
            printf("\n %d X %d = %d", n1, n2, mult);    
        break;  

        case 4: '/';
            printf("\n Insira N1: "); scanf("%d", &n1);
            printf("\n Insira N2: "); scanf("%d", &n2);
            div = n1/n2;
            printf("\n %d / %d = %.2f", n1, n2, div);
        break;      
    }

    return 0;
}
  • what the operation is doing in front of the case? already tried to remove '+'; ?

  • The program compiles with the line scanf("d", &operacao); ? Or even do the switch with a int ?

1 answer

1


One of the errors presented is that it is asking wrong formatting on scanf(). I actually think I wanted to ask for the operation character and not a number, so I changed that too. And the syntax of case you’re just wrong is just case and the value that is comparing and :, nothing else. I took advantage and reduced the scope of the variables and eliminated what was unnecessary.

#include <stdio.h>
#include <math.h>

int main() {
    char operacao;
    printf("Escolha a opecaracao que deseja realizar: "); 
    scanf("%c", &operacao);
    switch (operacao) {       
    case '+':
        int n1, n2;
        printf("\nInsira N1: "); scanf("%d", &n1);
        printf("\nInsira N2: "); scanf("%d", &n2);
        printf("\n%d + %d = %d", n1, n2, n1  +n2);
    break;
    case '-':
        int n1, n2;
        printf("\n Insira N1: "); scanf("%d", &n1);
        printf("\n Insira N2: "); scanf("%d", &n2);
        printf("\n %d - %d = %d", n1, n2, n1 - n2);
    break;
    case '*':
        int n1, n2;
        printf("\n Insira N1: "); scanf("%d", &n1);
        printf("\n Insira N2: "); scanf("%d", &n2);
        mult = n1*n2;
        printf("\n %d X %d = %d", n1, n2, n1 * n2);    
    break;
    case '/':
        int n1, n2;
        printf("\n Insira N1: "); scanf("%d", &n1);
        printf("\n Insira N2: "); scanf("%d", &n2);
        printf("\n %d / %d = %.2f", n1, n2, (float)n1 / n2);
    break;      
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • My compiler gives error when defining the variable inside the case, is it possible to do this with newer compilers? , chat instead of char

  • 1

    What kind of '80s compiler are you using today? : ) When you see an error you can edit.

  • I have to update my compiler so haha

  • My compiler gave error in this new code too haha. I use DEV++. Which one do you use?

  • Devc++ is a bomb, no one should use it, anything else is better.

Browser other questions tagged

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