0

I'm new to c, and for my assignment we have to calculate how much money to charge using a function. However, I keep getting "undefined reference to `Calculate_charge'" whenever I try to call the function. what exactly am I doing wrong here?

#include <stdio.h>

double Calculate_charge(double y);

int main(void)
{

    int customer;
    double charge = 0;
    int kwh;
    double totalcharge;
    int totalkwh;
    int totalcustomer;
    unsigned int count;

    customer = 0;
    kwh= 0;
    totalcharge = 0;
    totalkwh = 0;
    totalcustomer = 0;
    count = 0;

    printf( "%s","Enter customer number (-1 to quit):\n");
    scanf("%d", &customer);
    printf("%s", "Enter kwh used (-1 to quit):\n");
    scanf("%d", &kwh);

    while (customer != -1 && kwh != -1) {
        Calculate_charge(charge);
        printf( "Customer Num: %d\n", customer);
        printf("KWH used: %d\n", kwh);
        printf("Charge: %.2f\n", charge);
        count++;
        totalcustomer++;
        totalcharge = totalcharge + charge;
        totalkwh = totalkwh + kwh;

        printf( "%s","Enter customer number (-1 to quit):\n");
        scanf("%d", &customer);
        printf("%s", "Enter kwh used (-1 to quit):\n");
        scanf("%d", &kwh);  
        }

    double Calculate_charge(double y)
    {
        if (kwh <= 300) {
            y = .09 * kwh;  
        }
        else if (kwh > 300 &&  kwh <= 600){
            y = .08 * kwh;
        }
        else if (kwh > 600 && kwh <= 1000){
            y = .06 *kwh;
        }
        else {
            y = .05 * kwh;
        }
        return y;
    }

    if (count != 0) {

        printf("\n");
        printf("Total customers:%d\n", totalcustomer);
        printf("Total KWH used: %d\n", totalkwh);
        printf("Total Charged: %.2f" , totalcharge);
    }
}
tadman
  • 208,517
  • 23
  • 234
  • 262
  • Can you copy/paste the exact error message you get? – Code-Apprentice Mar 01 '19 at 23:49
  • The exact error message is: (.text+0x94): undefined reference to `Calculate_charge' – spjpiv117 Mar 01 '19 at 23:56
  • 1
    You've nested the code for `Calculate_charge()` inside `main()`. That is ***not*** standard C, though GCC recognizes nested functions if you're careful. (The error message means you're not being careful enough.) I neither know nor care what the rules are — nested functions are not C. (If I want Algol, or Pascal, I'll use them.) . You can find the docs at [Nested Functions](https://gcc.gnu.org/onlinedocs/gcc-8.3.0/gcc/Nested-Functions.html#Nested-Functions). You'd do best to move the function definition outside `main()`. – Jonathan Leffler Mar 01 '19 at 23:56

1 Answers1

1

You're using the function before it's declared. Either forward declare it and declare it after or move the declaration to before main().

What you've done in your code is forward declare it and then declare it inside main() which is not the correct place for that code.

It should be outside of the main function.

The easiest fix here is to eliminate the forward declaration and declare the function right there instead:

#include <stdio.h>

double Calculate_charge(double y)
{
    if (kwh <= 300) {
        y = .09 * kwh;  
    }
    else if (kwh > 300 &&  kwh <= 600){
        y = .08 * kwh;
    }
    else if (kwh > 600 && kwh <= 1000){
        y = .06 *kwh;
    }
    else {
        y = .05 * kwh;
    }
    return y;
}

int main() {
  // ...

  return 0;
}
tadman
  • 208,517
  • 23
  • 234
  • 262
  • Can you clarify what you mean by "declare the function right there instead"? I had to read this multiple times to understand what you are trying to say and I have some experience with C++. I expect that to a beginner it might be unclear. – Code-Apprentice Mar 01 '19 at 23:51
  • @Code-Apprentice As in move the declaration that's presently in the middle of `main()` to the exact spot where the forward declaration is right now. Added an example for clarification. – tadman Mar 01 '19 at 23:52
  • 2
    The `Calculate_charge()` function is _defined_ inside `main()` — there'd be a semicolon after the prototype line if it was just a declaration. So, the code in the question is using GCC's [nested functions](https://gcc.gnu.org/onlinedocs/gcc-8.3.0/gcc/Nested-Functions.html#Nested-Functions) (mis)feature — probably unwittingly. – Jonathan Leffler Mar 01 '19 at 23:59