I have the task of creating four functions that do multiplication, division, addition and subtraction between two numbers given by the user. The operation to be performed must be chosen using the * / - + characters, given by the user.
I need to use pointers to do the operations, the pointer to the first number works fine, but the second, when it enters the function, always changes the value to 0.
#include <iostream>
#include <cstdio>
int mult(int *a, int *b)
{
return *a *= *b;
}
float div(int *a, int *b)
{
return *a /= *b;
}
int sub(int *a, int *b)
{
return *a -= *b;
}
int soma(int *a, int *b)
{
return *a += *b;
}
int main()
{
int num1, num2;
char op;
printf("Digite o primeiro numero: ");
scanf("%d", &num1);
printf("\nDigite o segundo numero: ");
scanf("%d", &num2);
printf("\nNumeros: %d e %d", num1, num2);
printf("\nDigite a operacao a ser realizada (* / - +): ");
scanf("%s", &op);
if (op == '*')
{
printf("\n%d * %d = %d\n", num1, num2, mult(&num1, &num2));
}else if(op == '/')
{
printf("\n%d / %d = %f\n", num1, num2, div(&num1, &num2));
}else if(op == '-')
{
printf("\n%d - %d = %d\n", num1, num2, sub(&num1, &num2));
}else if (op == '+')
{
printf("\n%d + %d = %d\n", num1, num2, soma(&num1, &num2));
}else
{
printf("\nOperacao invalida\n");
}
return 0;
}
What is happening and why does one pointer differ from the other if they are implemented in the same way?