Metadata


Notes

Temperature Conversion Program: Fahrenheit to Celsius and Celsius to Fahrenheit

In this tutorial, we’re creating a simple temperature conversion program that allows a user to convert between Fahrenheit and Celsius. The program will ask the user for input, check the selected unit for conversion, perform the necessary conversion using formulas, and display the results. We’ll also use conditional operators (if, else if, and else) to control the flow based on the user’s choices.

1. Declaring Variables

At the top of the program, we declare the variables needed for the conversion:

double temp;      // temp stores the temperature input by the user
char unit;        // unit stores the character entered by the user to choose Fahrenheit or Celsius
 
  • temp: A double type variable is used because temperatures can be decimal values, and Fahrenheit or Celsius conversions may result in fractional values.
  • unit: A char type variable is used to store the unit the user wants to convert to—either 'f' for Fahrenheit or 'c' for Celsius.

2. Program Title and Output Formatting

To enhance user experience and make the program more user-friendly, we print a title at the beginning and some formatting characters at the end:

cout << "Temperature Conversion" << endl;   // Prints a title to indicate the program's purpose
cout << "***********************" << endl;  // Adds a line of asterisks at the end (optional)
 

While this part is not necessary for functionality, it makes the program more organized and visually appealing.

3. User Input for Conversion Choice

The program then outputs a simple prompt asking the user whether they want to convert to Fahrenheit or Celsius:

cout << "F = Fahrenheit" << endl;
cout << "C = Celsius" << endl;
cout << "What unit would you like to convert to: ";
cin >> unit;
 

Here, the user is expected to input either 'f' or 'c'. We handle both uppercase and lowercase responses by checking for both unit == 'f' and unit == 'F' (for Fahrenheit), or unit == 'c' and unit == 'C' (for Celsius).

4. Conversion Logic

Converting Celsius to Fahrenheit

If the user wants to convert a temperature to Fahrenheit, the program checks if unit is 'f' or 'F' using a conditional if statement:

if (unit == 'f' || unit == 'F') {
    cout << "Enter the temperature in Celsius: ";
    cin >> temp; // Input temperature in Celsius
 
    // Formula for converting Celsius to Fahrenheit
    temp = (temp * 1.8) + 32;
 
    cout << "Temperature is " << temp << " degrees Fahrenheit." << endl;
}
 
  • Explanation: The formula to convert Celsius (C) to Fahrenheit (F) is:

  • After the user inputs the temperature in Celsius, the program multiplies the temperature by 1.8 and adds 32 to it to get the Fahrenheit value. The result is then displayed.

Converting Fahrenheit to Celsius

Similarly, if the user wants to convert from Fahrenheit to Celsius, the program checks for the character 'c' or 'C':

else if (unit == 'c' || unit == 'C') {
    cout << "Enter the temperature in Fahrenheit: ";
    cin >> temp; // Input temperature in Fahrenheit
 
    // Formula for converting Fahrenheit to Celsius
    temp = (temp - 32) / 1.8;
 
    cout << "Temperature is " << temp << " degrees Celsius." << endl;
}
 
  • Explanation: The formula to convert Fahrenheit (F) to Celsius (C) is:

  • After inputting the Fahrenheit temperature, the program subtracts 32 from it and then divides by 1.8 to get the Celsius value. The result is then displayed.

5. Handling Invalid Input

If the user enters anything other than 'f' or 'c', the program will display an error message asking the user to enter a valid choice:

else {
    cout << "Please enter only 'C' or 'F'." << endl;
}
 

This is important for user input validation. The program will only perform conversions if the user provides valid input.

6. Sample Output and Testing

Here’s what the program looks like in action:

Valid Input Example 1 (Celsius to Fahrenheit):

Temperature Conversion
***********************
F = Fahrenheit
C = Celsius
What unit would you like to convert to: f
Enter the temperature in Celsius: 0
Temperature is 32 degrees Fahrenheit.

Valid Input Example 2 (Fahrenheit to Celsius):

Temperature Conversion
***********************
F = Fahrenheit
C = Celsius
What unit would you like to convert to: c
Enter the temperature in Fahrenheit: 100
Temperature is 37.7778 degrees Celsius.

Invalid Input Example:

Temperature Conversion
***********************
F = Fahrenheit
C = Celsius
What unit would you like to convert to: p
Please enter only 'C' or 'F'.

In this case, the user types an invalid character (p), and the program correctly asks for valid input.

7. Key Concepts and Lessons

  • Conditional Operators: This program makes extensive use of if, else if, and else statements to control the flow based on user input.
    • The use of || (OR operator) allows us to check both uppercase and lowercase inputs (unit == 'f' || unit == 'F').
  • Temperature Conversion Formulas:
    • Celsius to Fahrenheit: F = C * 1.8 + 32
    • Fahrenheit to Celsius: C = (F - 32) / 1.8
  • User Input Validation: The program ensures that only valid inputs (‘C’ or ‘F’) are accepted, and prompts the user again if the input is incorrect.

8. Additional Improvements

To further enhance this program, consider adding:

  • Floating-point precision control: Round the output to a certain number of decimal places for better presentation.

    cout << fixed << setprecision(2) << temp << " degrees Fahrenheit." << endl;
     
  • Looping for continuous conversions: Instead of ending after one conversion, you could allow the user to perform multiple conversions without restarting the program. This can be achieved with a while loop that asks the user if they want to perform another conversion.

Conclusion:

This temperature conversion program demonstrates how to use conditional statements, handle user input, and apply basic arithmetic to solve a real-world problem. It is a great exercise in learning how to control program flow and implement useful formulas in code.


References