Loading lessons...
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
- Run the starter and type
20. Confirm68prints. - Add validation: if
celsius < -273.15, printThat is colder than absolute zero.and stop. - For cleaner output,
#include <iomanip>and print withcout << fixed << setprecision(1) << fahrenheit;. - Try extreme inputs like
-300and0and check the program stays sensible. - Re-run after every change and confirm the results match the math.
Checklist
- [ ]
cin >> celsiusreads the user's number - [ ] The formula
(c * 9) / 5 + 32is used - [ ] The result is stored and printed as a
double - [ ] Values below
-273.15are rejected - [ ] The printout uses
fixedandsetprecision(1)
TL;DR
cin >> variablereads one value from the keyboard.- Store fractional temperatures in
double. (c * 9) / 5 + 32converts Celsius to Fahrenheit.- A short
ifguard handles nonsense input.