cplusplus
cplusplus

 

Introduction

In today’s world, we often need to convert units from one system to another, whether it be for length, weight, or volume.
For instance, we might need to convert inches to centimeters, kilograms to pounds, or liters to gallons.
A unit converter program simplifies this task by automating the conversion process. In this C++ program, we will develop
a tool to convert units between different categories such as length, weight, and volume.

Objective

The goal of this program is to create a simple C++ application that can convert units of length, weight, and volume.
The user will select the type of conversion and then input the value to be converted. The program will then output
the result in the target unit.

Code

#include 
using namespace std;

// Function prototypes for conversion
double convertLength(double value, int fromUnit, int toUnit);
double convertWeight(double value, int fromUnit, int toUnit);
double convertVolume(double value, int fromUnit, int toUnit);

int main() {
    int choice, fromUnit, toUnit;
    double value;

    // Main menu
    cout << "Unit Converter Program" << endl;
    cout << "1. Length (cm, m, km, inch, ft)" << endl;
    cout << "2. Weight (kg, lb, oz)" << endl;
    cout << "3. Volume (ml, l, gallon, pint)" << endl;
    cout << "Enter your choice (1-3): "; cin >> choice;

    // Choose conversion type
    switch (choice) {
        case 1: // Length Conversion
            cout << "Enter the value to convert: "; cin >> value;
            cout << "Enter the unit to convert from (0: cm, 1: m, 2: km, 3: inch, 4: ft): "; cin >> fromUnit;
            cout << "Enter the unit to convert to (0: cm, 1: m, 2: km, 3: inch, 4: ft): "; cin >> toUnit;
            cout << "Converted value: " << convertLength(value, fromUnit, toUnit) << endl;
            break;

        case 2: // Weight Conversion
            cout << "Enter the value to convert: "; cin >> value;
            cout << "Enter the unit to convert from (0: kg, 1: lb, 2: oz): "; cin >> fromUnit;
            cout << "Enter the unit to convert to (0: kg, 1: lb, 2: oz): "; cin >> toUnit;
            cout << "Converted value: " << convertWeight(value, fromUnit, toUnit) << endl;
            break;

        case 3: // Volume Conversion
            cout << "Enter the value to convert: "; cin >> value;
            cout << "Enter the unit to convert from (0: ml, 1: l, 2: gallon, 3: pint): "; cin >> fromUnit;
            cout << "Enter the unit to convert to (0: ml, 1: l, 2: gallon, 3: pint): "; cin >> toUnit;
            cout << "Converted value: " << convertVolume(value, fromUnit, toUnit) << endl;
            break;

        default:
            cout << "Invalid choice!" << endl;
    }

    return 0;
}

// Length conversion function
double convertLength(double value, int fromUnit, int toUnit) {
    // Conversion rates for length
    double conversionTable[5][5] = {
        {1.0, 0.01, 0.00001, 0.393701, 0.0328084}, // From cm
        {100.0, 1.0, 0.001, 39.3701, 3.28084},      // From m
        {100000.0, 1000.0, 1.0, 3937.01, 3280.84},   // From km
        {2.54, 0.0254, 0.0000254, 1.0, 0.0833333},   // From inch
        {30.48, 0.3048, 0.0003048, 12.0, 1.0}        // From ft
    };

    return value * conversionTable[fromUnit][toUnit];
}

// Weight conversion function
double convertWeight(double value, int fromUnit, int toUnit) {
    // Conversion rates for weight
    double conversionTable[3][3] = {
        {1.0, 2.20462, 35.274}, // From kg
        {0.453592, 1.0, 16.0},  // From lb
        {0.0283495, 0.0625, 1.0} // From oz
    };

    return value * conversionTable[fromUnit][toUnit];
}

// Volume conversion function
double convertVolume(double value, int fromUnit, int toUnit) {
    // Conversion rates for volume
    double conversionTable[4][4] = {
        {1.0, 0.001, 0.000264172, 0.00211338}, // From ml
        {1000.0, 1.0, 0.264172, 2.11338},      // From l
        {3785.41, 3.78541, 1.0, 8.32069},      // From gallon
        {568.261, 0.568261, 0.125, 1.0}         // From pint
    };

    return value * conversionTable[fromUnit][toUnit];
}

Explanation of the Program

The program begins by displaying a menu to the user, offering three categories: length, weight, and volume.
The user selects the type of conversion they want, and then they are prompted to enter the value to convert,
along with the source and target units. The program uses pre-defined conversion tables for each category,
which are implemented as 2D arrays. Each table contains conversion factors between different units.

Based on the user’s choices, the corresponding conversion function is called, and the value is multiplied by
the appropriate conversion factor. The result is then displayed to the user.

How to Run the Program

1. Copy the code into a C++ compiler or an Integrated Development Environment (IDE) like Code::Blocks,
Visual Studio, or an online C++ compiler.

2. Compile the code to generate an executable.

3. Run the program. Follow the prompts to select the type of conversion, and enter the value and units for conversion.

© 2024 Learn Programming

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

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

error

Enjoy this blog? Please spread the word :)