Evening Everyone:

I'm a bit of a newie with C++, and can't determine where to "declare" the functions in the following code. The compiler is stating I haven't defined the functions, but I've followed the instructors directions, and am now stuck. YAY! Thanks so much!

#include <iostream>

using namespace std;

int main( ) // calls readmiles & calcKM
{
    int numMiles = 0;
    double distKilo = 0.0;
    numMiles = readmiles( );
    calcKM(numMiles);
    cout << "Total miles in kilometers is :   " << distKilo << endl;
    system("pause"); 
    return 0;
}

int readmiles( ) //inputs number of miles
{
    cout <<"Enter number of miles:  ";
    cin >> numMiles;
    return numMiles;
}

double calcKM(int numMiles) // calculates miles into kilometers
{
    distKilo = numMiles * 1.67;
    return distKilo;
}

Recommended Answers

All 5 Replies

Step One: Please in future post code using the (CODE) button, which gives lots of good effect for very very little effort. (Please also look for the 'thread is solved' link at the bottom and after the thread is really solved be sure to mark it solved: That, too, gives good effect for very little effort.

The compiler sees thing in the order they are shown in the file. If main() needs to call readmiles() then readmiles() needs to be at least declared (if not defined) before main is seen. For this particular program, just re-order things. For larger programs, you will learn to create a header file that declares functions, classes, etc; then #include that header before the code that uses the declared "things". The body of the function (the implementation, also called definition) can be seen later as long as the linker can find it somewhere.

or

#include <iostream>

using namespace std;
int readmiles( );
double calcKM(int numMiles);

int main( ) // calls readmiles & calcKM
{
int numMiles = 0;
double distKilo = 0.0;
numMiles = readmiles( );
calcKM(numMiles);
cout << "Total miles in kilometers is : " << distKilo << endl;
system("pause"); 
return 0;
}

int readmiles( ) //inputs number of miles
{
cout <<"Enter number of miles: ";
cin >> numMiles;
return numMiles;
}

double calcKM(int numMiles) // calculates miles into kilometers
{
distKilo = numMiles * 1.67;
return distKilo;
}

I think this would work. I don't know wether you would need to use void with readmiles??

Thank you.

I think on line 27 distKilo is undeclared in that scope. It is not global(and i wouldn't recommend that) so on line 13 it will always output0.0

more like

cout << "Total miles in kilometers is : " << calcKM(numMiles) << endl;

remove line 10
and declare distKilo as a double in your function.

I think on line 27 distKilo is undeclared in that scope.

Thank you. I will mark my question as solved. It worked!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.