I’m having a problem with my code I can’t bring the data from one function to another!

Asked

Viewed 44 times

-3

I’m having a problem with my code I can’t bring the data from one function to another!!

in the case of Function sum I cannot print it in function print.

Can someone help me

1 - Insert into a vector 2 - Sum vector values 3 - Print values and sum 4 - Quit

At any time you can request printout of the data, do not print out memory junk At any time you can request the sum, do not add unread values yet


#include <stdio.h>
#define TAM 5
int vetor[TAM];
int menu(){
    int opcao;
    printf("1 - Inserir\n");
    printf("2 - Somar valores\n");
    printf("3 - Imprimir\n");
    printf("4 - Sair\n");
    scanf("%d", &opcao);
    return opcao;
}
//----------------------------------------------------INSERINDO OS DADOS
int inserir(int i){ 
    int vt;
    for (i = 0; i < TAM; i++){
        scanf("%d", &vetor[i]);
    }
    return vt;
}
//-----------------------------------------------------------------------
//------------------------------------------------------------------SOMAR
int somar(int i){
    int soma=0, total;
    for (i=0; i<TAM; i++){
        soma=soma+vetor[i];
    }
    total = soma;
    printf("\n");
    return total;
}
//-----------------------------------------------------------------------

//----------------------------------------------------IMPRIMINDO OS DADOS
void imprimir(int i){
    printf("Imprimindo elementos\n");
    for (int i=0; i<TAM; i++){
        printf("   %d   ", vetor[i]);
    }
    
    printf("%d\n", total);   //IMPRIMIR DADO DA SOMA <----------------
    printf("\n");
}
//-----------------------------------------------------------------------

int main(){
    int op = 0, i=0;
    i++;
    while(op != 4){
        op = menu();
        switch (op){
            case 1:
                inserir(i);
                break;
            case 2:
                somar(i);
                break;
            case 3:
                imprimir(i);
                break;
            case 4:
                printf("Saindo");
                break;
        }
    }
    return 0;
}


1 answer

2

In your code I found 2 errors, which were:

  • Do not call the add function within the print function
  • Do not declare TOTAL variable

The fixes were basically what I quoted above and their code worked

    void imprimir(int i){
    int total;
    printf("Imprimindo elementos\n");
    for (int i=0; i<TAM; i++){
        printf("   %d   ", vetor[i]);
    }
    total = somar(i);
    printf("%d\n", total);   //IMPRIMIR DADO DA SOMA <----------------
    printf("\n");
}

Since there was no exchange of value between the summing function and the printing function, memory garbage was always printed. So, calling the function, passing by parameter anything, it returned the sum correctly.

  • Perfectly, a thousand pardons

  • Mario if you want to leave a message making yourself available for help use the comments.

Browser other questions tagged

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