0

Error returned at line 27 (line beginning with "charges") stating "undeclared reference (for the calculateCharge, to my best guess) and compiler notes says "ld returned 1 exit status" I can't for the life of me get what need to change.

float calculateCharge(float);

int main()
{
  printf("Hello world!\n");

  int car;
  int num_cars;
  float total_charges = 0;
  float total_hours = 0;

  printf("How many cars?\n\n");      //prompt
  scanf("%d", &num_cars);            //prompt

  float hours [num_cars + 1];        //declaring parallel arrays
  float charges [num_cars + 1];

  for (car=1; car<=num_cars; car++)
  {
      printf("How many hours for car #%d?", car);     //prompt
      scanf("%f", &hours[car]);                       //input hours
      charges [car] = calculateCharge(hours [car]);
      total_charges = total_charges + charges [car];
      total_hours = total_hours + hours [car];
  }
  printf("%s\t%s\t%s\t", "Car", "Hours", "Charge");

  for (car = 1; car <=num_cars; car++)
  {
       printf("\n%d\t%.2f\t%.2f\n", car, hours[car], charges[car]);
  }

printf("\n%s\t%.2f\t%.2f\n", "Total", total_hours, total_charges);

return 0;
}
alk
  • 69,737
  • 10
  • 105
  • 255
  • 5
    Using a forced base-one indexing on your arrays will only get seasoned coders frustrated, and newbie coders confused. Please don't do it, use "normal" zero-base like everyone else. – Some programmer dude May 30 '16 at 18:04
  • 3
    As for your problem, where do you implement (*define*) the `calculateCharge` function? You *do* implement it somewhere? – Some programmer dude May 30 '16 at 18:05
  • 3
    You have a *declaration* for `calculateCharge()`. But where's the definition of `calculateCharge()`? – P.P May 30 '16 at 18:05
  • 1
    In addition to what others have said,, this is a linker error, not a compile error. There's nothing syntactically wrong with your code. The linker is basically saying, "You're calling this `calculateCharge` function, but I have no idea where the code for it is". You'll need to create a function body for `calculateCharge` or not use it. – yano May 30 '16 at 18:15
  • Thanks for the prompt replies all. I'm an electrical engineer by trade studying for an IT degree. I now understand that my declaration does no good with the definition. Any ideas on how to work this out? Not asking for the answer, just a nudge in the right direction. Cheers all. =) – Vernon Lemmon May 30 '16 at 18:15
  • http://www.tutorialspoint.com/cprogramming/c_functions.htm – yano May 30 '16 at 18:16
  • 1
    Woops. I see from my notes I left out the function entirely. Thanks again everyone! Got it working =) – Vernon Lemmon May 30 '16 at 18:17
  • 1
    float calculateCharge (float hours) { float charge; hours = ceil(hours); if (hours <=3) { charge = 2.00; }else { charge = 2.00 + (hours - 3) * 0.50; if (charge > 10.00) { charge = 10.00; } } return charge; – Vernon Lemmon May 30 '16 at 18:18
  • You could very well answer to your own question and even accept this answer. – alk May 31 '16 at 07:27

1 Answers1

3

Assuming this is the entirety of your code, you have provided a forward declaration of what the signature of calculateCharge is, so the compiler can generate the code to call it (once it knows where the actual definition is), but you haven't provided that actual definition.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101