Thread: Compiles but won't cout?

  1. #1
    Registered User Kayoss's Avatar
    Join Date
    Sep 2005
    Location
    California
    Posts
    53

    Compiles but won't cout?

    This program will not cout anything...what am I missing?

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    
    class Point3
    {
    
    public:
       Point3( int = 0, int = 0 ); // default constructor
    
       void setX( int ); // set x in coordinate pair
       int getX(); // return x from coordinate pair
       void setY( int ); // set y in coordinate pair
       int getY(); // return y from coordinate pair
       void print(); // output Point3 object
    
    private:
       int x; // x part of coordinate pair
       int y; // y part of coordinate pair
    
    }; // end class Point3
    
    class Circle4 
    {
    
    public:
       // default constructor
       Circle4( int = 0, int = 0, double = 0.0 );
    
       void setX( int );
       void setY( int );
       int getX();
       int getY();
    
       void setRadius( double ); // set Radius
       double getRadius(); // return Radius
       double getDiameter(); // return diameter
       double getCircumference(); // return circumference
       double getArea();
       void print(); // output Circle4 object
    
    private:
       double radius; // Circle4's radius
       Point3 point; // Point3 object - COMPOSITION
    
    }; // end class Circle4
    
    
    class Cylinder
    {
    
    public:
    
       // default constructor
       Cylinder( int = 0, int = 0, double = 0.0, double = 0.0 );
    
       void setHeight( double ); // set Cylinder's height
       double getHeight(); // return Cylinder's height
       double getArea(); // return Cylinder's area
       double getVolume(); // return Cylinder's volume
       void print(); // output Cylinder
    
        void setX( int ); // set x in coordinate pair
       int getX(); // return x from coordinate pair
       int getY(); // return y from coordinate pair
       void setY( int ); // set y in coordinate pair
       void setRadius( double ); // set Radius
       double getRadius(); // return Radius
       double getDiameter(); // return diameter
       double getCircumference(); // return circumference
    
    private:
       double height; // Cylinder's height
       Circle4 circle; // Circle4 object - COMPOSITION
    
    }; // end class Cylinder
    
    
    Point3::Point3( int xValue, int yValue )
       : x( xValue ), y( yValue )
    {
       // empty body
    } // end Point3 constructor
    
    
    // set x in coordinate pair
    void Point3::setX( int xValue )
    {
       x = xValue; // used for validation
    } // end function setX
    
    // return x from coordinate pair
    int Point3::getX()
    {
       return x;
    } // end function getX
    
    // set y in coordinate pair
    void Point3::setY( int yValue )
    {
       y = yValue; // no need for validation
    } // end function setY
    
    // return y from coordinate pair
    int Point3::getY()
    {
       return y;
    } // end function getY
    
    // output Point3 object
    void Point3::print()
    {
       cout << '[' << getX() << ", " << getY() << ']';
    } // end function print
    
    // default constructor
    Circle4::Circle4( int xValue, int yValue, double radiusValue )
        : radius( radiusValue )
    {
       point.setX( xValue ); 
       point.setY( yValue );
       setRadius( radiusValue );
    } // end Circle4 constructor
    
    // set radius
    void Circle4::setRadius( double radiusValue )
    {
       radius = ( radiusValue < 0.0 ? 0.0 : radiusValue );
    } // end function setRadius
    
    // return radius
    double Circle4::getRadius()
    {
       return radius;
    } // end function getRadius
    
    // calculate and return diameter
    double Circle4::getDiameter()
    { 
       return 2 * getRadius();
    } // end function getDiameter
    
    // calculate and return circumference
    double Circle4::getCircumference()
    {
       return 3.14159 * getRadius() * getRadius();
    } // end function getCircumference
    
    // calculate and return area
    double Circle4::getArea()
    {
       return 3.14159 * getRadius() * getRadius();
    } // end function getArea
    
    // set x in coordinate pair
    void Circle4::setX( int xValue )
    {
       point.setX( xValue );
    
    } // end function setX
    
    // set x in coordinate pair
    void Circle4::setY( int yValue )
    {
       point.setY( yValue );
    } // end function setX
    
    int Circle4::getX()
    {
       return point.getX();
    }
    
    int Circle4::getY()
    {
       return point.getY();
    }
    
    void Circle4::print()
    {
       cout << "Center = ";
       point.print(); // invoke Point3's print function
       cout << "; Radius = " << getRadius();
    } // end function print
    
    // default constructor
    Cylinder::Cylinder( int xValue, int yValue, double radiusValue, double heightValue )
        : height( heightValue )
    {
       circle.setX( xValue );
       circle.setY( yValue );
       setHeight( heightValue );
       circle.setRadius( radiusValue );
    } // end function setHeight
    
    void Cylinder::setX( int xValue )
    {
       circle.setX( xValue );
    
    } // end function setX
    
    // set x in coordinate pair
    void Cylinder::setY( int yValue )
    {
       circle.setY( yValue );
    } // end function setX
    
    int Cylinder::getX()
    {
       return circle.getX();
    }
    
    int Cylinder::getY()
    {
       return circle.getY();
    }
    
    // set radius
    void Cylinder::setRadius( double radiusValue )
    {
       circle.setRadius( radiusValue );
    } // end function setRadius
    
    // return radius
    double Cylinder::getRadius()
    {
       return circle.getRadius();
    } // end function getRadius
    
    // get Cylinder's height
    double Cylinder::getHeight()
    {
       return height;
    } // end function getHeight
    
    void Cylinder::setHeight( double heightValue )
    {
        setHeight( height );
    }
    
    // get Cylinder's circumference
    double Cylinder::getCircumference()
    {
       return 3.14159 * getRadius() * getRadius();
    } // end function getCircumference
    
    // calculate and return diameter
    double Cylinder::getDiameter()
    { 
       return 2 * getRadius();
    } // end function getDiameter
    
    double Cylinder::getArea()
    {
       return 2 * circle.getArea() * circle.getCircumference() * getHeight();
    } // end function getArea
    
    // calculate Cylinder volume
    double Cylinder::getVolume()
    {
       return circle.getArea() * getHeight();
    } // end function getVolume
    
    void Cylinder::print()
    {
       circle.print();
       cout << "Height = " << getHeight();
    } // end function print
    
    int main()
    {
       // instantiate Cylinder object
       Cylinder cylinder( 12, 23, 2.5, 5.7 );
    
       // display point coordinates
       cout << "X coordinate is " << cylinder.getX()
    	<< "\nY coordinate is " << cylinder.getY()
    	<< "\nRadius is " << cylinder. getRadius()
    	<< "\nHeight is " << cylinder.getHeight();
    
       cylinder.setX( 2 ); // set new x-coordinate
       cylinder.setY( 2 ); // set new y-coordinate
       cylinder.setRadius( 4.25 ); // set new radius
       cylinder.setHeight( 20 ); // set new height
    
       // display new cylinder value
       cout << "\n\nThe new location and radius of circle are\n";
       cylinder.print();
    
       // display floating point values wtih 2 digits of precision
       cout << fixed << setprecision( 2 );
    
       // display cylinder's diameter
       cout << "\n\nDiameter is " << cylinder.getDiameter();
    
       // display cylinder's circumference
       cout << "\nCircumference is "
    	<< cylinder.getCircumference();
    
       // display cylinder's area
       cout << "\nArea is " << cylinder.getArea();
    
       // display cylinder's volume
       cout << "\nVolume is " << cylinder.getVolume();
    
       cout << endl;
    
       return 0;
    
    } // end main
    THE redheaded stepchild.

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Ooh I know this one!!!

    Problem is with your setHeight method for Cylinder

    Code:
    void Cylinder::setHeight( double heightValue )
    {
        setHeight( height );
    }
    When i compile i get a warning about a recursive stack overflow
    (looks like an infinite loop, but recursion doesn't do that).
    Change that method to this and all will be well

    Code:
    void Cylinder::setHeight( double heightValue )
    {
        height = heightValue;
    }
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  3. #3
    Registered User Kayoss's Avatar
    Join Date
    Sep 2005
    Location
    California
    Posts
    53
    Thank you so much, I was pulling my hair out by this point!
    THE redheaded stepchild.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with program.
    By olgirl4life in forum C++ Programming
    Replies: 3
    Last Post: 12-11-2006, 11:06 PM
  2. New, making a survey program
    By shaffer in forum C++ Programming
    Replies: 18
    Last Post: 12-01-2006, 11:36 AM
  3. "Spying" on cout
    By sirjis in forum C++ Programming
    Replies: 4
    Last Post: 11-06-2006, 05:00 PM
  4. c++ string input
    By R.Stiltskin in forum C++ Programming
    Replies: 4
    Last Post: 02-22-2003, 04:25 PM
  5. FAQ cout
    By evilmonkey in forum FAQ Board
    Replies: 1
    Last Post: 10-07-2001, 11:32 AM