Need help with a class please

This is a discussion on Need help with a class please within the C++ Programming forums, part of the General Programming Boards category; Learning classes, and trying to make it all work. This code compiles OK, but when I execute, and say use ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    6

    Need help with a class please

    Learning classes, and trying to make it all work. This code compiles OK, but when I execute, and say use the number 100 as the Farenheit temperature input, I always get -17.7778 as the Celcius equivalent, even when I change the Farenheit temp input. What is my problem here?

    code:_____________________________________________ __

    #include <iostream.h>

    class Thermometer
    {
    public:
    Thermometer(); //constructor
    void setThermometer(double); //function to set temp
    void getFarenheit(double); //function to get temp in F
    void getCelcius(double); //function to get temp in C
    private:
    double FarenTemp; //data members in F
    };

    //Temperature constructor to initialize the data members to zero

    Thermometer::Thermometer() {FarenTemp = 0;}

    //Function to set temperature using Farenheit.

    void Thermometer::setThermometer(double temp)
    {
    temp = FarenTemp;
    }

    //Function to get temperature in degrees F from the user

    void Thermometer::getFarenheit(double t)
    {
    cout<<"Enter a temperature in degrees Farenheit: ";
    cin>>t;

    }

    //Funtion to get temperature in degrees C from the user

    void Thermometer::getCelcius(double)
    {
    cout<<"The temperature in Celcius: "<<(((FarenTemp - 32 ) * 5 ) / 9 );


    }


    int main()
    {
    Thermometer x; //instantiates object x of class Thermometer

    x.getFarenheit(1);

    x.getCelcius(1);

    cout<<endl;

    return 0;
    }

    end code:___________________________________________

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Click Here and adjust your post accordingly.

    edit: your code is pretty weird. you can't assign parameters values.

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    your conversion formula is wrong
    Farenheit to celsius 5.0/9.0*(Fahren_temp-32.0)
    Celsius to farenheit 9.0/5.0*(Celsius_temp + 32.0)

    Fahrentemp and celsius temp represent your variable name
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    6
    Yes, Alpha, I know I can't post parameters as values, but ...... LOL..... I am only a beginner, and if I put nothing in the () then I get an error, and if I put (double) as I think I should, I get errors......... Got any suggestions on how to fix this problem???? You know, us beginners just try to get the code to work, any way that we can. I am going to be starting intermediate programming soon, so hopefully I won't be so laughable with my code.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    6
    To Gamer4life....thanks for the input on the conversion. I will try that and see how it works out.

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    I'm somewhat of a beginner also. what i meant though is not what is inside the parentheses.

    for example:
    Code:
    void Thermometer::setThermometer(double temp)
    {
    temp = FarenTemp;
    }
    you cannot do this, it should be changed to:
    Code:
    void Thermometer::setThermometer(double temp)
    {
    FarenTemp = temp;
    }
    in a class, constructors and set modifier functions are supposed to set private data to something.

    also, you cannot do things such as:
    Code:
    void Thermometer::getFarenheit(double t)
    {
    cout<<"Enter a temperature in degrees Farenheit: ";
    cin>>t;
    }
    you have to do things such as
    Code:
    std::cin >> FarenTemp;
    when you cin to t, this doesn't do anything.

    here, try this:
    Code:
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    
    class Thermometer
    {
    	public:
    	Thermometer(); //constructor
    		//void setThermometer(double); //function to set temp
    		void getFarenheit(); //function to get temp in F
    		void getCelcius(); //function to get temp in C
    	private:
    		double FarenTemp; //data members in F
    };
    //Temperature constructor to initialize the data members to zero
    Thermometer::Thermometer() {FarenTemp = 0;}
    //Function to set temperature using Farenheit.
    
    /*void Thermometer::setThermometer(double temp)
    {
    	FarenTemp = temp;
    }*/
    //Function to get temperature in degrees F from the user
    
    void Thermometer::getFarenheit()
    {
    	cout<<"Enter a temperature in degrees Farenheit: ";
    	cin>>FarenTemp;
    }
    //Funtion to get temperature in degrees C from the user
    void Thermometer::getCelcius()
    {
    	cout<<"The temperature in Celcius: "<<(((FarenTemp - 32 ) * 5 ) / 9 );
    }
    int main()
    {
    	Thermometer x; //instantiates object x of class Thermometer
    	x.getFarenheit();
    	x.getCelcius();
    	cout<<endl;
    return 0;
    }
    This runs and works, at least with 32F to 0C and 212F to 100C.

    edit: your formula in getCelcius works, it does not need to be changed. what was making it produce weird answers was the cin >> t line, this doesn't do anything, and once the function is over, it goes away. If you cin >> FarenTemp, it will store this in private data, and when you use that private data, it has what the temp was. Otherwise, FarenTemp contains garbage.

    also, when you assign FarenTemp, you should assign FarenTemp the value of the parameter, and not assign the parameter the value of FarenTemp. That function wasn't needed, so I commented it out. But if it is a homework, and you are supposed to have the function, you have to think through your code more.

    If I offended you with my previous post, it was not intended and I am sorry for that. I am here to help people learn, and to learn myself.

    I hope this helps some, if you have any more questions, post up.
    Last edited by alpha; 03-08-2003 at 04:23 PM.

  7. #7
    Registered User mouse163's Avatar
    Join Date
    Dec 2002
    Posts
    49

    set & get

    you should be settting the farenheight temp in SET temp not get temp...once you set it THEN get it...and yes, you calc are offf..
    the ones provided are correct.
    gets should never modify data..a good way to ensure this is to make your 'gets' const
    ---M

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    oh, good catch. i didn't even look at the formula, it worked with 3 tests, so i assumed it was right. change it to what gamer said, and it should be good.

    to clarify a bit on what mouse said for the get fxns:
    Code:
    void Thermometer::getCelcius() const
    {
    cout<<"The temperature in Celcius: "<<(((FarenTemp - 32 ) * 5 ) / 9 );
    }
    notice the const after the fxn; this would be an accessor.

    also, as mouse said, get fxns should not be modifiers; the code in your getFarenheit fxn should just return FarenTemp. That code should be placed in setThermometer. Actually, ideally, you shouldn't have any cout or cin statements in your class functions.

  9. #9
    Registered User
    Join Date
    Feb 2003
    Posts
    6
    No offense taken! I would never learn anything if I got offended by my own mistakes.

    Thanks both Alpha and Mouse for your input so far. I will try to fix all this up. It isn't homework, just a project I came up with to make sure I understand how to do classes, before I go to the homework. Looks like it was a good idea to make up some work before I actually hit the homework stuff, huh!

    Thanks so much. If I run into more questions, I will be sure to ask you.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 08:10 AM
  2. Two conceptual questions
    By AntiScience in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2007, 11:36 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21