Lesson 39 +25 XP

Project 2: Temperature Converter

Project 2: Temperature Converter

You know how to read input with cin - now put it to work. Build a program that asks for a temperature in Celsius, converts it to Fahrenheit, and prints a clean result.

The goal

Ask the user for a Celsius temperature, convert it with (c * 9) / 5 + 32, and print the Fahrenheit answer as a double.

What you practice

  • Reading user input with cin >>
  • The conversion formula (c * 9) / 5 + 32
  • Storing fractional numbers in double
  • A little input validation - refuse temperatures below absolute zero

Starter code

Here is a compile-able start:

#include <iostream>
using namespace std;

int main() {
  double celsius = 0.0;
  cout << "Enter temperature in Celsius: ";
  cin >> celsius;
  double fahrenheit = (celsius * 9) / 5 + 32;
  cout << "Fahrenheit: " << fahrenheit << endl;
  return 0;
}

Step-by-step

  1. Run the starter and type 20. Confirm 68 prints.
  2. Add validation: if celsius < -273.15, print That is colder than absolute zero. and stop.
  3. For cleaner output, #include <iomanip> and print with cout << fixed << setprecision(1) << fahrenheit;.
  4. Try extreme inputs like -300 and 0 and check the program stays sensible.
  5. Re-run after every change and confirm the results match the math.

Checklist

  • [ ] cin >> celsius reads the user's number
  • [ ] The formula (c * 9) / 5 + 32 is used
  • [ ] The result is stored and printed as a double
  • [ ] Values below -273.15 are rejected
  • [ ] The printout uses fixed and setprecision(1)

TL;DR

  • cin >> variable reads one value from the keyboard.
  • Store fractional temperatures in double.
  • (c * 9) / 5 + 32 converts Celsius to Fahrenheit.
  • A short if guard handles nonsense input.