cppbeginner10 minutes

Simple Temperature Converter Mini Project

Build a small program that converts temperatures between Celsius and Fahrenheit scales, allowing users to enter a temperature and choose the conversion direction.

Challenge prompt

Create a C++ program that asks the user to input a temperature value and then select whether to convert it to Celsius or Fahrenheit. The program should perform the conversion using the correct formula and display the converted temperature clearly to the user.

Guidance

  • Use functions to separate the logic of converting temperatures for clarity.
  • Prompt the user to input both the temperature value and the desired conversion type.
  • Use simple conditional statements to decide which conversion formula to apply.

Hints

  • Conversion formulas: Fahrenheit to Celsius: (F - 32) * 5/9, Celsius to Fahrenheit: (C * 9/5) + 32.
  • Create two functions: one for converting Celsius to Fahrenheit and another for Fahrenheit to Celsius.
  • Make sure to handle the input carefully so the user knows what to enter.

Starter code

#include <iostream>
using namespace std;

// Function declarations
float celsiusToFahrenheit(float celsius);
float fahrenheitToCelsius(float fahrenheit);

int main() {
    float temperature;
    char conversionType;

    cout << "Enter temperature value: ";
    cin >> temperature;

    cout << "Convert to (C)elsius or (F)ahrenheit? ";
    cin >> conversionType;

    // Add your conversion logic here

    return 0;
}

float celsiusToFahrenheit(float celsius) {
    // TODO: implement conversion
}

float fahrenheitToCelsius(float fahrenheit) {
    // TODO: implement conversion
}

Expected output

Enter temperature value: 100 Convert to (C)elsius or (F)ahrenheit? F 100 Celsius is 212 Fahrenheit Enter temperature value: 32 Convert to (C)elsius or (F)ahrenheit? C 32 Fahrenheit is 0 Celsius

Core concepts

functionsconditionalsuser input/output

Challenge a Friend

Send this duel to someone else and see if they can solve it.