How to take information from one function and use it in another?

Asked

Viewed 3,240 times

2

I need a program that performs sum and multiplication according to the user’s choice and later select the corresponding function that will read the values, until the user enters 0 and the program generates the result. My program for any typed value is reporting 36. And another, if I wanted to repeat the process (calculate), and give the option to leave, would have to use one of the while? but when I get out of the loop?

 #include <stdio.h>

 void soma(void){
 int valor, soma, result;
  soma = 0;
   printf("Foi escolhida a soma:\n\n");
    do{
        printf("Informe os valores desejados e 0 (zero) para concluir:");
        scanf("%d", &valor);
        soma= soma+valor;
        result=soma;
    }while(valor!=0);
      }

     void mult(void){
     int valor, mult,result;
     mult= 1;
     printf("Foi escolhida a multiplicacao:\n\n");
       do{
        printf("Informe os valores desejados e 0 (zero) para concluir:");
        scanf("%d", &valor);
        if(valor==0){
            break;
        }
        mult= mult*valor;
        result=mult;
    }while(valor!=0);
     }

      int main()
       {
        int op,result;

        printf("Informe a operacao desejada soma(1) ou produto (2):");
        scanf("%d", &op);


    if(op==1){
        soma();
    }
    else if(op==2){
        mult();
    }

        printf("O resultado foi: %d", result);
      return 0;
       }
  • The program only does two operations multiplication and sum?

  • Yeah just that

2 answers

4


Your code is picking up trash. You need to pass data from one function to another. The variable result that exists within the main() is not the same variable within the other functions. In general the variables are local. There’s even a global variable that could solve this, but it’s the wrong way to do it in almost 100% of situations. Don’t even think about it.

I took the opportunity to organize the code, it is easier to identify problems and I got some other small problems that can pick up garbage also in other situations, besides cosmetic changes.

#include <stdio.h>

int soma(void) {
    int valor = 0, soma = 0;
    printf("Foi escolhida a soma:\n\n");
    do {
        printf("Informe os valores desejados e 0 (zero) para concluir:");
        scanf("%d", &valor);
        soma += valor;
    } while (valor != 0);
    return soma;
}

int mult(void) {
    int valor = 0, mult = 1;
    printf("Foi escolhida a multiplicacao:\n\n");
    do {
        printf("Informe os valores desejados e 0 (zero) para concluir:");
        scanf("%d", &valor);
        if (valor == 0) break;
        mult *= valor;
    } while (valor != 0);
    return mult;
}

int main() {
    int op = 0, result;
    printf("Informe a operacao desejada soma(1) ou produto (2):");
    scanf("%d", &op);
    if (op == 1) result = soma();
    else if (op == 2) result = mult();
    printf("O resultado foi: %d", result);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • To repeat the question and add the possibility to exit 3, what could you do?

  • To maintain the standard, I would choose 0 to exit. Just put a loop in the main() as you did within the other functions. If you have difficulty doing this, open another question with this subject that we answered for you.

  • Thanks! Thanks for the help! has how to "enjoy"?

  • This is not a social network, so you can’t enjoy it. You can vote for both, as I think you already have. And you can choose only one as the one that was most useful to you.

2

As its two functions perform calculation you must specify that it returns a certain value.

See the function soma():

 int soma(void)
 {
    int valor, soma, result;
    soma = 0;
    printf("Foi escolhida a soma:\n\n");
    do
    {
        printf("Informe os valores desejados e 0 (zero) para concluir:");
        scanf("%d", &valor);
        soma= soma+valor;
    }
    while(valor!=0);

    return soma;
}

And the function mult():

int mult(void)
{
    int valor, mult,result;
    mult= 1;
    printf("Foi escolhida a multiplicacao:\n\n");
    do{
        printf("Informe os valores desejados e 0 (zero) para concluir:");
        scanf("%d", &valor);
        if(valor==0)
        {
            break;
        }
        mult= mult*valor;
    }
    while(valor!=0);

    return mult;
}

Now the implementation of the functions:

int main()
{
    int op,result;

    printf("Informe a operacao desejada soma(1) ou produto (2):");
    scanf("%d", &op);

    if(op==1)
    {
        result = soma();
    }
    else if(op==2)
    {
        result = mult();
    }

    printf("O resultado foi: %d", result);
    return 0;
}

Always remember to define the type of return according to the data you are working on.

Browser other questions tagged

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