Help me with this while and switch code in C?

Asked

Viewed 106 times

-2

Well I did this code in college, where it receives two integers and as the User chooses it performs an arithmetic operation with these two numbers typed. I believe my logic is correct, it compiles but does not complete its proper function. There must be only one syntax error that is compromising the code completely. Help me please ! is worth noting this activity. Follow the code below.

```
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>


main(){
 int n1,n2,res,opc;
 float resu;

 do{
    printf("\nDigite o primeiro numero inteiro :\n");
    scanf("%d",&n1);
    printf("\nDigite o segundo numero inteiro :\n");
    scanf("%d",&n2);
    system("cls");
    printf("\nMENU DE OPCOES");        
    printf("\n0.sair");
    printf("\n1.soma dos dois numeros");   
    printf("\n2.subtracao do primeiro pelo segundo");   
    printf("\n3.subtracao do segundo pelo primeiro");   
    printf("\n4.multiplicacao dos dois numeros");   
    printf("\n5.divisao do primeiro pelo segundo");   
    printf("\n6.divisao do segundo pelo primeiro");   
    printf("\n7.quociente inteiro da divisao do primeiro pelo segundo");   
    printf("\n8.quociente inteiro da divisao do segundo pelo primeiro");   
    printf("\n9.resto da divisao do primeiro pelo segundo");   
    printf("\n10.resto da divisao do segundo pelo primeiro");   
    printf("\n11.o primeiro elevado pelo segundo");
    printf("\n12.o segundo elevado pelo primeiro");
    printf("\nDigite o numero da opcao desejada :\n");
    scanf("%d",&opc);
    system("cls");

 switch (opc){
 case 1 : 
        res=n1+n2;
        printf("O resultado da soma eh: %d",res);    
        break;
   case 2 :
        res=n1-n2;
        printf("O resultado da subtracao eh: %d",res);          
        break;     
   case 3 :
        res=n2-n1;
        printf("O resultado da subtracao eh: %d",res);
        break;
   case 4 :
        res=n1*n2;
        printf("O resultado da multiplicacao eh: %d",res);
        break;
   case 5 :
        if (n1 ==0){
        printf("Divisao por 0 !");  
        }
        else{
        resu =(float) n1/n2;
        printf("O resultado da divisao eh: %.2f",resu);
        }
        break;
   case 6 :
        if (n2 == 0){
        printf("Divisao por 0 !");  
        }
        else{
        resu =(float) n2/n1;
        printf("O resultado da divisao eh: %.2f",resu);
        }
        break;
   case 7 :
        res = n1 - ((n1/n2) * n2);
        printf("O quociente da divisao eh : %d",res);
        break;
   case 8 :
        res = n2 - ((n2/n1) * n1);
        printf("O quociente da divisao eh : %d",res);
        break;
   case 9 :
        res=n1%n2;
        printf("O resto da divisao eh: %d",res);
        break;
   case 10 :
        res=n2%n1;
        printf("O resto da divisao eh: %d",res);
        break;
   case 11 : 
        res=pow(n1,n2);
        printf("O resulltado da elevacao eh: %d",res);
        break;
   case 12 :
        res=pow(n2,n1);
        printf("O resulltado da elevacao eh: %d",res);
        break;
    case 0 :
        system("exit");
        break;
   default : 
        printf("Valor Invalido!\n");                                                                    
        break;
    }
   }while (opc!=0); 
   return 0;  
}

  • have tried scanf("%d",&opc);?

  • Take this system("Exit"); and let the program terminate normally when option 0 is informed.

  • Without having to do with the reported problem but in the division options you are testing if the numerator is zero when you should be testing the denominator.

1 answer

2


Friend what appears to be wrong is when selecting the option, scanf("%d",opc); the variable that will be receiving value must be with the & therefore --> scanf("%d",&opc), and you did not declare the library to use the function system("cls"), in case it would be the #include <windows> I hope I’ve helped.

  • 1

    Thank you for the observation, I broke my head a lot thinking that it was another structural error and really what was missing was the commercial one (&). Thanks for the attention !!!!

Browser other questions tagged

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