-
Thermometer Class
i need to creat a themometer that can show the temperature in degrees C and in Degrees F depending on what the user puts in.
i am very new with clases and i am wondering if i am on the right tracks with this begining code?
Code:
//CLASSES
#include <iostream>
using namespace std;
class C_Themometer
{
private:
float mDegreesC, mDegreesF;
public:
int SetDegreesC(float DegreesC, float DegreesF)
{
mDegreesC = DegreesC;
mDegreesF = DegreesF;
}
void SetDegreesF()
{
}
int GetDegreesC(float& DegreesC, float& DegreesF)
{
cout <<"Please enter a temperature";
return mDegreesC = (9.0/5.0 * mDegreesC) + (32.0);
}
void GetDegreesF()
{
}
};
void main()
{
C_Themometer Deg1, Deg2;
int mDegreesC;
int mDegreesF;
system("pause");
}
if anyone can give me a few pointers that will help then it would be very helpfull.
-
Here are a few pointers:
Code:
int *degrees;
double *celsius;
float *fahenheit;
-
1. What you are writing isn't really a thermometer class, it's a temperature conversion class.
2. If you are asking the user to input the temperature (as inGetDegreesC) you need to tell them whether the temperature they are entering is supposed to be in Celsius or Farenheit.
3. If the user is supplying the temperature to be converted each time, you don't have to store it.
4. Do you really need a class? What you are tring to achieve is really 2 simple functions. If you want to use a class, ask yourself what "extra" value the class can give you.
5. You could always expand whatever you do to include (say) Kelvin.
-
yes we need to put it into a class.
here is the question we have been asked. might make a bit more sence.
Code:
Write a class called C_Thermometer.
This is based on the solution to a similar question in the first set of lab exercises.
The class needs to know everything it needs required to support itself as a thermometer.
For example, it needs to store the temperature in degrees Celsius.
You should write member functions SetDegreesC, SetDegreesF, GetDegreesC and GetDegreesF.
Write a program to test your class.
You need to think about appropriate function parameters and return values.
-
OK, so your class only need store the temperature in Celsius (as a private data member). The SetDegreesC member function should simply save its argument into the private data member. The GetDegreesC member function should simply return the value of the private data member. The SetDegreesF member function should convert its argument from F->C before saving it into the private data member. The GetDegreesF member function should return the private data member converted C->F. You do not need to ever store the temperature in F.
The question is unclear as to what the initial state of the thermometer class should be, so your constructor could either be a default one (initializing temperature in Celsius to (say) zero), or one that takes an argument (the initial temperature in C) or you could write both of course!
-
i think the second part of the question that we have to do next asks us to set it to zero (default constructor).
-
Now all you have to do is write something like this:
Code:
Deg1.GetDegreesC(mDegreesC, mDegreesF);
Deg1.SetDegreesC(mDegreesC,mDegreesF);
-
I don't understand why you are making such strange functions, but I would do a thermometer like this:
Code:
#include <iostream>
class Thermometer{
private:
double mDegreesC;
public:
Thermometer(){
mDegreesC=0.00f;
}
void SetDegreesC(double DegreesC){
mDegreesC = DegreesC;
return;
}
void SetDegreesF(double DegreesF){
mDegreesC = (double)100/(double)180*(DegreesF-(double)32);
return;
}
double GetDegreesC(){
return mDegreesC;
}
double GetDegreesF(){
return 1.8*mDegreesC+32;
}
};
int main(void){
Thermometer therm1;
char buffer[256];
std::cout<<"Enter a temperature in Celsius:\n";
std::cin.getline(buffer,256,'\n');
therm1.SetDegreesC(strtod(buffer,NULL));
std::cout<<"It is "<<therm1.GetDegreesF()<<" in Fahrenheit.\n";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
return 0;
}
-
brain
Code:
int *degrees;
double *celsius;
float *fahenheit;
off topic but why not just int all of the temps or double all of the temps or float all of the temps? Keep them all the same? Why did you do it like that? thank you
-
I think it was supposed to be sarcasm.
-
yeah that is the right type of program but i also need it to do it backwards to, from degrees C to degrees F and then Degrees F to degrees C.
-
With my class, you can do that.
-
how can i get it to output the other way round as it stops after it has figured out degreesF?
will i just need to call [code[therm1.SetDegreesF(strtod(buffer,NULL));[/code]
-
This asks for Celsius and then outputs Fahrenheit degrees:
Code:
therm1.SetDegreesC(strtod(buffer,NULL));
std::cout<<"It is "<<therm1.GetDegreesF()<<" in Fahrenheit.\n";
but this asks for Fahrenheit and then outputs Celsius degrees:
Code:
therm1.SetDegreesF(strtod(buffer,NULL));
std::cout<<"It is "<<therm1.GetDegreesC()<<" in Celsius.\n";
Notice that you only need to replace C with F and reverse.
-
yeah thanks i got it but i have changed the code a bit to make it different but i have a problem with the outcome of DegreesF.
i dont know how to change the equation in the GetDegreesF() function so it will output the correct answer because at the moment it just goes up i am using an example temperature of 30 so in degreesf it is 86 but then when i convert it back it goes to 97 or something like that.
here is the code i have and changed.
Code:
#include <iostream>
using namespace std;
class Thermometer{
private:
double mDegreesC;
public:
Thermometer()
{
mDegreesC;
}
void SetDegreesC(double DegreesC)
{
mDegreesC = DegreesC;
return;
}
void SetDegreesF(double DegreesF)
{
mDegreesC = 9.0/5.0*(DegreesF-32.0);
return;
}
double GetDegreesC()
{
return mDegreesC;
}
double GetDegreesF()
{
return 1.8*mDegreesC+32;
}
};
int main(void){
Thermometer therm1, therm2;
float input;
cout<<"Please enter a value for degrees C: \n";
cin>>input;
therm1.SetDegreesC(input);
cout<<"It is "<<therm1.GetDegreesF()<<" in Fahrenheit.\n";
cout<<"Please enter a value for degrees F: \n";
cin >>input;
therm2.SetDegreesF(input);
cout <<therm2.GetDegreesC();
system("pause");
}