complete a working program that converts a given temperature from Fahrenheit to Celsius and vice versa, depending on which option (integer) the user enters.
Revise your temperature converter to use a switch statement. The switch should test a char variable holding the characters entered by the user, e.g: Enter "f" to convert from Fahrenheit to Celsius Enter "c" to convert from Celsius to Fahrenheit
You should provide an appropriate default statement.
it should be a switch statement
Source code
#include <iostream>
using namespace std;
void convertFahrenheitToCelsius(double f){
double c=(f-32)/1.8;
cout<<f<<" Fahrenheit = "<<c<<" Celsius";
}
void convertCelsiusToFahrenheit(double c){
double f=(c*1.8)+32;
cout<<c<<" Celsius = "<<f<<" Fahrenheit";
}
int main()
{
char choice;
cout<<"\nEnter 'f' to convert from Fahrenheit to Celsius Enter 'c' to convert from Celsius to Fahrenheit:";
cin>>choice;
double temp_f;
double temp_c;
switch(choice){
case 'f':
cout<<"\nEnter temperature in Fahrenheit: ";
cin>>temp_f;
convertFahrenheitToCelsius(temp_f);
break;
case 'c':
cout<<"\nEnter temperature in Celsius: ";
cin>>temp_c;
convertCelsiusToFahrenheit(temp_c);
break;
default:
cout<<"\nInvalid input!";
}
return 0;
}
Output 1
Output 2
Comments