Pointer changes the value within function in C++

Asked

Viewed 198 times

2

I have the task of creating four functions that do multiplication, division, sum and subtraction between two numbers given by the user. The operation to be performed must be chosen through the * / - +characters, given by the user.

I must use pointers to do the operations, the pointer to the first number works well, but the second, when you enter 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 one pointer differs from the other if they are implemented in the same way?

  • Why do you do return *a *= *b instead of return *a * *b. You need the first argument to contain the result when returning from the function?

  • Hello, I just need to return the result. I tried it the way return *a * *b but the second pointer continues at 0.

2 answers

3


The accounts in your functions are wrong. You should use +, -, *, / instead of +=, -=, *= and /=.

And I believe the error happens when the program tries to read the character of the operation. The correct one would be:

scanf(" %c", &op); //com espaço antes do %c

Note: why you are using printf and scanf instead of Std::Cout and Std::Cin ?

  • I thought mention to the cin was superworthy to a question of c++

  • I fixed with the " %c" and it went all right, the operations are being done correctly. Already the printf and scanf has become a custom use, kkkk.

0

You are putting the result of the value of your operations into num1.

What you are doing is similar to:

int num1 = 0;
int num2  = 5;
num1 += num2; 

Now the value of num1 is the result of the operation: 5.

What you want is return num1 + num2 or in pointer dereferentiation: return *a + *b. Which returns the sum of the values and does not add the variable num1 the result.

Just remove the = of operations.

Note also that although the function returns float you must explicitly convert the values to float in your division.

float div(int *a, int *b)
{
    return (float)*a / (float)*b;
}

Browser other questions tagged

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