Temperature Converter in C++
Last Updated : 19 Jul, 2025
In this project, you will build a simple command-line application that allows users to convert temperatures between Celsius and Fahrenheit. The program should take the user’s choice, perform the appropriate conversion, and display the result. It should continue running based on the user's input and handle invalid choices appropriately, as described in the features below.
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
Example:
C++ Temperature Converter 1. Celsius to Fahrenheit 2. Fahrenheit to Celsius 3. Exit Enter your choice (1-3): 1 Enter temperature in Celsius: 25 25 C = 77 F Temperature Converter 1. Celsius to Fahrenheit 2. Fahrenheit to Celsius 3. Exit Enter your choice (1-3): 2 Enter temperature in Fahrenheit: 98.6 98.6 F = 37 C Temperature Converter 1. Celsius to Fahrenheit 2. Fahrenheit to Celsius 3. Exit Enter your choice (1-3): 4 Invalid choice. Please try again. Temperature Converter 1. Celsius to Fahrenheit 2. Fahrenheit to Celsius 3. Exit Enter your choice (1-3): 3 Exiting temperature converter.
Try to solve this yourself before looking at the solution below.
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
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 constants const double celToFahMult = 9.0 / 5.0; const double fahToCelMult = 5.0 / 9.0; const int fahOffset = 32; int main() { int choice; double temp, converted; // Repeat until user chooses to exit while (true) { // Display menu cout << "\nTemperature Converter\n"; cout << "1. Celsius to Fahrenheit\n"; cout << "2. Fahrenheit to Celsius\n"; cout << "3. Exit\n"; cout << "Enter your choice (1-3): "; cin >> choice; // Exit condition if (choice == 3) { cout << "Exiting temperature converter.\n"; break; } // Celsius to Fahrenheit if (choice == 1) { cout << "Enter temperature in Celsius: "; cin >> temp; converted = (temp * celToFahMult) + fahOffset; cout << temp << " C = " << converted << " F\n"; } // Fahrenheit to Celsius else if (choice == 2) { cout << "Enter temperature in Fahrenheit: "; cin >> temp; converted = (temp - fahOffset) * fahToCelMult; cout << temp << " F = " << converted << " C\n"; } // Invalid choice else { cout << "Invalid choice. Please 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.
Explore
C++ Basics
Core Concepts
OOP in C++
Standard Template Library(STL)
Practice & Problems
My Profile