Warning

 

Close
Confirm Action

Are you sure you wish to do this?

Cancel Confirm
AR15.COM
1/25/2008 1:01:55 PM EDT
I have to create a function to convert miles to kilometers. But being out of school for almost 9 months due to the army has got me a bit lost on my code. Can someone help me with my code and tell me if it is wrong or not? Thanks

#include <iostream>
#include <cmath>

using namespace std;

double milestokilometers (double mi);

int main ()

{
double mi;

cout << "Enter miles: ";
cin >> mi;
cout << endl;
cout << milestokilometers (mi) << endl;

return 0;
}

double milestokilometers (double mi)
{

double kilometers;
kilometers = 1.6093 * mi;
return kilometers;
}
1/25/2008 3:05:11 PM EDT
[#1]
been a while since I've done any coding, but I think that will work
1/25/2008 3:59:31 PM EDT
[#2]
Yeah I just tested it. It compiles and runs. I have to add to it but at least the basic formula works. For some reason I could get it to compile but not run at school so i just wanted to make sure. Thanks for the help.
1/26/2008 2:58:57 PM EDT
[#3]
No.. you need to make an object call Covert and the method will be milestokilometers()
Then you create an instance of the convert class and pass it the miles, then print the results.

You way is too procedural, which is SOO 1960s.  Object Orientated is the 1980s.. err the future!




Quoted:
I have to create a function to convert miles to kilometers. But being out of school for almost 9 months due to the army has got me a bit lost on my code. Can someone help me with my code and tell me if it is wrong or not? Thanks

#include <iostream>
#include <cmath>

using namespace std;

double milestokilometers (double mi);

int main ()

{
double mi;

cout << "Enter miles: ";
cin >> mi;
cout << endl;
cout << milestokilometers (mi) << endl;

return 0;
}

double milestokilometers (double mi)
{

double kilometers;
kilometers = 1.6093 * mi;
return kilometers;
}


N
1/26/2008 5:00:03 PM EDT
[#4]

Quoted:
No.. you need to make an object call Covert and the method will be milestokilometers()
Then you create an instance of the convert class and pass it the miles, then print the results.

You way is too procedural, which is SOO 1960s.  Object Orientated is the 1980s.. err the future!




Quoted:
I have to create a function to convert miles to kilometers. But being out of school for almost 9 months due to the army has got me a bit lost on my code. Can someone help me with my code and tell me if it is wrong or not? Thanks

#include <iostream>
#include <cmath>

using namespace std;

double milestokilometers (double mi);

int main ()

{
double mi;

cout << "Enter miles: ";
cin >> mi;
cout << endl;
cout << milestokilometers (mi) << endl;

return 0;
}

double milestokilometers (double mi)
{

double kilometers;
kilometers = 1.6093 * mi;
return kilometers;
}


N


Bah! In school you really are writing throw-away code.  Who cares about maintenance?
1/26/2008 11:15:47 PM EDT
[#5]

Quoted:
Bah! In school you really are writing throw-away code.  Who cares about maintenance?


Some people just don't appreciate dumb answers.
1/27/2008 8:31:29 AM EDT
[#6]

Quoted:
No.. you need to make an object call Covert and the method will be milestokilometers()
Then you create an instance of the convert class and pass it the miles, then print the results.

You way is too procedural, which is SOO 1960s.  Object Orientated is the 1980s.. err the future!


No no no.  Now you're tightly coupling your code at compile time.  Ever hear of dependency injection?

First, define an interface (pure virtual base class in C++ terms) called IUnitConverter.  It has a convert() method that takes a double and returns a double.  Then you make a MilesToKmUnitConverterFactory class with a static getConverter() method that reads from a config file at run time to determine which IUnitConverter implementation to use.  You write at least one implementation, MiltsToKmUnitConverter, which implements IUnitConvert's convert() method to convert miles to KM.  Finally, you code your caller app to get an IUnitConverter instance from MilesToKmUnitConverterFactory so you can call the convert() method and get your answer.

Trust me, this approach is _ way_ better.  I read about it in a Java API document once...

1/29/2008 5:36:22 PM EDT
[#7]

Quoted:

Quoted:
No.. you need to make an object call Covert and the method will be milestokilometers()
Then you create an instance of the convert class and pass it the miles, then print the results.

You way is too procedural, which is SOO 1960s.  Object Orientated is the 1980s.. err the future!


No no no.  Now you're tightly coupling your code at compile time.  Ever hear of dependency injection?

First, define an interface (pure virtual base class in C++ terms) called IUnitConverter.  It has a convert() method that takes a double and returns a double.  Then you make a MilesToKmUnitConverterFactory class with a static getConverter() method that reads from a config file at run time to determine which IUnitConverter implementation to use.  You write at least one implementation, MiltsToKmUnitConverter, which implements IUnitConvert's convert() method to convert miles to KM.  Finally, you code your caller app to get an IUnitConverter instance from MilesToKmUnitConverterFactory so you can call the convert() method and get your answer.

Trust me, this approach is _ way_ better.  I read about it in a Java API document once...





And, of course, you'll be using XML for the config file. Sheesh, doesn't anyone believe in reuse anymore?