Answer to Question #249809 in C++ for Urooj

Question #249809

You should now have a complete 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.


1
Expert's answer
2021-10-12T00:36:50-0400
#include <iostream>
using namespace std;
class Celsius{
public:
    float setV(float x){
        v = x;
        convV = v * 9 / 5 + 32;
    }
    float getV(){
        return v;
    }
    float getConvV(){
        return convV;
    }
private:
    float v;
    float convV;
};


class Fahrenheit{
public:
    float setV(float c){
        v = c;
        convV = (v - 32)*5/9;
    }
    float getV(){
        return v;
    }
    float getConvV(){
        return convV;
    }
private:
    float v;
    float convV;
};




int main()
{
    char T;
    float v1;


    cout << "Enter the temperature to convert (Press c for celsius and f for fahrenheit.) ";
    cin >> T;
    cout << "Enter a temperature value: ";
    cin >> v1;


    if(T == 'C' || T == 'c'){
        Celsius M;
        M.setV(v1);
        cout << "The temperature in Fahrenheit is: " << M.getConvV();
    }
    else if(T == 'F' || T == 'f'){
        Fahrenheit E;
        E.setV(v1);
        cout << "The temperature in celsius is: " << E.getConvV();


    }
    else{
        return(0);
    }
    return(0);
}

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