Converting units

How can i use the same variable like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  double km, yard, mile;
  //like this below
    meter=100;
    km=meter*0.001;
    yard=meter*1.094;
    mile=meter*0.0006215;
  
    meter=200;
    km=meter*0.001;
    yard=meter*1.094;
    mile=meter*0.0006215;


You could either use an array or two different variables:
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
//Array
    double km[2], yard[2], mile[2];
  
    meter[0] = 100;
    km[0] = meter[0] * 0.001;
    yard[0] = meter[0] * 1.094;
    mile[0] = meter[0] * 0.0006215;
  
    meter[1] = 200;
    km[1] = meter[1] * 0.001;
    yard[1] = meter[1] * 1.094;
    mile[1] = meter[1] * 0.0006215;

//Different Variables
    double km_1, yard_1, mile_1, km_2, yard_2, mile_2;

    meter_1 = 100;
    km_1 = meter_1 * 0.001;
    yard_1 = meter_1 * 1.094;
    mile_1 = meter_1 * 0.0006215;
  
    meter_2 = 200;
    km_2 = meter_2 * 0.001;
    yard_2 = meter_2 * 1.094;
    mile_2 = meter_2 * 0.0006215;


Also note that the two examples above should be used if need to use the two variables at the same time for different thing. Your example works fine if you just need to use it once and then change the values.

Hope that helps
Last edited on
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
#include <iostream>
using namespace std;

const double METRE_TO_KM   = 0.001;
const double METRE_TO_YARD = 1.094;
const double METRE_TO_MILE = 0.0006215;


void lengthConvert( double metre, double &km, double &yard, double &mile )
{
   km   = metre * METRE_TO_KM;
   yard = metre * METRE_TO_YARD;
   mile = metre * METRE_TO_MILE;
}


int main()
{
   double metreArray[] = { 100, 200 };

   for ( double metre : metreArray )
   {
      double km, yard, mile;
      lengthConvert( metre, km, yard, mile );
      cout << metre << " m   = " << km << " km    = " << yard << " yards   = " << mile << " miles" << '\n';
   }
}


100 m   = 0.1 km    = 109.4 yards   = 0.06215 miles
200 m   = 0.2 km    = 218.8 yards   = 0.1243 miles
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;



int main()
{
  double km, yard, mile, meter;
  //like this below
    meter=100;
    km=meter*0.001;
    yard=meter*1.094;
    mile=meter*0.0006215;
  cout << "meter" << " km" << "  yard" << "    mile" << endl;
  cout << meter << "   " << km << " " << yard << "  " << mile << endl;  
  
    meter=200;
    km=meter*0.001;
    yard=meter*1.094;
    mile=meter*0.0006215;

 cout << meter << "   " << km << " " << yard << "  " << mile << endl;  

return 0;
Better was to use functions for km, yard, mile.
Probably define macro will work here.

-------------------------------------------------------------------------------
I am an expert and manager of https://www.homeworkhelp4u.com/ tutoring service. We have 18 years of experience helping students to write code, learn computer science, engineering, physics, math, calculus, statistics and other subjects. Our experts are familiar with courses taught in United States, Canada, Australia and UK universities and we produce example solutions that exactly meet all requirements. We have huge database of existing solutions collected from different sources. Feel free to contact me I can find existing solution of your problem or examples for you. (We do not publish solutions we wrote for our clients, but we can do original work for you) Our service is confidential we do not collect customer information. Live help available 7 days week and 24 hours a day with small break between 8pm-1am EST. We answer short homework questions from our existing clients free of charge.
Last edited on
No. Do not use a macro. This is not C. Just write a function called km_to_yard() or whatever. It's okay to use functions.
something like
#define CONVERT km=meter*0.001; yard=meter*1.094;mile=meter*0.0006215;
It's should work. this is pure c
@codehelper, that will work fine until you want to put the results in variables of other names; for example array elements:
km[i] = metre[i] * 0.001;
or related names:
km_Manchester_London = metre_Manchester_London * 0.001;
at which point a macro - pure text substitution - becomes worthless.

Use a function: it is much more flexible and reusable; a macro isn't either of those.
Last edited on
@lastchance
Thank you for your clarification. I know about limitations and i suggested to use functions in my first post here. But initial question was ambigous. So i suggested additional possible options.
Using functions have some disadvantages. (more code required, slower execution speed) function is prefereble from fundamental theory point. (easy code modification)
Topic archived. No new replies allowed.