Thread: Structure Problem

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    19

    Structure Problem

    My code does not display the value that the user inputs, but displays all zeros. Why?








    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    struct Circle
    {
    	double radius; 
    	double diameter; 
    	double area; 
    	double circumference; 
    
    	Circle( )
    	{
    		radius = 0; 
    		diameter = 0; 
    		area = 0; 
    		circumference = 0; 
    
    	}
    
    };
    Circle getCircleData();
    void calculateCircleData (Circle &);
    void displayCircleData (Circle);
    
    int main()
    {
    	Circle c1; 
    
    	cout << "Welcome to our Circle Calculator" << endl;
    	cout << "--------------------------------" << endl;
    
    	getCircleData();
    	calculateCircleData (c1);
    	displayCircleData (c1);
    
    
    return 0;
    }
    
    Circle getCircleData( )
    {
    	Circle c1;
    	cout << "Enter the radius for the first circle: " << endl; 
    	cin >> c1.radius;
    
    	return c1;
    }
    
    void calculateCircleData (Circle &c1)
    {
    	c1.diameter = c1.radius *2;
    	c1.area = c1.radius * c1.radius * 3.14159;
    	c1.circumference = c1.radius * 2 * 3.14159; 
    
    }
    
    void displayCircleData (Circle c1)
    { 
    	cout << "The circle data is: " << endl;
    	cout << "Radius: " << c1.radius << fixed << setprecision(2) << endl; 
    	cout << "Diameter: " << c1.diameter << fixed << setprecision(2) << endl;
    	cout << "Area: " << c1.area << fixed << setprecision(2) << endl; 
    	cout << "Circumference" << c1.circumference << fixed << setprecision(2) << endl; 
    	
    
    }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The problem is that you don't use the Circle returned from getCircleData.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    38
    Why not pass the circle, defined in main, to getCircleData -- similar to how you did calculateCircleData?

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    19
    Well how do I use it??
    Because my assignement says that it has to be "getCircleData()" so I can't pass anything to it.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For starters:
    Code:
    Circle c1 = getCircleData();
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Basically the same question
    Circle Program Issues
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. poiner to structure problem
    By bluetxxth in forum C Programming
    Replies: 9
    Last Post: 02-25-2010, 04:56 PM
  2. problem getting structure to work in my function
    By Tom Bombadil in forum C Programming
    Replies: 18
    Last Post: 05-28-2009, 09:53 AM
  3. Problem with structure and class
    By Bargi in forum C++ Programming
    Replies: 3
    Last Post: 09-25-2007, 02:30 AM
  4. Problem with arrays inside a structure
    By babu in forum C Programming
    Replies: 4
    Last Post: 07-12-2007, 09:35 AM
  5. Problem checking for numeric value in a structure
    By ronkane in forum C++ Programming
    Replies: 4
    Last Post: 01-20-2002, 02:53 PM