calling a function inside another function in c

1

I need to get float values to store in struct , but I need to validate these values through a validation function. '

The function that validates these values is working correctly, but when I try to call it inside the function it will store inside the struct it states: "undefined reference to ' lerfloat '".

The function lerfloat :

float lerfloat()
{
    float f;
    char c;
    int ret;

    do
    {
        puts("Insira um numero: \n");
        ret=scanf("%f",&f);
        fflush(stdin);

        if(ret==1)
        {
            return f;
        }
        else{
            puts("Digite apenas numeros por favor.\n");}

    }while(ret!=1);
    return f;
}

The function to store the values:

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

#include "headLer.h"


struct armazenar
{
    float x;
    float y;
};

float func_lerVetor(float vet_pontos[][2], int n)
{
    struct armazenar p;
    int i;

    for(i=0;i<n;i++)
    {
        p.x= lerfloat();
        vet_pontos[i][0]=p.x;
        p.y= lerfloat();
        vet_pontos[i][1]=p.y;
    }
    return 0;
}

headLer.h

#ifndef FUNCLERN_H_INCLUDED
#define FUNCLERN_H_INCLUDED

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

int func_lerN();
float lerfloat();
float func_lerVetor();


#endif // FUNCLERN_H_INCLUDED

I can not see where I'm going wrong, the lerfloat function is properly set to headLer.h .

ps.:a function calls read vector pq it reads coordinates of a vector. ps2: I already tried to put the prototype of lerfloat into func_lerVetor and the error continued.

    
asked by anonymous 01.12.2016 / 01:26

2 answers

1

The .h files serve only as a reference for methods, structures, and values, creating a method within a header file can cause serious problems in the future.

The correct would be another file .c with the method inside.

headLer.h:

#pragma once // faz com que o arquivo não seja incluso mais de uma vez

float lerfloat();

headLer.c:

float lerfloat(){
    float f;
    char c;
    int ret;

    do
    {
        puts("Insira um numero: \n");
        ret=scanf("%f",&f);
        fflush(stdin);

        if(ret==1)
        {
            return f;
        }
        else{
            puts("Digite apenas numeros por favor.\n");}

    }while(ret!=1);
    return f;
}

func_lerVetor.c:

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

#include "headLer.h"


struct armazenar
{
    float x;
    float y;
};

float func_lerVetor(float vet_pontos[][2], int n)
{
    struct armazenar p;
    int i;

    for(i=0;i<n;i++)
    {
        p.x= lerfloat();
        vet_pontos[i][0]=p.x;
        p.y= lerfloat();
        vet_pontos[i][1]=p.y;
    }
    return 0;
}

The .c files must all be compiled to have the function inside them, so you can make the call.

If you use gcc:

gcc *.c -o program.exe
  

Note: It should have the function main , otherwise your code will not be compiled.

    
02.12.2016 / 05:26
0

Probably because of the error you get, the compiler is not finding its function, to resolve this you need to declare the function prototype .

  

According to the wiki: "If the function is not pre-declared and its name occurs in some expression followed by a parenthesis (" ("), it is implicitly declared as a function with no argument returning an int."

In your case, it would just be: float lerFloat();

    
01.12.2016 / 01:39