-2

Ive tried a lot of things and I cant seem to find out why my functions are not being detected? I understand that some of the code may be wrong I just started but I don't get why the functions wont be read to the main? Ive used functions in previous programs but I used integers. Im not sure if its cause im trying to use floats but I just cant get it to work.

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



float amountoffence(float chain, float link, float width, float length)
{
    float howmuchfence;

    howmuchfence=length+length+width+width;

    return howmuchfence;
}

float costoffence(float link, float post, float gate, float chain, howmuchfence)
{
    float fencecost;

    link=howmuchfence*chain;
    fencecost = link+post+gate;

    return fencecost;
}

float amountofgrass(float length, float width)
{
    float grass;
    grass = length*width;

    return grass;
}

float costofgrass(float grass)
{
    float grassprice;

    grassprice=grass*64.25;

    return grassprice;
}



int main()
{
    float grass, length, width, height, post, chain, gate, size, link, grassprice, total, gst, totaltax, howmuchfence, costoffence, fencecost, amountoffence, amountofgrass, costofgrass;


printf("Please note that all measurements are in meters \n \n \n");
    printf("Please enter the length of the base (Minimum length is 2 and Maximum is 5) \n");
    scanf("%f", &length);
    printf("Please enter the width of the base (Minimum width is 1 and Maximum is 2) \n");
    scanf("%f", &width);
    printf("Do you have a large dog or small dog (Type 1 for large or 2 for small)");
    scanf("%f", &size);

    if(size == 1)
    {
        chain=8.10;
        gate=165;
        post=82;
    }
    else
    {
        chain=12.45;
        gate=148;
        post=66;
    }
    fencecost = costoffence(link, post, gate);

    grass = amountofgrass(length, width);

    howmuchfence = amountoffence(chain, link, width);

    grassprice = costofgrass(grass);



    total=fencecost+grassprice;
    gst=5;
    totaltax=total*(gst/100);




    printf("--Customized Dog Run Recipt--\n");
    printf("Length of dog run: %0.02f m\n", length);
    printf("Width of dog run: %0.02f m\n", width);
    printf("Height of dog run: %0.02f m\n", height);
    printf("Amount of fence: %0.02f m\n", howmuchfence);
    printf("Amount of gates: 1\n");
    printf("Cost of fence: $ %0.02f\n", fencecost);
    printf("Amount of grass: %0.02f m^2\n", grass);
    printf("Cost of grass: $ %0.02f\n", grassprice);
    printf("Total cost before GST: $ %0.02f\n", total);
    printf("Amount of GST: %0.02f percent \n", gst);
    printf("Total cost after GST: $ %0.02f \n", totaltax);




    return 0;
}
  • 1
    All of these thing you are trying to call as a function are defined as local `float` *variables* in `main`. – Eugene Sh. Nov 13 '19 at 17:37

2 Answers2

4

When I compile your program with GCC, I get several error messages.

fence.c: At top level:
fence.c:15:68: error: unknown type name ‘howmuchfence’
 float costoffence(float link, float post, float gate, float chain, howmuchfence)
                                                                    ^~~~~~~~~~~~

You forgot the data type, probably float howmuchfence.

The main problem is this error and some similar ones:

fence.c: In function ‘main’:
fence.c:69:17: error: called object ‘costoffence’ is not a function or function pointer
     fencecost = costoffence(link, post, gate);
                 ^~~~~~~~~~~
fence.c:46:120: note: declared here
 , gate, size, link, grassprice, total, gst, totaltax, howmuchfence, costoffence, fencecost, amountoffence, amountofgrass, costofgrass;
                                                                     ^~~~~~~~~~~

Here the compiler complains that costoffence is not a function because you defined a variable of the same name at the beginning of function main which shadows the corresponding function. GCC even tells you where you declared the identifier that is not a function.

If you enable warnings the compiler will also warn you about some unused function parameters.

Bodo
  • 9,287
  • 1
  • 13
  • 29
0

Found out why. It's because the variables for the functions were also in the main as stated variables. Thought I needed to do that but turns out you CAN'T do that.

Simson
  • 3,373
  • 2
  • 24
  • 38
  • If you "Thought you needed to do that" you should read about scope and shadowing in C. See e.g. https://stackoverflow.com/a/623279/10622916 and https://softwareengineering.stackexchange.com/a/306799 and https://en.wikipedia.org/wiki/Variable_shadowing (Even if the title is "Variable shadowing", this applies to other tidentifiers as well.) – Bodo Nov 14 '19 at 09:06