Thread: ggggrrrr the functions

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    465

    ggggrrrr the functions

    I gave up on functions before because I could never seem to get them to work so I figured its about time I learned. I'm making a calculator to solve all of the equations we are using in physics.
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    float df(float appgrav=10, float time)
    {
    return ((.5*appgrav)*(time*time);
    }
    
    int main(int argc, char *argv[])
    {	
    	float acceleration;
    	float time;
    	float velocity;
    	float distance;
    	float speed;
    	float gravity=9.8;
    	float appgrav=10;
    
    	char begin;
    	
    	cout<<"What equation would you like to use? [d]istance fallen, [v]elocity,\n[h]horizontal distance, [a]verage velocity.";
    	cin>>begin;
    	if (begin=='d')
    	cout<<"Enter time:";
    	cin>>time;
    	df(appgrav,time);
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    Really messed up.
    My computer is awesome.

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    i. Your missing a paren.
    ii. You never assign the result of the computation to anything:
    Code:
    float result = df(appgrav, time);
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    - Don't get used to system(). Its a horrible function.

    - you can save some typing by declaring your variables like this :
    Code:
    float acceleration, time, velocity, distance;
    float speedm, gravity =9.8 , appgrav =10;
    they'd look prettier , too.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    float df(float appgrav=10, float time)
    {
    return (.5*appgrav)*(time*time);
    }
    If you are going to have a single default argument to a function taking two arguments like you are doing here, the default parameter must go with the second argument, like so:

    Code:
    float df(float time,float appgrav=10)
    {
        return (.5*appgrav)*(time*time);
    }
    Last edited by hk_mp5kpdw; 03-10-2005 at 07:58 PM. Reason: fixed extra parenthesis on code samples
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Additionally, some brackets wouldn't hurt.
    Code:
    	if (begin=='d')
    	{
    	    cout<<"Enter time:";
    	    cin>>time;
    	    cout << df(appgrav,time) << endl;
    	}
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  6. #6
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    While you're at it, take away the useless params in main, and switch EXIT_SUCCESS to 0. You're cooler than that crap, aren't you?

    And as was said earlier, don't use system() honey. As its name implies, it's system-dependent, and it might not run on someone else's machine. Something like 'cin.get()' might be a better choice, you see.
    Last edited by Krak; 03-10-2005 at 08:07 PM.

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    The reason I used system was because my compiler automatically puts it there and I forgot to change it. I already understand why I shouldn't use system().



    P.S. I got a compiler for my mac now yay!
    My computer is awesome.

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    It doesn't do what I want it to if you work the formula d=.5gt^2 it is 125 with 5 as t and 10 as g, but my program returns 250.
    My computer is awesome.

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by cerin
    It doesn't do what I want it to if you work the formula d=.5gt^2 it is 125 with 5 as t and 10 as g, but my program returns 250.
    Here's how to debug your program yourself. If you get a wrong answer, make sure that the formula is working on whatever you think is. That is, put output statements at strategic places in the program that indicate the values that the formula is using.

    So, for example, put the following statement in the beginning of your function:

    Code:
      cout << "In df: appgrav = " << appgrav << ", time = " << time << endl << endl;
    Here's another suggestion: since you indicate that you "could never seem to get functions to work", start with a simple case: no default arguments. Then make a simple program that shows whether the function can perform the calculation and return the correct value.

    A first effort would be something like this:

    Code:
    #include <iostream>
    
    using namespace std;
    
    float df(float appgrav, float time)
    {
      cout << "In df: appgrav = " << appgrav << ", time = " << time << endl << endl;
      return ((.5*appgrav)*(time*time));
    }
    
    int main(int argc, char *argv[])
    { 
      float time;
      float gravity;
      float distance;
    
      gravity = 10;
      time = 5;
    
      distance = df(gravity, time);
      
      cout << "distance = " << distance << endl;;
    
      return 0;
    }
    Regards,

    Dave

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM