Answer to Question #252541 in C++ for Urooj

Question #252541

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


1
Expert's answer
2021-10-17T14:00:31-0400

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





Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS