Calculator in C does not return decimal numbers, when dividing 7/2 it returns me 3. And how do I get the code back to the choice menu?

Asked

Viewed 44 times

-4

I have tried to use some other topics here but unfortunately they have not solved my problem. The most I could was showing 3.00 using float, but I don’t think I used it correctly. Whenever I divide I never return decimal number, for example 7/2 = 3 or 10/3 = 3. And another problem, how do I get my code instead of terminating the execution back to the "choose an operation"?

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include  <stdlib.h>
int soma (int s1, int s2)
{
int resultado;
resultado = s1+s2;
return(resultado);
}

int subtracao (int sub1, int sub2)
{
int resultado;
resultado = sub1-sub2;
return (resultado);
}

int multiplicacao (int m1, int m2)
{
int resultado;
resultado = m1*m2;
return (resultado);
}

int divisao (int d1, int d2)
{
int resultado;
if (d1==0 && d2==0){
printf ("Nao é possivel dividir por zero!");
}
else{
resultado = d1/d2;
}
return (resultado);
}

int fatorial (int numfat)
{
int resultado, fat;
resultado=1;
for (fat=1;fat<=numfat;fat++)
{
resultado=resultado*fat;
}
return (resultado);
}

int potencia (int base, int exp)
{
int resultado;
if (exp==0)
return 1;
else if (exp==1)
return base;

return resultado=base*potencia(base, exp-1);
}

int main (void)
{

int op, num1, num2, numfato, bas, expo, resultado;

printf ("\n1 - Adicao (+)\n");
printf ("2 - Subtracao (-)\n");
printf ("3 - Multiplicacao (*)\n");
printf ("4 - Divisao (/)\n");
printf ("5 - Fatoral (!)\n");
printf ("6 - Potenciacao (^)\n");
printf ("\nEscolha a operacao que sera realizada:\n");

scanf ("%d", &op);
system("cls");

switch (op){
case 1:
printf ("Operacao escolhida > ADICAO\n");
printf ("Insira o primeiro numero:\n");
scanf ("%d", &num1);
printf ("Insira o segundo numero:\n");
scanf ("%d", &num2);
resultado=soma(num1, num2);
printf ("Resultado da operacao: %d\n", resultado);
break;

case 2:
printf ("O peracao escolhida > SUBTRACAO\n");
printf ("Insira o primeiro numero:\n");
scanf ("%d", &num1);
printf ("Insira o segundo numero:\n");
scanf ("%d", &num2);
resultado=subtracao(num1, num2);
printf ("Resultado da operacao: %d\n", resultado);
break;

case 3:
printf ("Operacao escolhida > MULTIPLICACAO\n");
printf ("Insira o primeiro numero:\n");
scanf ("%d", &num1);
printf ("Insira o segundo numero:\n");
scanf ("%d", &num2);
resultado=multiplicacao(num1, num2);
printf ("Resultado da operacao: %d\n", resultado);
break;

case 4:
printf ("Operacao escolhida > DIVISAO\n");
printf ("Insira o primeiro numero:\n");
scanf ("%d", &num1);
printf ("Insira o segundo numero:\n");
scanf ("%d", &num2);
resultado=divisao(num1, num2);
printf ("Resultado da operacao: %d\n", resultado);
break;

case 5:
printf ("Operacao escolhida > FATORIAL\n");
printf ("Insira o numero a ter sua fatorial calculada:\n");
scanf("%d", &numfato);
resultado=fatorial(numfato);
printf ("Resultado da operacao: %d\n", resultado);
break;

case 6:
printf ("Operacao escolhida > POTENCIACAO\n");
printf ("Insira a base:\n");
scanf ("%d", &bas);
printf ("Insira o expoente:\n");
scanf ("%d", &expo);
resultado=potencia(bas,expo);
printf("Resultado da operacao: %d\n", resultado);
break;

default:
printf ("OPCAO INVALIDA\n");
}

getch();
return 0;
}
  • 3

    The division of two int's results in another int (with "automatic" rounding). If you want the decimals, you have to change everything to float or double: https://answall.com/q/453132/112052

  • This answers your question? decimal division result

1 answer

0

You have two situations here: To return to the menu at the end of the operation you can use the goto. With it you will tell your program that when it reaches a certain point, it will return to a point that you set in your program. Example:

First you indicate to which point of the program you want to return by placing an identification followed by :

menu:
printf ("\n1 - Adicao (+)\n");
printf ("2 - Subtracao (-)\n");

And then, where you want it to be called again the menu you insert goto menu. In your case it would be after displaying the result of each operation.

Your second problem, the division problem, is that whenever you work the division of two int it will return an integer number, so you need to change your code to double or float, both in the division block and to display the result.

Example:

#include <stdio.h>

int main(){
    float resultado,a,b;
    a=7;
    b=3;
    numero = a/b;
    printf("O resultado e':%.2f \n\n",resultado);
}
  • goto? could not use something better as to control the flow of the program, a while for example?

  • Using the goto will be easier to do, less complicated to read and will not impact on performance, beyond what by the question it is starting, so why not use?

  • well I suggest reading that question: Why the use of GOTO is considered bad?, i wouldn’t indicate to someone who is starting to learn goto, it is much more worth learning to use for/while/etc

  • The link you sent yourself says (not only this part, but several) that it’s not bad if used sparingly. " Take care of your health and use a little, very little bit of droplet, whenever it benefits more than hinder." And that’s the case. But it’s just an answer option, put another one in there and let him test the two, it’ll help him learn more than to debate with me here...

  • 1

    So much so that I even edited my answer from "need" to "can" because I expressed myself badly. But for those who are starting this discussion in the comments is less productive than you putting one more answer there for him. The problem with the goto is more the bad reputation for being overused in codes than for being really bad.

Browser other questions tagged

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