Create your own
Lesson illustration

Building a Temperature-Conversion Program

You now have the complete interaction pattern for a useful console program: prompt with std::cout, read a value with std::cin, store it in a variable, and display a result. This lesson combines those pieces with arithmetic and numerical formatting to build a temperature converter.

We will create a program that accepts a Celsius temperature, calculates its Fahrenheit equivalent, and displays both values clearly to two decimal places. It is a small project, but it introduces a crucial programming habit: translating a real-world formula into an accurate C++ expression.


From a formula to C++ arithmetic

The Celsius-to-Fahrenheit conversion formula is:

Here, is the input temperature in Celsius and is the calculated Fahrenheit temperature.

In C++, multiplication uses * and division uses /, so the formula becomes:

fahrenheit = celsius * 9.0 / 5.0 + 32.0;

There are two variables because they represent different pieces of information:

double celsius {};
double fahrenheit {};

Use double, not int, because temperatures often have fractional values. For example, 21.5 Celsius is a perfectly reasonable input, and its Fahrenheit equivalent is 70.7.

The .0 in 9.0, 5.0, and 32.0 matters. It marks these as floating-point values, so C++ performs decimal division.

Consider this expression:

(9 / 5) * celsius

Because both 9 and 5 are integers, C++ performs integer division first. The result of 9 / 5 is 1, not 1.8. That would make the conversion wrong.

By writing:

(9.0 / 5.0) * celsius

the division produces 1.8, as intended.

For a concise walkthrough of the core program, watch Portfolio Courses’ Convert A Temperature From Celsius To Fahrenheit | C++ Example.

Convert A Temperature From Celsius To Fahrenheit | C++ Example

Watch this short video to see the complete input, calculation, and output pattern assembled in one C++ program. Pay particular attention to why the variables use double and why the conversion factors contain decimal points.

Watch variables and input to review the storage and prompt steps. Then watch the conversion formula, focusing on the integer-division mistake that 9.0 / 5.0 avoids. Finish with the output to see the result displayed after the calculation.


Planning the program before typing it

A program runs its statements from top to bottom. For this project, that means:

  1. Create variables to hold the entered Celsius value and the calculated Fahrenheit value.
  2. Ask the user for a Celsius temperature.
  3. Read the input into celsius.
  4. Calculate and store the Fahrenheit value.
  5. Display a labeled result.

This sequence is worth tracing with a concrete input. Suppose the user enters 21.5.

fahrenheit = celsius * 9.0 / 5.0 + 32.0;

C++ substitutes the value stored in celsius, then evaluates the arithmetic:

The final value, 70.7, is assigned to fahrenheit.

The equals sign in this C++ statement is assignment: calculate the expression on the right, then store the result in the variable on the left.

fahrenheit = celsius * 9.0 / 5.0 + 32.0;

It does not mean that the two sides were previously equal. It tells the program to update fahrenheit.


Formatting decimal output

A conversion can produce many decimal places. For a user-facing result, two decimal places are usually easier to read:

21.50 C is 70.70 F

C++ provides formatting tools in the <iomanip> header. Add this line alongside <iostream>:

#include <iomanip>

Then use these two formatting manipulators before displaying a double:

std::cout << std::fixed << std::setprecision(2);

They work together:

CodeEffect
std::fixedDisplays a decimal value in ordinary fixed-point notation.
std::setprecision(2)Shows exactly two digits after the decimal point when used with std::fixed.

For example, if a double stores 70.7, this formatting displays it as 70.70.

Importantly, formatting changes only how the number is displayed. It does not alter the value stored in the variable.


Build: Celsius-to-Fahrenheit converter

Open the Visual Studio console project you have used in earlier lessons. Replace the contents of your .cpp file with this program:

#include <iostream>
#include <iomanip>

int main()
{
    double celsius {};
    double fahrenheit {};

    std::cout << "Enter a temperature in Celsius: ";
    std::cin >> celsius;

    fahrenheit = celsius * 9.0 / 5.0 + 32.0;

    std::cout << std::fixed << std::setprecision(2);

    std::cout << "\n--- Temperature Conversion ---\n";
    std::cout << celsius << " C is " << fahrenheit << " F\n";

    return 0;
}

Save with Ctrl+S, then run without debugging with Ctrl+F5.

Try entering 21.5. You should see:

Enter a temperature in Celsius: 21.5

--- Temperature Conversion ---
21.50 C is 70.70 F
A Windows console application prompts for an arithmetic expression and prints a calculated result. Your temperature converter follows the same input-process-output structure: it accepts a value, performs arithmetic, and reports a labeled result.

Read the program in groups rather than treating it as one large block:

double celsius {};
double fahrenheit {};

These declarations reserve storage for the input and calculated result.

std::cout << "Enter a temperature in Celsius: ";
std::cin >> celsius;

These lines conduct the input conversation. The program explains what it needs, waits for keyboard input, and stores the entered decimal value.

fahrenheit = celsius * 9.0 / 5.0 + 32.0;

This is the processing step. It uses the mathematical formula to transform the input into a new value.

std::cout << celsius << " C is " << fahrenheit << " F\n";

This is formatted output: it combines variables, labels, spaces, and a newline into one readable sentence.


Check that the calculation is trustworthy

A program that builds successfully is not automatically correct. Use a few known values to check both the formula and the formatting.

Celsius inputExpected Fahrenheit output
032.00
100212.00
-40-40.00
3798.60
21.570.70

The first two values are especially useful reference points: water freezes at 0 Celsius and boils at 100 Celsius under ordinary conditions. The -40 test is also valuable because Celsius and Fahrenheit have the same value at that temperature.

If your program prints a result such as 53.50 F for an input of 21.5, inspect the conversion expression first. A likely error is this:

fahrenheit = (9 / 5) * celsius + 32;

Because 9 / 5 becomes 1, the program would calculate:

Correct it by using floating-point literals:

fahrenheit = (9.0 / 5.0) * celsius + 32.0;

Also check that #include <iomanip> is present if Visual Studio reports that it does not recognize std::setprecision.


A small extension: convert Fahrenheit to Celsius

Once the Celsius-to-Fahrenheit version works, you can create a second version that performs the reverse conversion. The formula is:

Its C++ expression is:

celsius = (fahrenheit - 32.0) * 5.0 / 9.0;

The parentheses are important. They ensure that the program subtracts 32.0 from the Fahrenheit input before multiplying and dividing.

A Fahrenheit-to-Celsius version would change the prompt, calculation, and result line:

std::cout << "Enter a temperature in Fahrenheit: ";
std::cin >> fahrenheit;

celsius = (fahrenheit - 32.0) * 5.0 / 9.0;

std::cout << fahrenheit << " F is " << celsius << " C\n";

Test it with 32 Fahrenheit. The program should display 0.00 C.

For now, keep each version as a straightforward program with one clearly defined conversion. Later in the course, you will learn decisions that let one program ask which conversion the user wants.


Key takeaways

  • A temperature-conversion program follows the input-process-output pattern: read a value, calculate a result, then display it.
  • Use double for temperatures because decimal values are common.
  • Celsius-to-Fahrenheit conversion uses:
  • Write decimal conversion factors such as 9.0 / 5.0 to prevent accidental integer division.
  • std::fixed and std::setprecision(2), from <iomanip>, display decimal results consistently to two places.
  • Clear prompts and labeled output make a small console program much easier to use and verify.

This completes the first module: you can now create a Windows console project and build programs that use variables, keyboard input, arithmetic, and formatted output. Next, you will examine arithmetic expressions more carefully, including the precedence rules that determine which parts of a calculation C++ evaluates first.

Can't find a good explanation? Sign up and we'll make it for you

Sign up