Function does not return the value I want

Asked

Viewed 66 times

-1

#include<stdlib.h>
#include<stdio.h>
int calculo(int pontos, int placar1, int placar2);

int main()
{
    int c;
    int placar1, placar2;
    char bet1n[30], bet2n[30], bet3n[30], bet4n[30], bet5n[30];
    int bet1a = 0, bet1b = 0, bet2a = 0, bet2b = 0, bet3a = 0, bet3b = 0, bet4a = 0, bet4b = 0, bet5a = 0, bet5b = 0;
    int p1 = 0, p2 = 0, p3 = 0, p4 = 0, p5 = 0;
    printf("Digite a quantidade de gols do primeiro time:\n");
    scanf_s("%d", &placar1);
    while ((c = getchar()) != '\n' && c != EOF) {}

    printf("Digite a quantidade de gols do segundo time:\n");
    scanf_s("%d", &placar2);
    while ((c = getchar()) != '\n' && c != EOF) {}

    printf("O placar do jogo foi %d a %d\n", placar1, placar2);

    printf("Apostador 1. Digite o seu nome:\n");
    gets_s(bet1n);

    printf("Apostador digite a quantidade de gols do primeiro time:\n");
    scanf_s("%d", &bet1a);
    while ((c = getchar()) != '\n' && c != EOF) {}

    printf("Apostador digite a quantidade de gols do segundo time:\n");
    scanf_s("%d", &bet1b);
    while ((c = getchar()) != '\n' && c != EOF) {}

    calculo(p1, bet1a, bet1b);
    printf("%d", p1);   // aqui so retorna 0
    printf("Apostador 2. Digite o seu nome:\n");
    gets_s(bet2n);


    printf("Apostador 3. Digite o seu nome:\n");
    gets_s(bet3n);


    printf("Apostador 4. Digite o seu nome:\n");
    gets_s(bet4n);


    printf("Apostador 5. Digite o seu nome:\n");
    gets_s(bet5n);


    system("pause");
    return 0;
}
int calculo(int pontos, int placar1, int placar2) {
    if (placar1 < placar2)
    {
        pontos = 10;
        if (placar1 == 0)
        {
            pontos = pontos + 5;
        }
        if (placar2 == 4)
        {
            pontos = pontos + 5;
        }
        return (pontos);
    }
    else {
        if (placar1 == 0)
        {
            pontos = pontos + 5;
        }
        if (placar2 == 4)
        {
            pontos = pontos + 5;
        }
        return (pontos);
    }
}
  • 2

    And what did you want to return?

  • The value of points summed after condition

  • The function is basically not working, instead of P1 receiving the value of the points summed, it is getting 0

  • Your function is set to receive 3 integers as parameters, but you are invoking it with an integer and two strings, as well as disregarding the return value. You are using parameter-by-value passage and therefore will not change the value of the variable in the calling program.

  • strings? but bet1a and bet1b are in int, and what is my error in return value? I am beginner, I believe that revert return the value of points using the variable points.

  • I got confused with the variable names, but you are disregarding the return value of the function and therefore printing the value with which the function was called and which is not modified by the function, which in this case is 0.

  • And how I find that?

  • Put a variable to receive the return value of the calculation function

Show 3 more comments

1 answer

1

It is necessary to change the declaration and the use of the function "calculation".

Option 1:

int calculo(int, int);

int main()
{
  ....
  p1 = calculo(bet1a, bet1b);
  printf("%d", p1);
  ...
}

int calculo(int placar1, int placar2)
{
  int pontos = 0;

  if (placar1 < placar2)
  {
    pontos = 10;
    if (placar1 == 0)
    {
      pontos += 5;
    }
    if (placar2 == 4)
    {
      pontos += 5;
    }
    return pontos;
  }
  else {
    if (placar1 == 0)
    {
      pontos += 5;
    }
    if (placar2 == 4)
    {
      pontos += 5;
    }
    return pontos;
  }
}

Option 2:

void calculo(int*, int, int);

int main()
{
  ....
  calculo(&p1, bet1a, bet1b);
  printf("%d", p1);
  ...
}

void calculo(int* pontos, int placar1, int placar2)
{
  if (placar1 < placar2)
  {
    *pontos = 10;
    if (placar1 == 0)
    {
      *pontos += 5;
    }
    if (placar2 == 4)
    {
      *pontos += 5;
    }
  }
  else {
    if (placar1 == 0)
    {
      *pontos += 5;
    }
    if (placar2 == 4)
    {
      *pontos += 5;
    }
  }
}

Also, probably the logic of the "calculation" function is wrong, since in any case the function is always adding 5 to the variable "points".

  • Man, thank you very much the logic was right actually, but I declared P1 within the function and I thought that the return would be enough, but making option 1 worked, I declared a variable to receive the value, thank you very much bro

Browser other questions tagged

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