A simple C ++ error: "... uneclared (use this function first)"

Hey guys, I'm working on my first C ++ program for school. For some reason, I get the following error when trying to compile it:

`truncate' undeclared (first use this function)

Full source code:

#include <iostream>
#include <math.h>

using namespace std;

#define CENTIMETERS_IN_INCH 2.54
#define POUNDS_IN_KILOGRAM 2.2

int main() {
    double feet, inches, centimeters, weight_in_kg, weight_in_lbs;

    // get height in feet and inches
    cout << "Enter height (feet): ";
    cin >> feet;
    cout << "Enter (inches): ";
    cin >> inches;

    // convert feet and inches into centimeters
    centimeters = ((12 * feet) + inches) * CENTIMETERS_IN_INCH;

    // round 2 decimal places and truncate
    centimeters = truncate(centimeters);

    printf("Someone that is %g' %g\" would be %g cm tall", feet, inches, centimeters);

    // weights for bmi of 18.5
    weight_in_kg = truncate(18.5 * centimeters);
    weight_in_lbs = round(weight_in_kg * POUNDS_IN_KILOGRAM);

    printf("18.5 BMI would correspond to about %g kg or %g lbs", weight_in_kg,   weight_in_lbs);

    // weights for bmi of 25
    weight_in_kg = truncate(25 * centimeters);
    weight_in_lbs = round(weight_in_kg * POUNDS_IN_KILOGRAM);

    printf("25.0 BMI would correspond to about %g kg or %g lbs", weight_in_kg, weight_in_lbs);

    // pause output
    cin >> feet;

    return 0;
}

// round result
double round(double d) {
   return floor(d + 0.5);
}

// round and truncate to 1 decimal place
double truncate(double d) {
   return round(double * 10) / 10;
}

Any help would be greatly appreciated. Thanks.

+3
source share
3 answers

You need to send a declaration before main:

double truncate(double d);
double round(double d);

You can simply define your functions before main, which will also solve the problem:

#include <iostream>
#include <math.h>

using namespace std;

#define CENTIMETERS_IN_INCH 2.54
#define POUNDS_IN_KILOGRAM 2.2

// round result
double round(double d) {
   return floor(d + 0.5);
}

// round and truncate to 1 decimal place
double truncate(double d) {
   return round(double * 10) / 10;
}

int main() {
...
}
+10
source

You are trying to call a function truncate()at:

centimeters = truncate(centimeters);

You have not yet told the compiler what this function is, so it is undefined and the compiler objects.

++ ( ) . , ++, . , ++, .

, POSIX truncate() - , ; , .


- - truncate() round(). , . , .

+3

You need to declare functions before using them. Moving definitions over truncateand roundover a function mainshould do the trick.

+2
source

Source: https://habr.com/ru/post/1716436/


All Articles