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.
#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);
}
Comments
Leave a comment