Doubt about switch

Asked

Viewed 77 times

1

I would like the switch show me the sum if I choose case 1 as follows the code, but it does not perform the operations.

#include<stdio.h>

int main() {

int a,b;
int operacao;
int soma = a + b;
int subtracao = a - b;
int multiplicacao = a * b;
int divisao = a / b;
//exponenciacao;


printf("digite o primeiro numero:\n");
scanf("%d", &a);
printf("digite o segundo numero:\n");
scanf("%d", &b);

printf("Qual operacao voce deseja ?\n");
printf(" 1 Soma\n 2 Subtracao\n 3 Multiplicacao\n 4 Divisao\n");
scanf("%d", &operacao);

switch(operacao)
{
case 1:
    printf("voce escolheu soma %d", soma);  *// AQUI EU GOSTARIA QUE ELE REALIZASSE A SOMA, PORÉM ELE SEMPRE APRESENTA O VALOR 10557854* 
    break;
case 2:
    printf("voce escolheu Subtracao %d", subtracao);
    break;
case 3:
    printf("voce escolheu Multiplicacao %d", multiplicacao);
    break;
case 4:
    printf("voce escolheu Divisao %d", divisao);
    break;

}
}
  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.

2 answers

4

I don’t know why you created these variables, especially doing the operations before having the values typed. A program executes in an order. And contrary to what you must have learned, you declare the variables as needed, in the location closest to their use.

#include<stdio.h>

int main() {
    int a, b, operacao;
    printf("digite o primeiro numero:\n");
    scanf("%d", &a);
    printf("digite o segundo numero:\n");
    scanf("%d", &b);
    printf("Qual operacao voce deseja ?\n");
    printf(" 1 Soma\n 2 Subtracao\n 3 Multiplicacao\n 4 Divisao\n");
    scanf("%d", &operacao);
    switch (operacao) {
    case 1:
        printf("voce escolheu soma %d", a + b);
        break;
    case 2:
        printf("voce escolheu Subtracao %d", a - b);
        break;
    case 3:
        printf("voce escolheu Multiplicacao %d", a * b);
        break;
    case 4:
        printf("voce escolheu Divisao %d", a / b);
        break;
    }
}

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

3

Operations must be done after you receive the values of the variables.

case 1:
    printf("voce escolheu soma %d", a + b);
    break;

Or

case 1:
    soma = a + b;
    printf("voce escolheu soma %d", soma);
    break;

Your mistake is to perform the sum before having the values of a and b.

  • In case then I always have to call the operation within each case ? because above in the code there is a part that asks the user to enter the values of a and b and the same are assigned to the due variables and in the declension of the variable sum I have already determined that it receives the value of a + b, so I understood then I must "repeat" this declaration process within each case, correct ?

  • 2

    It doesn’t have to be inside the case, but when performing the operation the variables have to have values. if you do so: int a = 10; int b= 20; int sum a +b; // will give 30 But if you do: int a; int b; int sum a +b; // will give problem, the program does not know the values of a and b, How will he add up values he still doesn’t know about? When the program passes through a line it executes the operation.

  • Now I get it man, I forgot the basics of it read the instructions sequentially, thank you so much for the help.

Browser other questions tagged

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