Goal: Create a constructor with parameters which initializes an object with parameters. This project entails working with temperatures, so the temperatures have to be entered correctly. The scale can only be Kelvin (K), Celsius (C), and Fahrenheit (F). Also, 0 degrees K represents the lowest possible temperature in existence (the complete absence of heat), so K CANNOT be less than 0 degrees, C CANNOT be less the -273.15 degrees because when converted to K it is less than 0 degrees. Lastly, F CANNOT be less than -459.67 degrees, because when converted to either C or K, it less than the appropriate values accepted.
What I've tried: In my implementation file, I created a constructor and CheckTemp function that accepts 2 parameters, one for the degree and the other for the scale. The CheckTemp function has a return type of bool. So, if it meets any of the conditions I stated above, it returns false. Else it returns true.
As you can see below, although some of the objects should result in a return of the true value for the CheckTemp function, returns false. As a result, I am getting 0 degrees Celsius. One thing I did was to place cout statements for each statement to identify where the problem may be, but it only ran the first statement five times.
Does anyone know what I may have done wrong?
NOTE: I have a constructor with no parameters, hence the reason why t1 is initialized. Objects with no parameters should be set up to 0 degrees C. As well as objects with inappropriate values.
Code:
/------------------------- MAIN.CPP -------------------------
//Program is designed for weather forecasting
#include <iostream>
#include "temperature.hpp"
using namespace std;
int main()
{
//Initiatizes the temperature objects
Temperature t1, t2(23.5, 'F'), t3(12.6, 'Z'), t4(-300, 'c'), t5(15, 'k');
t1.Show();
t2.Show();
t3.Show();
t4.Show();
t5.Show();
return 0;
}
//------------------------- TEMPERATURE.CPP -------------------------
#include <iostream>
#include "temperature.hpp"
using namespace std;
//Destructor, used to delete objects
Temperature::~Temperature()
{
}
//Initializes an object with 0 degrees Celsius
Temperature::Temperature()
{
degrees=0;
scale='C';
}
Temperature::Temperature(double d, char s)
{
if (CheckTemp(d,s)==false)
{
degrees=0;
scale='C';
}
else {
degrees=d;
scale=s;
}
}
//Displays the temperature
void Temperature::Show() const
{
cout << degrees << " " << scale << "\n";
}
//Checks if temperature if appropriate
bool Temperature::CheckTemp(double d, char s) const
{
if (s!='K' || s!='k' || s!='C' || s!='c' || s!='F'|| s!='f')
return false;
else if ((s!='K' || s!='k') && d<0)
return false;
else if ((s!='C' || s!='c') && d<-273.15)
return false;
else if ((s!='F' || s!='f') && d<-459.67)
return false;
else return true;
}