C++ Tutorial

C++ Project: Temperature Converter

Convert Celsius, Fahrenheit, and Kelvin with a small set of functions and formatted output.

What you will build

Temperature conversion is a handful of formulas. Each formula lives in a function so you do not retype the arithmetic in every print. Celsius, Fahrenheit, and Kelvin all appear. Output uses<iomanip> so columns line up and each value shows one digit after the decimal.

Sample temperatures are literals. Open Try it in C++ and compile at/cpp/try. Leave Python and HTML editors closed for this file.

Celsius to Fahrenheit and Kelvin

Fahrenheit is c * 9 / 5 + 32. Kelvin is c + 273.15. Write both as functions that take and return double. Integer 9 / 5 would be 1 in C++; use 9.0 and 5.0.

Example

#include <iostream>
using namespace std;

double cToF(double c) {
  return c * 9.0 / 5.0 + 32.0;
}

double cToK(double c) {
  return c + 273.15;
}

int main() {
  double c = 20.0;
  cout << c << " C" << endl;
  cout << cToF(c) << " F" << endl;
  cout << cToK(c) << " K" << endl;
  return 0;
}

20 C is 68 F and 293.15 K. Change 20 to 0 to see the freezing point of water on each scale.

The other direction

Going back to Celsius is the inverse formula. Keep those in functions too, even if main starts from Celsius. You will need them when a later input is already Fahrenheit or Kelvin.

Example

#include <iostream>
using namespace std;

double fToC(double f) {
  return (f - 32.0) * 5.0 / 9.0;
}

double kToC(double k) {
  return k - 273.15;
}

int main() {
  cout << fToC(68.0) << endl;
  cout << kToC(293.15) << endl;
  return 0;
}

Both lines should print 20, or a value extremely close to 20 given binary floating point.

Formatted columns

fixed plus setprecision(1) prints one digit after the decimal.setw sets a minimum field width so a table does not wobble when 9 becomes 100. Include<iomanip>.

Example

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
  cout << fixed << setprecision(1);
  cout << setw(8) << 20.0 << setw(8) << 68.0 << setw(8) << 293.2 << endl;
  cout << setw(8) << 100.0 << setw(8) << 212.0 << setw(8) << 373.2 << endl;
  return 0;
}

Compile this table at /cpp/try with Try it in C++. If the columns look left aligned, you still have fixed; setw applies only to the next value, so call it before every number.

Complete converter

Print a header, then convert a small list of Celsius values. Each row is C, F, and K. The list is avector<double> so you can add a temperature with one push_back.

Example

#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;

double cToF(double c) {
  return c * 9.0 / 5.0 + 32.0;
}

double cToK(double c) {
  return c + 273.15;
}

void printRow(double c) {
  cout << fixed << setprecision(1);
  cout << setw(10) << c
       << setw(12) << cToF(c)
       << setw(12) << cToK(c) << endl;
}

int main() {
  vector<double> samples;
  samples.push_back(-40.0);
  samples.push_back(0.0);
  samples.push_back(20.0);
  samples.push_back(37.0);
  samples.push_back(100.0);

  cout << setw(10) << "C" << setw(12) << "F" << setw(12) << "K" << endl;
  for (double c : samples) {
    printRow(c);
  }
  return 0;
}

Minus 40 is the point where C and F match. 37 is typical human body temperature. 100 is boiling water at standard pressure. Read the F and K columns and confirm the functions, not a mental chart.

Common mistakes

  • Writing 9 / 5 with integers. That is 1. Then 20 C becomes 52 F. Use 9.0 / 5.0.
  • Adding 32 before multiplying. Parentheses matter: (f - 32.0) * 5.0 / 9.0.
  • Calling setprecision once and expecting setw to stick. Width resets after each value. Precision stays until you change it.
  • Treating Kelvin as Fahrenheit plus 273. Convert to Celsius first, or use the C-to-K function onfToC(f).

Practice

  1. Add fToK that reuses fToC and cToK instead of a new formula.
  2. Print two digits after the decimal for a science-style table.
  3. Convert a hardcoded Fahrenheit value of 98.6 to C and K and print both on one line.

Next project: tic-tac-toe on a 3x3 board with a winner check.

FAQ: C++ Project: Temperature Converter

Common questions about this page.

What is the StudyGrid C++ tutorial?

The StudyGrid C++ tutorial is a full beginner-to-advanced track: syntax, types, input, loops, functions, classes, the STL, templates, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run c++ temperature project examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn c++ temperature project in this C++ C++ lesson (C++ Project: Temperature Converter).

Is the C++ editor the same as Try Python or Try HTML?

No. Try C++ compiles with g++ at /cpp/try and shows stdout plus compiler messages. Try Python stays at /try. Try HTML stays at /html/try. C++ lessons never open those editors.

Do I need to install a compiler to learn C++?

No. Open a chapter, click Try it in C++, and compile in the browser. You can also download a .cpp file and compile locally with g++.

Where should I start the C++ tutorial?

Start at C++ Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open C++ Examples, then templates, map, and lambdas. Use Next at the bottom of each chapter.

Is the C++ tutorial free?

Yes. The C++ workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.