Im stuck....

This is a discussion on Im stuck.... within the C++ Programming forums, part of the General Programming Boards category; I cannot get this program to return any values at all what am I doing wrong please help and thank ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    29

    Im stuck....

    I cannot get this program to return any values at all what am I doing wrong please help and thank you for visitng my post...
    Code:
    #include <iostream.h>
    #include <cmath>
    
    
    double CalculateDist(float, float, float, float);			//Function prototypes
    double CalculateRadius(float, float, float, float);
    double CalculateCircum(double, double);
    double CalculateArea(double, double);
    const double Pi = 3.1416;
    
    int main()
    {
    	float x1 = 0;    // Variables
    	float x2 = 0;
    	float y1 = 0;
    	float y2 = 0;
    	float dis = 0;
    	float rad = 0;
    	float cir = 0;
    	float are = 0;
    
    	
    	
    
    	cout << "Please enter the x coordinate for the center point " << endl;
    	cin >> x1;
    	cout << "Please enter the y coordinate for the center point " << endl;
    	cin >> y1;
    	cout << "Please enter the x coordinate for a point on the circle " << endl;
    	cin >> x2;
    	cout << "Please enter the y coordinate for a point on the circle " << endl;
    	cin >> y2;
    
    
    	cout << "The distance between the points is " << CalculateDist(x1,x2,y1,y2) << endl;
    	cout << "The radius of the circle is        " << CalculateRadius(x1,x2,y1,y2) << endl;
    	cout << "The circumfrence of the circle is  " << CalculateCircum(Pi,rad) << endl;
    	cout << "The area of the circle is          " << CalculateArea(Pi, rad) << endl;
    	
    		return 0;
    }
    
    //=====================================================================================================================
    // Functions
    
    double CalculateDist(float x1, float x2, float y1, float y2)
    {		
    	return sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2));
    	
    }
    
    double CalculateRadius(float x1, float x2, float y1, float y2)
    {
    	return	CalculateRadius(x1, x2, y1, y2);
    
    }
    
    double CalculateCircum(double Pi, double rad)
    {
    	return (2* Pi * rad);
    }
    
    double CalculateArea(double Pie, double rad)
    {
    	return (Pi * pow(rad, 2));
    }

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    1) cmath requires you to indicate namespace std somehow. math.h wouldn't, but it isn't standard.

    2) You have a never ending recursive function.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    29
    2) You have a never ending recursive function.


    Whats that????

    code update still get no values

    Code:
    #include <iostream>
    #include <math.h>
    
    using namespace std;
    
    
    double CalculateDist(float, float, float, float);			//Function prototypes
    double CalculateRadius(float, float, float, float);
    double CalculateCircum(double, double);
    double CalculateArea(double, double);
    const double Pi = 3.1416;
    double rad = 0;
    
    int main()
    {
    	float x1 = 0;    // Variables
    	float x2 = 0;
    	float y1 = 0;
    	float y2 = 0;
    	
    	cout << "Please enter the x coordinate for the center point " << endl;
    	cin >> x1;
    	cout << "Please enter the y coordinate for the center point " << endl;
    	cin >> y1;
    	cout << "Please enter the x coordinate for a point on the circle " << endl;
    	cin >> x2;
    	cout << "Please enter the y coordinate for a point on the circle " << endl;
    	cin >> y2;
    
    
    	cout << "The distance between the points is " << CalculateDist(x1,x2,y1,y2) << endl;
    	cout << "The radius of the circle is        " << CalculateRadius(x1,x2,y1,y2) << endl;
    	cout << "The circumfrence of the circle is  " << CalculateCircum(Pi,rad) << endl;
    	cout << "The area of the circle is          " << CalculateArea(Pi, rad) << endl;
    	
    		return 0;
    }
    
    //=====================================================================================================================
    // Functions
    
    double CalculateDist(float x1, float x2, float y1, float y2)
    {		
    	return sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2));
    	
    }
    
    double CalculateRadius(float x1, float x2, float y1, float y2)
    {
    	return	CalculateRadius(x1, x2, y1, y2);
    
    }
    
    double CalculateCircum(double Pi, double rad)
    {
    	return (2* Pi * rad);
    }
    
    double CalculateArea(double Pie, double rad)
    {
    	return (Pi * pow(rad, 2));
    }

  4. #4
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Code:
    double CalculateRadius(float x1, float x2, float y1, float y2)
    {
    	return	CalculateRadius(x1, x2, y1, y2);
    
    }
    That's recursive. The function calls itself [infinity] times.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    recursion = funct1(void){ funct1(); }

  6. #6
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Code:
    double CalculateRadius(float x1, float x2, float y1, float y2)
    {
    	return	CalculateRadius(x1, x2, y1, y2);
    
    }
    A recursive function is a function that keeps on calling itself over and over, as the one above.

    In some cases it's useful, but always inside of the function there are safegaurds put in that under certain conditions, the function will skip calling itself again, and return an actual value. (This can never be accomplished, though, if you call the function in the Return, as you've done, hence it's never-ending).

    [EDIT]
    Guess I was too slow on the trigger
    [/EDIT]

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    try changing this:


    return CalculateRadius(x1, x2, y1, y2);

    to this:

    return CalculateDist(x1, x2, y1, y2);
    ______________________________________
    if you use iostream you would logically use cmath and use namespace std. If you use iostream.h you would logically use math.h and not use a namespace.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    29
    final code
    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    void getData(float &, float &, float &, float &);
    double CalcDist(float, float, float, float);
    double CalcRadius(float, float, float, float);
    double CalcCircum(double, double);
    double CalcArea(double, double);
    void PrintAll(double, double, double, double);
    
    int main()
    {
    	float x1 = 0;
    	float x2 = 0;
    	float y1 = 0;
    	float y2 = 0;
    
    	double distance = 0;
    	double radius = 0;
    	double circumfrence = 0;
    	double area = 0;
    
    	const double Pi = 3.1416;
    
    	getData(x1, x2, y1, y2);
    	distance = CalcDist(x1, x2, y1, y2);
    	radius = CalcRadius(x1, x2, y1, y2);
    	circumfrence = CalcCircum(Pi, radius);
        area = CalcArea(Pi, radius);
    	PrintAll(distance, radius, circumfrence, area);
    	
    	return 0;
    }
    
    void getData(float &xcord1, float &xcord2, float &ycord1, float &ycord2)
    {
    	cout << "Please enter the x coordinate for the center point " ;
    	cin >> xcord1;
    	cout << "Please enter the y coordinate for the center point " ;
    	cin >> ycord1;
    	cout << "Please enter the x coordinate for a point on the circle " ;
    	cin >> xcord2;
    	cout << "Please enter the y coordinate for a point on the circle " ;
    	cin >> ycord2;
    }
    
    double CalcDist(float xcord1, float xcord2, float ycord1, float ycord2)
    {
    	return sqrt(pow(xcord1 - xcord2, 2) + pow(ycord1 - ycord2, 2));
    }
    
    double CalcRadius(float xc1, float xc2, float yc1, float yc2)
    {
    	double Temp = 0;
    	Temp = CalcDist(xc1, xc2, yc1, yc2);
    	return Temp;
    }
    
    double CalcCircum(double Pie, double rad)
    {
    	return (2* Pie * rad);
    }
    
    double CalcArea(double Pie, double rad)
    {
    	return (Pie * pow(rad, 2));
    }
    
    void PrintAll(double dis, double rad, double cir, double are)
    {
    	cout << "The distance between the points is :" << dis ;
    	cout << "The radius of the circle is        :" << rad ;
    	cout << "The circumfrence of the circle is  :" << cir ;
    	cout << "The area of the circle is          :" << are ;
    }

  9. #9
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Using:
    Code:
    return CalcDist(xc1, xc2, yc1, yc2);
    Would be fine too since the same function does not keep calling itself. All that would happen is that:
    Main calls CalcRadius
    CalcRadius calls CalcDist
    CalcDist Returns X
    CalcRadius Return X
    Done

    The recursion problem was that, before:
    Main calls CalcRadius
    CalcRadius calls CalcRadius
    CalcRadius calls CalcRadius
    CalcRadius calls CalcRadius
    And so forth, and you never actually got to the part of returning the actual value.

    But now that you called a seperate function, which actually returns a value, you would be fine to leave out the "Temp" variable
    Last edited by Epo; 10-11-2004 at 05:55 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-23-2006, 08:22 PM
  2. string array stuck:(
    By mass in forum C Programming
    Replies: 18
    Last Post: 05-22-2006, 05:44 PM
  3. Program stuck in infinite loop-->PLEASE HELP
    By Jedijacob in forum C Programming
    Replies: 5
    Last Post: 03-26-2005, 12:40 PM
  4. Stuck on random generating
    By Vegtro in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2003, 08:37 PM
  5. stuck ky
    By JaWiB in forum Tech Board
    Replies: 2
    Last Post: 06-15-2003, 09:28 PM

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