Menu-Driven Temperature Conversion (Celsius ⇄ Fahrenheit)
Problem Statement
Formulate an interactive menu-driven console application with options: 1. Convert Celsius to Fahrenheit, 2. Convert Fahrenheit to Celsius, 3. Exit. The loop repeats until the user chooses option 3, with validation for invalid menu entries.
Variable Dictionary / Table
| Variable Name | Data Type | Role & Purpose |
|---|---|---|
| Choice | Integer | User menu option (1, 2, or 3) |
| C | Float / Double | Celsius temperature |
| F | Float / Double | Fahrenheit temperature |
Step-by-Step Formal Algorithm
Step 1: Start.
Step 2: Display Menu:
1. Celsius to Fahrenheit
2. Fahrenheit to Celsius
3. Exit
Step 3: Read Choice.
Step 4: If Choice == 1:
Read Celsius C.
F = (C * 9 / 5) + 32.
Print F.
Go to Step 2.
Step 5: Else if Choice == 2:
Read Fahrenheit F.
C = (F - 32) * 5 / 9.
Print C.
Go to Step 2.
Step 6: Else if Choice == 3:
Print 'Exiting program. Goodbye!'
Go to Step 8.
Step 7: Else:
Print 'Invalid Choice! Please enter 1, 2, or 3.'
Go to Step 2.
Step 8: Stop.
Flowchart (Interactive Graphical Diagram)
Flowchart (ANSI Terminal ASCII Chart)
+-----------------+
| [ START ] |
+-----------------+
|
v
+-----------------+
+------> | Display Menu |
| | Read Choice |
| +-----------------+
| |
| v
| / \
| /Choice==1?\ ---YES--> [Read C -> F=C*9/5+32 -> Print F] --+
| \ / |
| | NO |
| v |
| / \ |
| /Choice==2?\ ---YES--> [Read F -> C=(F-32)*5/9 -> Print C]-+
| \ / |
| | NO |
| v |
| / \ |
| /Choice==3?\ ---YES--> [Print Goodbye] --> [ STOP ] |
| \ / |
| | NO |
| v |
| +-----------------+ |
+------- | Print Invalid | <-----------------------------------------+
+-----------------+
Dry-Run Execution Trace Table
| Execution Step / Case | Variables State 1 | Variables State 2 | Condition Evaluation | Output / Result |
|---|---|---|---|---|
| Action 1 | Choice = 1, C = 100°C | F = (100 * 9/5) + 32 | F = 180 + 32 = 212°F | Result: 212.00 °F |
| Action 2 | Choice = 2, F = 32°F | C = (32 - 32) * 5/9 | C = 0 * 5/9 = 0°C | Result: 0.00 °C |
| Action 3 | Choice = 3 | Exit loop | Loop terminates | Exiting program. Goodbye! |
Interactive C++ Compiler & Runner
Live In-BrowserEdit the code directly with the Edit Code button, provide custom inputs, and press Run Code to compile and execute in the live terminal console below!
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int choice;
cout << fixed << setprecision(2);
do {
cout << "\n========================================\n";
cout << " TEMPERATURE CONVERTER SYSTEM MENU \n";
cout << "========================================\n";
cout << " 1. Celsius to Fahrenheit (C -> F)\n";
cout << " 2. Fahrenheit to Celsius (F -> C)\n";
cout << " 3. Exit Application\n";
cout << "----------------------------------------\n";
cout << "Enter your choice (1 - 3): ";
if (!(cin >> choice)) {
cout << "Invalid input! Exiting program...\n";
break;
}
switch (choice) {
case 1: {
double c;
cout << "Enter temperature in Celsius (°C): ";
cin >> c;
double f = (c * 9.0 / 5.0) + 32.0;
cout << ">>> Result: " << c << " °C = " << f << " °F\n";
break;
}
case 2: {
double f;
cout << "Enter temperature in Fahrenheit (°F): ";
cin >> f;
double c = (f - 32.0) * (5.0 / 9.0);
cout << ">>> Result: " << f << " °F = " << c << " °C\n";
break;
}
case 3:
cout << "\nThank you for using Temperature Converter. Goodbye!\n";
break;
default:
cout << "\n[!] Error: Invalid selection! Please enter 1, 2, or 3.\n";
break;
}
} while (choice != 3);
return 0;
}
Input: 1 100 3 Output: >>> Result: 100.00 °C = 212.00 °F Thank you for using Temperature Converter. Goodbye!
Menu-driven CLI architectures simulate real-world interactive microservices, using do-while loops and switch-case blocks for clean operational control.