🌿 Smart Garden Manager in C++ with Robotics, UI, Drones, and Sound "A Rainwater Conservation System for Tomorrow’s Farms"

Hey guys I you're doing great. I'm back with another project to push your skills up to a new level as well as your confidence. You can now easily create this temperature converter with your very own hands by writing c++ that you have learned. So what are you waiting for let go!
A program to convert temperatures between Celsius, Fahrenheit, and Kelvin.
Skills you will be learning throughout this project: Input/output handling and arithmetic calculations.
User has to select a source scale, then has to enter the temperature then has to pick the target scale to convert.
Here is the code to achieve the desired result.
#include <iostream>
using namespace std;
int main() {
double temperature, convertedTemperature;
char sourceUnit, targetUnit;
cout << "Temperature Converter\n";
cout << "---------------------\n";
cout << "Available units: \n";
cout << "C - Celsius\nF - Fahrenheit\nK - Kelvin\n";
// Input source unit
cout << "Enter the source unit (C/F/K): ";
cin >> sourceUnit;
// Input temperature
cout << "Enter the temperature: ";
cin >> temperature;
// Input target unit
cout << "Enter the target unit (C/F/K): ";
cin >> targetUnit;
// Conversion logic
if (sourceUnit == 'C' || sourceUnit == 'c') {
if (targetUnit == 'F' || targetUnit == 'f') {
// Celsius to Fahrenheit
convertedTemperature = (temperature * 9 / 5) + 32;
} else if (targetUnit == 'K' || targetUnit == 'k') {
// Celsius to Kelvin
convertedTemperature = temperature + 273.15;
} else {
convertedTemperature = temperature;
}
} else if (sourceUnit == 'F' || sourceUnit == 'f') {
if (targetUnit == 'C' || targetUnit == 'c') {
// Fahrenheit to Celsius
convertedTemperature = (temperature - 32) * 5 / 9;
} else if (targetUnit == 'K' || targetUnit == 'k') {
// Fahrenheit to Kelvin
convertedTemperature = (temperature - 32) * 5 / 9 + 273.15;
} else {
convertedTemperature = temperature;
}
} else if (sourceUnit == 'K' || sourceUnit == 'k') {
if (targetUnit == 'C' || targetUnit == 'c') {
// Kelvin to Celsius
convertedTemperature = temperature - 273.15;
} else if (targetUnit == 'F' || targetUnit == 'f') {
// Kelvin to Fahrenheit
convertedTemperature = (temperature - 273.15) * 9 / 5 + 32;
} else {
convertedTemperature = temperature;
}
} else {
cout << "Invalid source unit!\n";
return 1; // Exit program with an error code
}
// Output the result
cout << "Converted temperature: " << convertedTemperature << " " << targetUnit << endl;
return 0;
}
**Display message**:
```cpp
cout << "Temperature Converter\n";
cout << "---------------------\n";
cout << "Available units: \n";
cout << "C - Celsius\nF - Fahrenheit\nK - Kelvin\n";
```
This part describes the available units of temperature.
**Handling the input**:
The program prompts the user to input the source unit, then the temperature value for conversion, and lastly the target unit.
**The logic of conversion**:
The program processes the input and performs the necessary conversions based on the provided units.
Let's have a closer look at the logic of conversion.
if (sourceUnit == 'C' || sourceUnit == 'c') {
if (targetUnit == 'F' || targetUnit == 'f') {
convertedTemperature = (temperature * 9 / 5) + 32; // Celsius to Fahrenheit
} else if (targetUnit == 'K' || targetUnit == 'k') {
convertedTemperature = temperature + 273.15; // Celsius to Kelvin
} else {
convertedTemperature = temperature; // Same unit
}
}
### Sequence in which the code is written
The **if block** gives the description of the source unit.
The **nested if block** gives the description of the target unit.
### Conversion Formulas
**Celsius to Fahrenheit**:
$$T_F = T_C \times \frac{9}{5} + 32$$
**Celsius to Kelvin**:
$$T_K = T_C + 273.15$$
**Fahrenheit to Celsius**:
$$T_C = (T_F - 32) \times \frac{5}{9}$$
**Fahrenheit to Kelvin**:
$$T_K = (T_F - 32) \times \frac{5}{9} + 273.15$$
**Kelvin to Celsius**:
$$T_C = T_K - 273.15$$
**Kelvin to Fahrenheit**:
$$T_F = (T_K - 273.15) \times \frac{9}{5} + 32$$
### Handling of invalid inputs
```cpp
else {
cout << "Invalid source unit!\n";
return 1; // Exit the program with an error code
}
```
In case of an invalid input, an error is displayed, and the program exits.
### Displaying the result
```cpp
cout << "Converted temperature: " << convertedTemperature << " " << targetUnit << endl;
```
Displays the converted temperature with the target unit.
### Sample of running
```
Enter the source unit (C/F/K): C
Enter the temperature: 25
Enter the target unit (C/F/K): F
Output.....Converted temperature: 77 F
```
### Skills used
- Input with `cin` and output with `cout` statements.
- Calculation with temperature conversion formulas.
- Conditional statements using nested if blocks for handling different cases of conversion.
- Handling of invalid cases with error messages.
So I hope you like this project and I will see you next time!
Comments
Post a Comment