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

2

I need a program that adds and multiplies as the user chooses and then select the corresponding function that will read the values until the user types 0 and the program generates the result. My program for any value typed is reporting 36. And another, if I wanted to repeat the process (calculate), and give the option to exit, would I have to use one of while ? but when do 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;
       }
    
asked by anonymous 18.10.2015 / 01:00

2 answers

3

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

I used to organize the code, it is easier to identify problems and I have fixed some other small problems that can take garbage in other situations as well as 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);
    return 0;
}

See working on ideone .

    
18.10.2015 / 01:24
2

Since your two functions perform a calculation you must specify that it returns a certain value.

See the soma() function:

 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 mult () function:

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 set the return type according to the data you are working on.

    
18.10.2015 / 01:18