1

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;
    }  
Rams
  • 21
  • 3
  • 1
    Welcome to Stack Overflow! What does `CheckTemp` look like? It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and [Debugging Guide](http://idownvotedbecau.se/nodebugging/) – NathanOliver Mar 16 '21 at 13:34
  • BTW, you don't need to delete your [old question](https://stackoverflow.com/questions/66646551) and ask a new one. When you make edits, we'll end up looking at the changes and reopening it if necessary. – cigien Mar 16 '21 at 13:36
  • 1
    Assume `s` is `'K'`. What happens when you do `s!='C'`? – Eljay Mar 16 '21 at 13:37
  • 3
    Hint: `if (s!='K' || s!='k' .... )` is always TRUE. If "A is not B OR A is not C" is always TRUE. – rustyx Mar 16 '21 at 13:38
  • 2
    Please see [this canonical](https://stackoverflow.com/questions/26337003) for an in-depth explanation of what rustyx is referring to with regards to your `if` condition. – cigien Mar 16 '21 at 13:40
  • 1
    I suggest something like `enum class TemperatureUnit {Kelvin, Celsius, Farenheit};` instead of `char`. – Jarod42 Mar 16 '21 at 13:45
  • Static constructors might also be useful. – Jarod42 Mar 16 '21 at 13:48
  • OP, your previous question had good comments with tips on fixing your code. None of them were implemented before re-posting. – sweenish Mar 16 '21 at 14:07
  • @Rams There's a simpler (and more readable) way to implement your CheckTemp function with a switch: https://godbolt.org/z/6WGvae – m88 Mar 16 '21 at 14:09
  • This doesn't address the question, but `if (CheckTemp(d, s) == false) would usually be written as `if (!CheckTemp(d, s))`. Or, even better, write `if (CheckTemp(d, s))` and switch the two controlled blocks around. That way readers of the code (including your future self) don't have to sort out that negated condition. – Pete Becker Mar 16 '21 at 14:09

0 Answers0