We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Calling functions in beginning C++

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;
}
3
Contributors
5
Replies
27 Minutes
Discussion Span
2 Years Ago
Last Updated
6
Views
Question
Answered
figment56
Newbie Poster
5 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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.

griswolf
Veteran Poster
1,176 posts since Apr 2010
Reputation Points: 344
Solved Threads: 262
Skill Endorsements: 1

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??

frogboy77
Posting Pro in Training
481 posts since Aug 2010
Reputation Points: 100
Solved Threads: 39
Skill Endorsements: 1

Thank you.

figment56
Newbie Poster
5 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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.

frogboy77
Posting Pro in Training
481 posts since Aug 2010
Reputation Points: 100
Solved Threads: 39
Skill Endorsements: 1

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

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

figment56
Newbie Poster
5 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 2 Years Ago by frogboy77 and griswolf

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0842 seconds using 2.84MB