It's time to put your function skills to work! You'll create a temperature converter that uses functions to convert between Celsius and Fahrenheit.
YOUR TASK
Create a program that:
Shows a menu with conversion options (C to F, F to C, Exit)
Has a function celsiusToFahrenheit() that takes Celsius and returns Fahrenheit
Has a function fahrenheitToCelsius() that takes Fahrenheit and returns Celsius
Uses a loop to keep showing the menu until the user chooses to exit
Displays the converted temperature with proper formatting
Conversion Formulas:
Celsius to Fahrenheit: F = (C × 9/5) + 32
Fahrenheit to Celsius: C = (F - 32) × 5/9
Required Functions:
double celsiusToFahrenheit(double celsius) {
// Convert celsius to fahrenheit
// Return the result
}
double fahrenheitToCelsius(double fahrenheit) {
// Convert fahrenheit to celsius
// Return the result
}
Hint 1: Program Structure
Break it down:
1. Create both conversion functions BEFORE main()
2. In main(), use a while loop that continues until user chooses exit
3. Inside the loop: show menu, get choice, use switch or if-else
4. For each conversion: get temperature, call function, display result
5. Use a variable like int choice and exit when choice == 3
Hint 2: Function Implementation
Writing the functions:
• Both functions should return double
• celsiusToFahrenheit: return (celsius * 9.0 / 5.0) + 32;
• fahrenheitToCelsius: return (fahrenheit - 32) * 5.0 / 9.0;
• Use 9.0 and 5.0 (not 9 and 5) for accurate decimal division
Hint 3: Menu Loop Logic
Menu structure: int choice = 0; while (choice != 3) {
Display menu (1: C to F, 2: F to C, 3: Exit)
Get choice
If choice == 1: get celsius, call function, display
If choice == 2: get fahrenheit, call function, display
If choice == 3: display "Goodbye!" }
Hint 4: Complete Solution Outline
Full structure:
1. Define conversion functions (before main)
2. In main(): declare choice variable
3. Create while loop (continues until choice == 3)
4. Show menu with cout statements
5. Get user choice with cin
6. Use switch/if-else to handle each option
7. For conversions: declare temp variable, get input, call function, display
8. After loop ends, return 0
Test Cases
Input: 1, then 0 → Output: 0°C = 32.0°F
Input: 1, then 100 → Output: 100°C = 212.0°F
Input: 2, then 32 → Output: 32°F = 0.0°C
Input: 2, then 212 → Output: 212°F = 100.0°C
Input: 1, then 25, then 3 → Shows conversion, then exits
Example Run:
=== Temperature Converter ===
1. Celsius to Fahrenheit
2. Fahrenheit to Celsius
3. Exit
Enter choice: 1
Enter temperature in Celsius: 25
25°C = 77.0°F
=== Temperature Converter ===
1. Celsius to Fahrenheit
2. Fahrenheit to Celsius
3. Exit
Enter choice: 3
Goodbye!
Code Editor
Ready
Output
Console
Console initialized...
Ready for Practice Project 5: Temperature Converter
> Create your conversion functions
> Build a menu-driven program
> Use loops to keep the program running
> Test with multiple conversions!