Temperature Conversion

Can someone guide me in the right direction? I don't know what's going on. I'm getting a segmentation error

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172`` ``````#include using namespace std; class temperature{ double input; double degreef; double degreek; double degreec; public: double setTempKelvin(); double setTempFahrenheit(); double setTempCelsius(); }; double temperature::setTempKelvin(){ degreek = degreec + 273.15; return degreek; } double temperature::setTempFahrenheit(){ degreef = degreec * (9.0/5) + 32; return degreef; } double temperature::setTempCelsius(){ degreec = (5.0/9) * (degreef - 32); return degreec; } int main(){ temperature *temp; double input, degreef, degreek, degreec; char type; cout << "Which kind of temperature would you like to enter?"<< endl; cout << "(Kelvin(1), Fahrenheit(2), Celsius(3))"; cin >> type; if(type == '1'){ cout << "Enter your temperature: "; cin >> input; input = degreek; } else if (type == '2'){ cout << "Enter your temperature: "; cin >> input; input = degreef; } else if(type == '3'){ cout <<"Enter your temperature: "; cin >> input; input = degreec; } else { cout << "You have entered something invalid"<setTempKelvin() << endl; cout << "Your temperature in Celsius is "<< temp->setTempCelsius() << endl; cout << "Your temperature in Fahrenheit is "<< temp->setTempFahrenheit() << endl; }``````
Last edited on
help!
anyone?
The class temperature has a major omission. It has outputs but no input. There should be a constructor(s) or some function(s) to get the values into the class.

At line 36, ` temperature *temp;` an uninitialised pointer is declared. This should not be de-referenced until it has been set to point to a valid address. Or don't bother with a pointer at all, just declare an actual temperature object.
Last edited on
Okay I've fixed a few things, now I don't know how to make it display the right conversion calculations.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778`` ``````#include using namespace std; class temperature{ double input; double degreef; double degreek; double degreec; public: double setTempKelvin(); double setTempFahrenheit(); double setTempCelsius(); }; double temperature::setTempKelvin(){ degreek = degreec + 273.15; return degreek; } double temperature::setTempFahrenheit(){ degreef = degreec * (9.0/5) + 32; return degreef; } double temperature::setTempCelsius(){ degreec = degreef - 273.15; return degreec; } int main(){ temperature *temp = new temperature; double input, degreef, degreek, degreec; char type; cout << "Which kind of temperature would you like to enter?"<< endl; cout << "(Kelvin(1), Fahrenheit(2), Celsius(3))"; cin >> type; if(type == '1'){ cout << "Enter your temperature: "; cin >> input; cout << "Your temperature in Kelvin is " << input <setTempCelsius() <setTempFahrenheit() <> input; cout << "Your temperature in Kelvin is "<< temp->setTempKelvin() << endl; cout << "Your temperature in Celsius is "<< temp->setTempCelsius() << endl; cout << "Your temperature in Fahrenheit is "<< input <> input; cout << "Your temperature in Kelvin is "<< temp->setTempKelvin() << endl; cout << "Your temperature in Celsius is "<< input << endl; cout << "Your temperature in Fahrenheit is "<< temp->setTempFahrenheit() << endl; } else { cout << "You have entered something invalid"<
There is still no way to get the values into the temperature object.
For example, this function, `double setTempKelvin();` doesn't accept any parameter.

You could have something like this, to set the value inside the object.
 `` `` ``void setTempKelvin(double input);``

Here, simply store whichever of the three values was supplied by the user, and calculate and store the other two values. That is, most of the work is done in the setXXX() function.

The getXXX() function simply retrieves a value, e.g.
 `` `` ``double getFahrenheit() { return degreef; };``

Last edited on
Here is a working temperature thingy:

Loads of debugging and making sure it worked fine for you...

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131`` ``````#include #include #include #define number .55555555555555555555555555555555555555555 using namespace std; void setTempKelvin(double degreek,double degreec,double degreef,char type) { if(type == 'c') { degreek = degreec + 273.15; } else if(type == 'f') { degreec = degreef * number - 32; degreek = degreec + 273.15; } cout << degreek; } void setTempFahrenheit(double degreef,double degreec,double degreek,char type) { if(type == 'c') { degreef = degreec * 1.8 + 32; } else if(type == 'k') { degreec = degreek - 273.15; degreef = degreec * 1.8 + 32; } cout << degreef; } void setTempCelsius(double degreec,double degreef,double degreek,char type) { if(type == 'f') { degreec = degreef * number - 32; } else if(type == 'k') { degreec = degreek - 273.15; } cout << degreec; } int main() { double input, degreef = 0, degreek = 0, degreec = 0; int type; char t; cout << "Which kind of temperature would you like to enter?"<< endl; cout << "(Kelvin(1), Fahrenheit(2), Celsius(3)): "; cin >> type; switch(type) { case 1: cout << "Enter your temperature: "; cin >> input; degreek = input; break; case 2: cout << "Enter your temperature: "; cin >> input; degreef = input; break; case 3: cout <<"Enter your temperature: "; cin >> input; degreec = input; break; default: cout << "You have entered something invalid" << endl; cout << "This program will now end " << endl; return 0; } switch(type) { case 1: t = 'k'; break; case 2: t = 'f'; break; case 3: t = 'c'; break; } cout << "Your temperature in Kelvin is "; if(type != 1) { setTempKelvin(degreek,degreec,degreef,t); } else { cout << input; } cout << endl; cout << "Your temperature in Celsius is "; if(type != 3) { setTempCelsius(degreef,degreec,degreek,t); } else { cout << input; } cout << endl; cout << "Your temperature in Fahrenheit is "; if(type != 2) { setTempFahrenheit(degreef,degreec,degreek,t); } else { cout << input; } cout << endl; return 0; } ``````
Topic archived. No new replies allowed.