Coders Packet

Unit Converter & BMI Using C++

By Anshuman Prakash

This is a c++ program that convert units of lenght, weight, volume, time, force and also can calculate BMI

In this tutorial, I have created a simple yet log program in C++ that converts a particular unit in other units and also has a BMI calculator. It has perimeters to convert units of length, weight, volume, time, force.

It works in a very simple and straight forward manner.

First, it displays the units to be converted and asks the user to select the unit by entering a number and stores the input in variable 'a'.

int a, n;
cout<<"Select the unit type to be converted\n";
cout<<"1-Length\n2-Weight\n3-Volume\n4-Time\n5-Force\n6-BMI Calculator\n\nEnter Input : ";
cin>>a;

Then according to the selected unit, it enters in an if-else conditional statement and then proceeds with the input unit type.

Then again it asks the user to input a specific unit of selected unit type. A very basic idea used in the calculation in every unit calculation is that it converts whatever unit type user inputs and converts it into a standard unit.

For example, All the units that the user input for length conversion is converted to ‘meter’.

if(a==1){
        cout<<"\nEnter the unit type of data available :\n\n";
        cout<<"1-Millimeter\n2-centimeter\n3-Decimeter\n4-Meter\n5-Decameter\n6-Hectometer\n7-Kilometer\n";
        cout<<"8-Inch\n9-Foot\n10-Mile\n";
        cout<<"Enter Input : ";
        cin>>n;
        float m;
        if(n==1){
            float mm; cout<<"Enter Millimeters : ";cin>>mm; m=mm/1000; //cout<<m;
        }
        else if(n==2){
            float cm; cout<<"Enter Centimeters : "; cin>>cm; m=cm/100; //cout<<m;
        }
        else if(n==3){
            float dm; cout<<"Enter Decimeters : "; cin>>dm; m=dm/10; //cout<<m;
        }
        else if(n==4){
            cout<<"Enter Meters : "; cin>>m; //cout<<m;
        }
        else if(n==5){
            float dam; cout<<"Enter Decameters : "; cin>>dam; m=dam*10; //cout<<m;
        }
        else if(n==6){
            float hm; cout<<"Enter Hectometers : "; cin>>hm; m=hm*100; //cout<<m;
        }
        else if(n==7){
            float km; cout<<"Enter Kilometers : "; cin>>km; m=km*1000; //cout<<m;
        }
        else if(n==8){
            float in; cout<<"Enter Inches : "; cin>>in; m=(in*2.54)/100; //cout<<m;
        }
        else if(n==9){
            float ft; cout<<"Enter Feets : "; cin>>ft; m=(ft*0.3048); //cout<<m;
        }
        else if(n==11){
            float mile; cout<<"Enter Miles : "; cin>>mile; m=(mile*1609.34); //cout<<m;
        }
        else{
            cout<<"\nInvalid Input";
        }
cout<<"\nMillimeters : "<<(long long int)m*1000<<"\nCentimeter : "<<(long long int)m*100<<"\nDecimeter : "<<(long long int)m*10<<"\nMeter : "<<m<<"\nDecameter : "<<m/10;
cout<<"\nHectometer : "<<m/100<<"\nKilometer : "<<m/1000<<"\nInch : "<<(m*100)/2.54<<"\nFoot : "<<(m/0.3048)<<"\nMile : "<<(m/1609.34)<<"\n";
    }

Here i have used unit 'float' so that the decimal values don't get neglected.

Then that standard unit value is converted to every unit using proper conversion formulas.

This program also calculates BMI (Body Mass Index), where it asks the user to input height in meters and weight in Kg. And prints the range of BMI values so the user can compare his BMI status

//for bmi calculation
    else if(a==6){
        float height, mass, bmi;
        cout<<"Enter Height in Meters :";
        cin>>height;
        cout<<"Enter Mass in Kilograms :";
        cin>>mass;
        bmi=mass/(height*height);
        printf("\nBMI : %0.2f",bmi);

        cout<<"\n\nSevere Thinness : <16\nModerate Thinness : 16 - 17\nMild Thinness : 17 - 18.5\nNormal : 18.5 - 25\n";
        cout<<"Overweight : 25 - 30\nObese Class I : 30 - 35\nObese Class II : 35 - 40\nObese Class III : >40\n";
    }

Also, this program outputs an error message if users input the wrong value since a conditional statement is used.

else{
cout<<"Invalid Option Selected\n";
}

Download Complete Code

Comments

No comments yet