Open In App

Temperature Converter in C++

Last Updated : 16 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The Temperature Converter is a simple command-line application that allows users to convert temperatures between two temperature units i.e. Celsius and Fahrenheit.

Features

Our temperature converter program will have the following features:

  • Convert Celsius to Fahrenheit
  • Convert Fahrenheit to Celsius
  • Validate user input for menu selection
  • Keep running until the user chooses to exit

Project Requirements

You must be familiar with the following topics in C++:

  • Variable, Data Types and Constants
  • Basic input/output
  • Operators
  • Decision Making and Loops

You’ll also need a basic C++ compiler and a text editor or IDE.

Implementation

We start by defining constexpr constants for the conversion factors.

Then, we use a while (true) loop to keep the converter running. Inside the loop, we display a menu and take user input. Based on the input, the appropriate conversion is performed. Input is validated to handle invalid menu choices.

C++
#include <iostream>
using namespace std;

// Define conversion factors
constexpr double CEL_TO_FAH_MULT =
    9.0 / 5.0;
constexpr double FAH_TO_CEL_MULT =
    5.0 / 9.0;
constexpr int FAH_OFFSET = 32;

int main() {
    int choice;
    double temp, converted;

    while (true) {
        cout << "\n--- Temperature Converter "
             << "---\n";
        cout << "1. Celsius to Fahrenheit\n";
        cout << "2. Fahrenheit to Celsius\n";
        cout << "3. Exit\n";
        cout << "Choose (1-3): ";
        cin >> choice;

        if (choice == 3) {
            cout << "Exiting temperature "
                 << "converter.\n";
            break;
        }

        if (choice == 1) {
            cout << "Enter temperature in "
                 << "Celsius: ";
            cin >> temp;
            converted = (temp *
                CEL_TO_FAH_MULT)
                + FAH_OFFSET;
            cout << temp << " C = "
                 << converted
                 << " F\n";
        }
        else if (choice == 2) {
            cout << "Enter temperature in "
                 << "Fahrenheit: ";
            cin >> temp;
            converted = (temp -
                FAH_OFFSET)
                * FAH_TO_CEL_MULT;
            cout << temp << " F = "
                 << converted
                 << " C\n";
        }
        else {
            cout << "Invalid choice. "
                 << "Try again.\n";
        }
    }

    return 0;
}

Execution

When you compile and run the program, it displays:

Summary

In this project, we learned:

  • How to create a simple temperature converter using cin and cout
  • How to use constexpr to define conversion factors
  • How to use conditional statements to handle menu choices
  • How to use a while loop to allow multiple conversions until exit

You can also extend this program to support kelvin (K) also.


Next Article
Article Tags :
Practice Tags :

Similar Reads