Gas Consumption Converter

If you are like me, you enjoy watching car reviews, but get annoyed hearing gas consumption you can’t understand; MPG or L/100km. Here is a simple program I wrote in C++ solving this issue. Feel free to use it.
Enjoy 😉

Preview:

1 – MPG -> L/100km
2 – L/100km -> MPG
0 – exit
1 or 2?

The source code:

#include<iostream>

using namespace std;

//making the MPG to L/100km conversion
float MtoL (float value)
{
return 100/((value*1.609344)/3.78541178);
}

//making the L/100km to MPG conversion
float LtoM (float value)
{
return 62.1371192/(value*0.264172052);
}

//round up the result to two digits
float round (float value)
{
return ((int)(value * 100 + .5) / 100.0);
}

int main() {

float value;

char menu = 0;

while (menu != 0)
{
cout << “1 – MPG -> L/100km” <<endl;
cout << “2 – L/100km -> MPG” << endl;
cout << “0 – exit” << endl;
char menu;
cout << “1 or 2? “;
cin >> menu;
getchar();

switch (menu)
{ case ‘1’:
cout << endl<< “MPG -> L/100km” << endl << “Value: “;
cin >> value;
cout << endl;
cout << value << ” MPG in L/100km: ” << round(MtoL(value)) << endl;
cout << endl;
cout << endl;
break;

case ‘2’:
cout << endl << “L/100km -> MPG” << endl << “Value: “;
cin >> value;
cout << endl;
cout << value << ” L/100km in MPG: ” << round(LtoM(value)) << endl;
cout << endl;
cout << endl;
break;

case ‘0’:
return 0;
break;
}
}

return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.