Thread: Problems with displaying class variables

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    2

    Problems with displaying class variables

    Whenever I try to display class variables in my program I get weird results that I shouldn't be getting such as "-7.92985e+185" instead of a real number. I'm not sure where the problem is coming from, when the program ran off all functions it worked fine. Any help is appreciated, thanks in advance.

    Proto.h
    Code:
    #ifndef proto_h
    #define proto_h
    
    class myArea
    {
    private:
    	double length_c;
    	double width_c;
    	double height_c;
    
    public:
    	void setlw(double length, double width);
    	void setheight(double height);
    	void setlwh(double length, double width, double height);
    
    	void getlw(double length, double width);
    	void getheight(double height);
    	void getlwh(double length, double width, double height);
    
    	double findfloorarea(void);
    	double findawallarea(void);
    	double findvolume(void);
    
    	void show_findfloorarea(void);
    	void show_findwallarea(void);
    	void show_findvolume(void);
    	void show_height(void);
    }; 
    
    #endif
    myArea.cpp
    Code:
    #include "proto.h"
    #include <iostream>
    using namespace std;
    
    void myArea::setlw(double length, double width)
    {
    	if (length_c > 0, width_c > 0)
    	{
    		length_c = length;
    		width_c = width;
    	}
    }
    
    void myArea::setheight(double height)
    {
    	if (height_c > 0)
    	{
    		height_c = height;
    	}
    }
    
    void myArea::setlwh(double length, double width, double height)
    {
    	if (length_c > 0, width_c > 0, height_c > 0)
    	{
    		length_c = length;
    		width_c = width;
    		height_c = height;
    	}
    }
    
    void myArea::getlw(double length, double width)
    {
    	length = length_c;
    	width = width_c;
    }
    
    void myArea::getheight(double height)
    {
    	height = height_c;
    }
    
    void myArea::getlwh(double length, double width, double height)
    {
    	length = length_c;
    	width = width_c;
    	height = height_c;
    }
    
    void myArea::show_findfloorarea(void)
    {
    	cout<<(length_c * width_c)<<endl;
    }
    
    void myArea::show_findwallarea(void)
    {
    	cout<<(length_c * height_c)<<endl;
    }
    
    void myArea::show_findvolume(void)
    {
    	cout<<(length_c * width_c * height_c)<<endl;
    }
    
    void myArea::show_height(void)
    {
    	cout<<height_c<<endl;
    }
    main.cpp
    Code:
    #include <iostream>
    #include "proto.h"
    
    using namespace std;
    
    void demo(const double & l, const double & w, const double & h);
    
    int main()
    {
    	demo(4,3,5);
    	demo(5,8,12);
    	demo(8,3,18);
    	demo(6,14,9);
    	demo(7,13,2);
    	return 0;
    }
    
    void demo(const double & l, const double & w, const double & h)
    {
    	myArea area;
    	cout<<"Using functions: 'setlw()','getlw()' and 'show_findfloorarea()', with the parameters "<<l<<", "<<w<<"."<<endl;
    	area.setlw(l,w);
    	area.getlw(l,w);
    	area.show_findfloorarea();
    	cout<<"Using functions: 'setheight()','getheight()' and 'show_findwallarea()' with the parameter "<<h<<"."<<endl;
    	area.setheight(h);
    	area.getheight(h);
    	area.show_findwallarea();
    	cout<<"Using functions: 'setlwh()','getlwh()' and 'show_findvolume()' with the parameters "<<l<<", "<<w<<", "<<h<<"."<<endl;
    	area.setlwh(l,w,h);
    	area.getlwh(l,w,h);
    	area.show_findvolume();
    	cout<<endl;
    
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > if (length_c > 0, width_c > 0, height_c > 0)

    Depending on what you want, this should be written either as:
    if (length_c > 0 && width_c > 0 && height_c > 0)
    Or:
    if (length_c > 0 || width_c > 0 || height_c > 0)

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Same problem here:
    > if (length_c > 0, width_c > 0)

    >void myArea::setheight(double height)
    >{
    > if (height_c > 0)

    This should be:
    if (height > 0)

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    2
    Quote Originally Posted by swoopy
    Same problem here:
    > if (length_c > 0, width_c > 0)

    >void myArea::setheight(double height)
    >{
    > if (height_c > 0)

    This should be:
    if (height > 0)
    Yes, that fixed it. Its obvious now, it was testing the global variable when it should have been checking the local one. Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  2. Replies: 8
    Last Post: 10-02-2005, 12:27 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Problems with static const class members
    By sigfriedmcwild in forum C++ Programming
    Replies: 5
    Last Post: 12-05-2004, 07:57 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM