Help with functions

This is a discussion on Help with functions within the C++ Programming forums, part of the General Programming Boards category; You're also trying to pass a radius variable that doesn't exist: Code: cout <<"The area of the circle is: " ...

  1. #16
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    You're also trying to pass a radius variable that doesn't exist:
    Code:
    	cout <<"The area of the circle is: "
    		 << area(radius) << endl;
    You're going to have to pass it an actual radius somehow. Either:
    Code:
    double radius = radius(x1, y1, x2, y2);
    cout <<"The area of the circle is: "
    	 << area(radius) << endl;
    Or...
    Code:
    	cout <<"The area of the circle is: "
    		 << area(radius(x1, y1, x2, y2)) << endl;
    You're doing the same thing with circumference and diameter, so you're probably better off with the first choice. Otherwise you'll be calculating the radius multiple times.
    If you understand what you're doing, you're not learning anything.

  2. #17
    Registered User
    Join Date
    Jan 2011
    Posts
    16
    OMG that was like a little light buld went off.. itsme86 , YOU ROCK. ok I will change that and see what happens.


  3. #18
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    yep you can have more than one parameter!

    and also..I don't see where you declared a variable radius in your main.
    you pass an unidentified variable to area and the other function..
    You ended that sentence with a preposition...Bastard!

  4. #19
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by lostinprogramC+ View Post
    ok changed some code so here is the big sha bang again..... :-P

    ok silly question, can I have more than one variable in the parameter? or is it best ot have one parameter?
    Of course you can. Some Windows functions have 10 or more...
    You use what you need to get the job done.


    I still think that throwing the cumpter will help ;-P
    Then by all means go ahead and throw it...

    Or, if you'd like to do something just a little bit more sensible...
    Go back to your manuals, tutorials and text books and review the chapter on functions.

    People need to learn there is no shame in not knowing something and no shame in pulling out the books for a second look. Very typically when I'm programming I will have the IDE open (of course) as well as help files for the function calls, a web page or two, a tutorial or book and very often one or more previous sources I've written... I learned a long time ago there's no way anybody is going to memorize all this stuff. The programmer's major skills are 1) the ability to set pride aside and 2) knowing how to look stuff up when you need it.

  5. #20
    Registered User
    Join Date
    Jan 2011
    Posts
    16
    Eman- your right I didn't declare a variable of radius. I will fix that Knowing that I can have more then one parameter helps. Thank you.

    Tater - Thank you for the good advice. I am just learning all of this and believe me I have gone through the section on functions several times. lol... Plus looked it up online until I came across this wonderful forum. (btw that's what stopped me from throwing the computer earlier) I think the worst part about this class is that it is an online class and I have MANY questions. I find that talking through a problem noemally helps me to come to the solution and find the mistakes that I have made.

  6. #21
    Registered User
    Join Date
    Jan 2011
    Posts
    16
    itsme86 - thank you for the clarification. I can now get the code to actually run... kinda lol
    when I go to debug the console actually comes up and askes me to put in a center point, then it hangs. Have to close out the console window but have no errors to try and figure out what the issues is.

    here is my improved code (thanks to all of you)

    Code:
    //***************************************
    //* CISP231: C++ Programming I          *
    //*Programming Exercises Chapter 6 #6   *
    //*Instructor: Dr. Loay Alnaji          *
    //*Created By: Jennifer Tichelaar       *
    //*Date Created: Jan 19, 2011           *
    //***************************************
    
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    		
    	double distance (double x1, double y1, double x2, double y2)		//function to figure out the distance
    	{  
    		double dx = x2 - x1;  
    		double dy = y2 - y1;  
    		double dsquared = dx*dx + dy*dy;  
    		double result = sqrt (dsquared);  
    		return result;
    	} 
    		
    	double radius (double x1, double y1, double x2, double y2)			//function to figure out the radius
    	{  
    		double radius = distance (x1, y1, x2, y2);  
    		return radius;
    	} 
    
    	double circumference(double radius)					//function to figure out the circumference
    	{
    		double circumference = 3.1416 * (radius * 2);
    		return circumference;
    	}  
    
    	double area(double radius)								//function to figure out the area
    	{   
    		double area = 3.1416 * radius * radius;   
    		return area;
    	} 
    
    	double diameter(double radius)							//function to figure out the diameter
    	{
    		double diameter = 2 * radius; 
    		return diameter;
    
    	}
    
    
    int main()
    { 
    	double x1;								//variable to store the center point of the circle
    	double x2;								//variable to store the second point on the circle
    	double y1;								//variable to store the center point of the circle
    	double y2;								//variable to store the second point on the circle
    
    	cout <<"Enter a center point: ";
    	cin  >> x1 >> y1; 
    		
    	cout <<"Enter a point on the circle: ";
    	cin  >> x2 >> y2; 
    	
    	
    	cout <<"The distance of the circle is: "
    		 << distance(x1, y1, x2, y2) 
    		 << endl; 
    	
    	//area return
    	cout <<"The area of the circle is: "
    		 << area(radius(x1, y1, x2, y2)) << endl; 
    	
    	//radius return
    	cout <<"The radius of the circle is: "
    		 << radius(x1, y1, x2, y2) << endl; 
    	
    	//circumference return
    	cout <<"The circumference of the circle is: "
    		 << circumference(radius(x1, y1, x2, y2)) << endl; 
    	
    	//diameter
    	cout <<"The diameter of the circle is: "
    		 << diameter(radius(x1, y1, x2, y2))  << endl; 
    
    	return 0;
    
    }

  7. #22
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    What do you mean it hangs?
    It works for me..
    You ended that sentence with a preposition...Bastard!

  8. #23
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The way you have your input set up, you cannot type in any punctuation like parentheses or commas, and you have to have a space between your numbers.

  9. #24
    Registered User
    Join Date
    Jan 2011
    Posts
    16
    FOR THE LOVE OF... SERIOUSLY!!!!!

    then why won't it work for me???? *pouts*

    does it actually ask all the questions and return all the information?

  10. #25
    Registered User
    Join Date
    Jan 2011
    Posts
    16
    tabstop - can you please provide an example of what you are explaining? (in other words... I don't get it? lol)

  11. #26
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    for example
    enter in the console
    12 13 and press enter
    or separately as in

    12 and press enter
    second input: 13 and press enter
    or do
    cin>> x
    cin >> y
    You ended that sentence with a preposition...Bastard!

  12. #27
    Registered User
    Join Date
    Jan 2011
    Posts
    16
    Eman - OMG it worked! now I just need to put a pause in so that I can see all of the answers.

    THANK YOU to everyone. For helping me, pointing out the syntax errors and how to call a function properly.

    Seriously all of you should get a medal for this!

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of functions
    By frktons in forum C Programming
    Replies: 29
    Last Post: 06-30-2010, 10:51 AM
  2. Need help with functions
    By jusfry01 in forum C Programming
    Replies: 2
    Last Post: 05-22-2010, 07:25 PM
  3. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 06:30 PM
  4. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 12:50 PM
  5. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM

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