0

i got this error message

pls help

$ make triangle
clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow    triangle.c  -lcrypt -lcs50 -lm -o triangle
triangle.c:24:5: error: function definition is not allowed here
    {
    ^
1 error generated.
make: *** [<builtin>: triangle] Error 1

my code that i wrote

#include <cs50.h>
#include <stdio.h>
#include <string.h>


bool valid_triangle(float x, float y, float z);
int main(void)
{

    float a,b,c;

    a = get_float("first side: ");
    b = get_float("second side: ");
    c = get_float("third side: ");

    bool result = valid_triangle(a,b,c);

    printf("the triangle is %d\n", result);



    bool valid_triangle(float x, float y, float z)
    {
         if(x <= 0 || y<=0 || z <= 0)
        {
            return false;
        }

        if((x + y <= z)|| (z + y <= x)|| (z + x <= y))
        {
            return false;

        }

        return true;
    }





}
0

This is because you have declared a function prototype ( bool valid_triangle(float x, float y, float z);) and then did not declare the function as the compiler was expecting. Instead of writing your valid_triangle function inside of your main function, it should be after it. C does not support nested functions, so it will declare an error that "a function definition is not allowed here." Here is an example of how it should look:

int main(void)
{
    //code for main function
}

bool valid_triangle(parameters)
{
    //code for secondary function
}

Because you have everything inside of your main function, the compiler hits the new function declaration inside of main and declares that there is an error: essentially, "what is valid_triangle?"

The compiler is not smart, so you and I both know where it is, but it is not where we told the compiler it would be (after main). Instead, it is in an unsupported place. Move the function to match the order I show above and that should resolve this issue.

Hope this helps. If it does, you can accept this answer by clicking on the check mark beside it. But if it doesn't, let me know and I will try to help further.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .