How to assign a treated value in the function to my vector?

Asked

Viewed 78 times

1

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define TAM_Min 1
#define TAM_Max 500

/*
Sintese
Objetivo: Classificar animais de um zoologico segundo seu peso
Entrada: Numeros de animais do zoologico, e para cada animal o numero de identificacao e o peso em gramas
Saída: Identificacao, peso do animal mais pesado, a identificacao e o peso do animal mais leve, e a quantidade
de animais de peso menor que o peso medio de todos os animais

*/

int le_valida_codigo_animal(int cod[], char titulo[]);
int le_valida_inteiro(int ValorMin, int ValorMax, char texto[]);


int main() {
    int i, num_animais, cod_animal[TAM_Max];
    float peso_animal[TAM_Max];

    //validar numero de animais
    printf("#--- Cadastro de animais ---#");
    num_animais =le_valida_inteiro(TAM_Min, TAM_Max, "\nDigite o numero de animais do zoologico:");

    for(i=1;i<=num_animais;i++){

        cod_animal[i] = le_valida_codigo_animal(cod_animal[i], "Digite o codigo de identificacao dos animais:");


        printf("Digite o peso (em gramas) do animal:");
        scanf("%d", &peso_animal[i]);
        system("cls");
    }

    return 0;
}

int le_valida_inteiro(int ValorMin, int ValorMax, char texto[]){
    int num_animais=0;
    do{

        printf(texto);
        scanf("%d", &num_animais);

        if(num_animais<ValorMin||num_animais >ValorMax){
            printf("\nValor invalido.\nDigite um numero ate 500!");
        }

    }while(num_animais<ValorMin||num_animais >ValorMax);
    return num_animais;
}

int le_valida_codigo_animal(int cod[], char titulo[]){

    char cod_an[0];

    do{
        printf(titulo);
        scanf("%d", &cod);
        if(cod == '\0'){
            printf("O codigo deve nao pode ser vazio!");
        }

    }while(cod == '\0');
}

Error received

C: Users Lain Desktop c.c In Function 'main': 32 43 C: Users Lain Desktop c.c [Warning] Passing argument 1 of 'le_valida_animal code' makes Pointer from integer without a cast 18 5 C: Users Lain Desktop c.c [Note] expected 'int *' but argument is of type 'int' C: Users Lain Desktop c.c In Function 'le_valida_codigo_animal': C: Program Files Dev-Cpp Mingw64 x86_64-W64-mingw32 bin Ld.exe Reopening C: Users Lain Desktop c.exe: Permission denied

  • Missed to put what is the error or problem you are facing.

  • Inside the for, that vector receiving the value validated by the function. See error: C: Users Lain Desktop c.c In Function 'main': 32 43 C: Users Lain Desktop c.c [Warning] Passing argument 1 of 'le_valida_codigo_animal' makes Pointer from integer without a cast 18 5 C: Users Lain Desktop c.c [Note] expected 'int *' but argument is of type 'int' C: Users Lain Desktop c.c In Function 'le_valida_animal code': C: Program Files Dev-Cpp Mingw64 x86_64-W64-mingw32 bin Ld.exe Reopening C: Users Lain Desktop c.exe: Permission denied

  • Sorry I’m not in the mood to explain. This is the first time I’ve asked a question here in the stack.

1 answer

2


I’ll give you an upgrade on your code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define TAM_MIN 1
#define TAM_MAX 500

/*
Síntese
Objetivo: Classificar animais de um zoológico segundo seu peso.
Entrada: Números de animais do zoológico, e para cada animal,
         o número de identificação e o peso em gramas.
Saída: A identificação e peso do animal mais pesado, a identificação
       e o peso do animal mais leve, e a quantidade de animais de
       peso menor que o peso médio de todos os animais.
*/

int le_valida_codigo_animal(char *titulo);
int le_valida_inteiro(int valor_min, int valor_max, char *texto);

int main() {
    int cod_animal[TAM_MAX];
    float peso_animal[TAM_MAX];

    printf("#--- Cadastro de animais ---#");
    int num_animais = le_valida_inteiro(TAM_MIN, TAM_MAX, "\nDigite o número de animais do zoológico:");

    for (int i = 1; i <= num_animais; i++) {

        cod_animal[i] = le_valida_codigo_animal("Digite o codigo de identificacao dos animais:");

        printf("Digite o peso (em gramas) do animal:");
        scanf("%f", &peso_animal[i]);
        system("cls");
    }

    return 0;
}

int le_valida_inteiro(int valor_min, int valor_max, char *texto) {
    int num_animais = 0;

    while (1) {
        printf("%s", texto);
        scanf("%d", &num_animais);

        if (num_animais >= valor_min && num_animais <= valor_max) {
            return num_animais;
        }
        printf("\nValor invalido.\nDigite um número entre %d e %d!", valor_min, valor_max);    
    }
}

int le_valida_codigo_animal(char *titulo) {
    while (1) {
        int cod = 0;
        printf("%s", titulo);
        scanf("%d", cod);
        if (cod != 0) return cod;
        printf("O codigo deve nao pode ser vazio!");
    }
}

The main problem is that its function le_valida_codigo_animal was defined incorrectly. int cod[] as a parameter is equivalent to int *cod. When you use it then scanf("%d", &cod);, it occurs that cod is already a pointer (i.e., a memory address), and therefore the correct one would be scanf("%d", cod);. When using &cod, you give the address of a pointer, ie a pointer to pointer, and this will not be what you want.

However, it occurs that you want to return the code in le_valida_codigo_animal, then there is no reason to receive it as a parameter.

As to printf(texto);, best is to use printf("%s", texto); because this way you ensure that the text will be reproduced even if it contains things that could be confused with string formatters. For example, imagine a scenario that the texto were "Concluido 50% ou mais". In this scenario, use the printf(texto); I’d go for this one "%" would be mistaken as part of the format string. The solution is therefore to use printf("%s", texto);.

  • Wow, bro! Thanks so much for your explanations. I’ll finish writing and code and if any questions arise I’ll be here. I was already thinking about doing something else with my life... hahah

  • @Lucascorrhea If this answer solved your problem and there is no doubt left, mark it as correct/accepted by clicking on the " " that is next to it, which also marks your question as solved. If you still have any questions or would like further clarification, feel free to comment. :)

  • The only problem is that you used pointers. My teacher forbade their use before we saw the subject in class. Anyway, your help gave me courage to study.

  • @Lucascorrhea So use as char [], that is, with the array notation. In fact, in C, pointers and arrays almost always give in the same thing (there are very few places where there is any difference).

  • Ok. I’ll do what you suggested and mark the answer as correct there. Thanks! I’ll be back as soon as I have new questions.

Browser other questions tagged

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