calling a function within another function in c

Asked

Viewed 5,472 times

1

I need to receive values float to store in a struct, however, these values must be validated by means of a validation function.`

The function that reads and validates these values is working correctly, but when I try to call it within the function that will store within the structaccuses: "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’t see where I’m going wrong, the function lerfloatthis duly defined in headLer.h.

ps.:the function calls read vector pq it reads coordinates of a vector. ps2.: I already tried to put the prototype of lerfloatin func_lerVetorand the error continued.

  • The lerfloat class has been compiled?

  • Do you say if it worked properly? If so, yes, I tested it before implementing func_lerVetor and it was working properly.

  • Using any IDE? or doing everything by linux, command line, etc?

  • Ta all in project or this selecting and giving build?

  • everything is in project.

2 answers

1

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

The correct would be to have another file .c with the method within.

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 archives .c should all be compiled to have the function within you, so you can make the call.

If you use gcc:

gcc *.c -o program.exe

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

  • The lerfloat is in a file . c, that . h from the beginning was just to see if that was not what was missing. I will also put the file . h in the question.

  • @soAna how are you compiling your code? is using any IDE?

  • I believe so, I use code Blocks.

  • try to play all methods for a single file .c, it may be that your IDE is not compiling the entire project

  • it did not work to put them in a single . c too

  • could be some logic error, upload the project and pass the link

  • Actually, I just found the mistake. There was a test file in the project folder (but not included in the project) that was confusing the compiler. Sorry...

  • hassle-free..

Show 3 more comments

0

Probably by the error obtained, the compiler is not finding its function, to solve this you need to declare the function prototype.

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

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

  • Sorry, I forgot to mention in the question, but I’ve tried that too...

  • The file "headLer. h" is in the same folder as the file . c ?

  • Where did you declare the prototype? The correct would be just below the includes, outside any function.

  • yes, this. in a folder, and I declared the prototype just below the includes and above the struct, and also I tried just below the struct and it did not work.

Browser other questions tagged

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