Thread: Circle Program Issues

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

    Smile Circle Program Issues

    My assignment is to have a program that creates a structure called Circle that has radius, diameter, area, and circumference. The program also needs 4 functions under the structure. Circle getCircleData(); void calculateCircleData(Circle &); void displayCircleData(Circle); and bool tryAgain();. Right now my code has a "do" statement in between the main and the first function, because it has to be able to run the functions again, with ANOTHER circle, while the bool statement returns true. I know that won't work because the "do" is not inside any function. But I don't know where to put it. I think my program has other problems too, but I'm not sure what they are. Anyways, if you could help me out, it would be greatly appreciated.


    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); 
    bool tryAgain(); 
    
    int main()
    {
    
    	cout << "Welcome to our Circle Calculator" << endl;
    	cout << "--------------------------------" << endl;
    
    
    return 0; 
    }
    do
    {
    
    Circle getCircleData ( )
    {
    	Circle c1;  
    
    	cout << "Enter the radius of the circle (number greater than 0)" << endl; 
    	cin >> c1.radius; 
    	
    	return c1; 
    }
    
    void calculateCircleData (Circle &value)
    {
    	Circle c1; 
    	c1.diameter = (c1.radius * 2); 
    	c1.area = (c1.radius * c1.radius * 3.14159);
    	c1.circumference = (2 * c1.radius * 3.14159);
    
    }
    
    
    void displayCircleData (Circle)
    {
    	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; 
    }
    }
    while (tryAgain());
    
    bool tryAgain ()
    {
    	char again;
    
    	cout << "Do you want to try again? (y/n)" << endl; 
    	cin >> again; 
    
    	if ((again == 'y')||(again == 'Y'))
    	{
    		return 1; 
    	}
    	else 
    	{
    		return 0; 
    	}
    
    
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You would put the function calls (not the body of the functions themselves!) where you want them to happen. Main is usually a good place to start.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    19
    Ohhh duh!
    okay. here's what I have now, and it's running, but all the values are being displayed as 0 still. What am I doing wrong?


    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); 
    bool tryAgain(); 
    
    int main()
    {
    	Circle c1; 
    
    	cout << "Welcome to our Circle Calculator" << endl;
    	cout << "--------------------------------" << endl;
    do
    {
    	getCircleData( ); 
    	calculateCircleData (c1);
    	displayCircleData (c1);
    	tryAgain( );
    }
    while (tryAgain());
    
    
    
    return 0; 
    }
    
    Circle getCircleData ( )
    {
    	Circle c1;  
    
    	cout << "Enter the radius of the circle (number greater than 0)" << endl; 
    	cin >> c1.radius; 
    	
    	return c1; 
    }
    
    void calculateCircleData (Circle &value)
    {
    	Circle c1; 
    	c1.diameter = (c1.radius * 2); 
    	c1.area = (c1.radius * c1.radius * 3.14159);
    	c1.circumference = (2 * c1.radius * 3.14159);
    
    }
    
    
    void displayCircleData (Circle)
    {
    	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; 
    }
    
    
    bool tryAgain ()
    {
    	char again;
    
    	cout << "Do you want to try again? (y/n)" << endl; 
    	cin >> again; 
    
    	if ((again == 'y')||(again == 'Y'))
    	{
    		return 1; 
    	}
    	else 
    	{
    		return 0; 
    	}
    
    
    }

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    109
    You've initialized all of the circle's values to 0 and are using that in your calculations.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    19
    Well I just initialized them all as 0 in my default thing in my structure. For some reasons the calculations in my functions aren't working.

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    109
    Right, look at your getCircleData( ) function. You don't keep the value entered, so the circle is still initialized to 0.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    19
    Oh.. I thought by putting the .radius after c1, it put the new inputted value into radius? How do I make the value that the user put in the new radius value?

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    109
    Yes, correct, but that function has a local circle object c1 that is not the same as the one you declared in main. In main, you want to store the returned value of getCircleData() in c1.

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    19
    like,
    c1 = getCircleData; ?
    Do I need to do that with the rest of the functions too?

  10. #10
    Registered User
    Join Date
    Mar 2010
    Posts
    109
    No, but the other 2 circle functions are incorrect as well. You should study a little bit more about passing by reference and passing by value, as well as getting a little more understanding of your function arguments.

    You pass data to your functions but then declare a new object inside the function and use that object instead of the data you passed, which is what you really want to use. E.g., in calculateCircleData(), you should be doing calculations on 'value' not 'c1'. 'value' is the same object as the 'c1' object you declared in main.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hi, Quiz C program Assignment updated
    By Eman in forum C Programming
    Replies: 19
    Last Post: 11-22-2009, 04:50 PM
  2. Continued issues with Winows Errors when running program
    By hpteenagewizkid in forum C Programming
    Replies: 6
    Last Post: 11-14-2006, 03:51 PM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  4. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 05:23 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM