Calling functions

I can't seem to get the hang of this. Can anyone tell me what my problem is?
[code]
// Lab010Ex1.cpp
Showdon wrote:
I can't seem to get the hang of this. Can anyone tell me what my problem is?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Lab010Ex1.cpp � Calculate child tax credit with 3 functions
// Created by Man-Chi Leung on 10/27/10

#include <iostream>

using namespace std;

double readMiles();
double calcKm();

int main(double kilo)
{
    readMiles();
    calcKm();

   cout<< "The distance in kilometers is: " << kilo << endl;

   system("pause");
   return 0;
}

double readMiles()
{
    double miles=0;

    cout<<"What is the distance in miles: ";
    cin>> miles;
    return miles;
}

double calcKm(double miles)
{
    double kilo=0;

    kilo=miles*1.67;
    return kilo;
}


Compiler Error:
obj\Debug\main.o(.text+0x132)||In function `main':|
C:\Users\Showdon\Desktop\C++\Lab10\lab1004\main.cpp|14|undefined reference to `calcKm()'|
||=== Build finished: 1 errors, 0 warnings ===|

Line 9 declares a function prototype that doesn't take a double as parameter as opposed to function definition found in line 31 which does. End result: The body of double calcKm() is nowhere to be found.
Your code is invalid in whole. As for the error message then you did not define function double calcKm(); that is the definition double calcKm(double miles) does not corresponds to the declaration. The declaration has no any parameter.
Last edited on
Thanks webJose, but I'm pretty new to programming, anyway you can dumb it up a little for me?
Last edited on
main() doesn't know about the functions that are declared & defined after it. You have to forward declare the functions before main, which he tried to do but did incorrectly. The forward declarations should look like this:

1
2
double readMiles();
double calcKm(double miles);


As you can see, there's no information about what they do just that they exist and this is what parameters they take and return. If you put those definitions before the main function, it will now understand what they are and it will use them correctly, even though you don't define what they actually do until after main().

The less-good alternative would be to define those functions in their entirety before the main() function.
Last edited on
Excellent! changed it just like you asked and it worked. Heres the code for anyone that comes across the same issue.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Lab010Ex1.cpp – Calculate child tax credit with 3 functions
// Created by Man-Chi Leung on 10/27/10

#include <iostream>

using namespace std;

double readMiles();
double calcKm(double miles);

int main()
{
    double kilo=0.0, miles=0.0;
    miles=readMiles();
    kilo=calcKm(miles);

   cout<< "The distance in kilometers is: " << kilo << endl;

   system("pause");
   return 0;
}

double readMiles()
{
    double miles=0;

    cout<<"What is the distance in miles: ";
    cin>> miles;
    return miles;
}

double calcKm(double miles)
{
    double kilo=0;

    kilo=miles*1.67;
    return kilo;
}
Except that the conversion from miles to km is multiply by 1.609 not 1.67 :D
Topic archived. No new replies allowed.