Thread: help making a program run

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    33

    help making a program run

    I've just been reading about functions and thought I'd give them a shot in a program, but I'm having a hard time getting it to compile. One way I try it gives me errors about doubles in my function prototype not being identified, but when I define the prototype of the function inside the main(), it gives me the error: "pyth' : local function definitions are illegal". I'm pretty sure this is a newb question since I'm just learning about functions, but if anyone could help out I'd appreciate it. my code's below.


    #include <iostream.h>
    #include <math.h>
    #include <conio.h>

    void pyth(void)
    {
    c = pow(a, 2);
    d = pow(b, 2);
    e = c + d;
    f = sqrt(e);
    }

    int main()
    {
    double a, b, c, d, e, f, z;
    int i;
    cout << "This program is to be used to figure out the side of a right triangle using Pythagoran's Theorum. if you want to know the background of pythagoran's theorum, type 3"<<endl;
    for (;;)
    {
    cout << "To find out the length of the hypotenuse, press 1, and to find the length of a leg, press 2, to check to see if a triangle is right, press 3, for help, type 5 and to exit, press 9."<<endl;
    cin >>i;
    switch (i)
    {
    case 5:
    cout << "basicly Pythags Theorum is used to find the length of the hypotenuse, or leg of a right triangle, and only a right triangle. Incidently, since it;s only used to find out a right triangle, you can use it to find out if the triangle in question is square or not if you have the length of the two sides. The theorum finds out the length of a side using the fromula a^2 + b^3 = c^2(for the hypotenuse), or c^3 - b^2 = a^2. Then after you have the answer, you square root it to get the side. This program does this very fast and easily.\n\n\n"<<endl;
    break;
    case 9:
    return 0;

    case 1:
    cout << "Please enter the length of one side, then the other."<<endl;
    cin >>a>>b;
    pyth();
    cout << "the length of the hypotenuse is " <<f<<" and the length of it before being rooted is "<<e<<".\n\n\n";
    break;
    case 2:
    cout << "Please enter the length of one side, then the hypotenuse "<<endl;
    cin >>a>>b;
    pyth();
    cout << "the length of the missing side is " <<f<<", and before being square rooted, is "<<e<<".\n\n\n";
    break;
    case 3:
    cout << "please enter the length of two sides " <<endl;
    cin >>a>>b;
    pyth();
    cout << "\nNow, please enter the length of the hypotenuse ";
    cin >>z;
    if (z == f)
    cout <<"yes, the triangle is a right triangle\n\n\n";
    else if (z < f)
    cout << "Nope, you've got yourslef an acute triangle\n\n\n";
    else if (z > f)
    cout << "Nope, you've got yourself an obtuse triangle\n\n\n";
    break;
    default:
    cout <<"please enter a valid entry"<<endl;
    }
    }
    return 0;
    }



    thanks
    oh, one more question, the way I get my programs to run forever in I do a for loop with no perameters, like
    for (;;)
    and it seems to work fine, but I'm sure this is just a loop hole and there's a real way of doing this, if anyone who knows could tell me, thanks.

  2. #2
    Registered User Invincible's Avatar
    Join Date
    Feb 2002
    Posts
    210
    You need to set up your function to accept parameters and return a value. To do this, it needs a type. Like so:


    Code:
    int addTheseNumbers(int a, int b)
    {
         int c = a+b;
         return c;
    }
    int main()
    {
         int x;
         int y;     
    
         cin >> x;
         cin >> y;
    
         //call our function, the return variable 'c' is stored in 'result'
         int  result = addTheseNumbers(x, y); 
    
         cout << result << endl;
    }
    For a function to return a value to main, it must have a type. To pass variables to a function it must have parameters (arguments). In this case the function takes 2 integers. These integers that you pass don't necessarily have to have the same name as the integers that you use for the parameters. As you can see in the code above, int x and int y are passed to the function parameters int a and int b. The function then returns an integer value of c and it is stored in result.

    As for your other question: use a while statement.

    Code:
    int main()
    {
         int x = 0;
    
         cout << "Enter -1 to quit." << endl;
    
         while(x >= 0)
         {
               cin >> x;
               cout << x << endl;
         }
         return 0;
    }
    Hope that helps
    Last edited by Invincible; 02-22-2002 at 03:15 PM.
    "The mind, like a parachute, only functions when open."

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    33
    wow, that cleared a lot up, but I'm still having probloms with getting the function to work, this is what I've done, but still some questions:


    #include <iostream.h>
    #include <math.h>
    #include <conio.h>

    double pyth(double s1, double s2, double in1, double in2, double sqAdd, double product);


    int main()
    {
    double z, in1, in2, product; //I want x and y to be the inputs and assign them to be c and d in the function, if that's legal.
    int i;
    cout << "This program is to be used to figure out the side of a right triangle using Pythagoran's Theorum. if you want to know the background of pythagoran's theorum, type 3"<<endl;
    for (;;)
    {
    cout << "To find out the length of the hypotenuse, press 1, and to find the length of a leg, press 2, to check to see if a triangle is right, press 3, for help, type 5 and to exit, press 9."<<endl;
    cin >>i;
    switch (i)
    {
    case 5:
    cout << "basicly Pythags Theorum is used to find the length of the hypotenuse, or leg of a right triangle, and only a right triangle. Incidently, since it;s only used to find out a right triangle, you can use it to find out if the triangle in question is square or not if you have the length of the two sides. The theorum finds out the length of a side using the fromula a^2 + b^3 = c^2(for the hypotenuse), or c^3 - b^2 = a^2. Then after you have the answer, you square root it to get the side. This program does this very fast and easily.\n\n\n"<<endl;
    break;
    case 9:
    return 0;
    case 1:
    cout << "please enter the length of one leg, then the other.\n";
    cin >>in1>>in2;
    cout << "the length of the hypotenuse is " <<product<<" and the length of it before being rooted is .\n\n\n";
    break;
    case 2:
    cout << "Please enter the length of one side, then the hypotenuse "<<endl;
    cin >>in1>>in2;
    cout << "the length of the missing side is " <<product<<", and before being square rooted, is .\n\n\n";
    break;
    case 3:
    cout << "please enter the length of two sides " <<endl;
    cin >>in1>>in2;
    cout << "\nNow, please enter the length of the hypotenuse ";
    cin >>z;
    if (z == product)
    cout <<"yes, the triangle is a right triangle\n\n\n";
    else if (z < product)
    cout << "Nope, you've got yourslef an acute triangle\n\n\n";
    else if (z > product)
    cout << "Nope, you've got yourself an obtuse triangle\n\n\n";
    break;
    default:
    cout <<"please enter a valid entry"<<endl;
    }
    }
    }

    double pyth(double s1, double s2, double in1, double in2, double sqAdd, double product)
    {
    s1 = pow(in1, 2);
    s2 = pow(in2, 2);
    sqAdd = s1 + s2;
    product = sqrt(sqAdd);
    return product;
    }

    did anyone notice anything I did wrong? the program compiles, but the problom is it doesn't return the right value.
    thanks again for the help.
    Last edited by Dummies102; 02-22-2002 at 11:47 AM.

  4. #4
    Unregistered
    Guest
    you arent calling the function anywhere. the best way to do it is to have a product variable which is assigned the return value of the function.
    eg
    Code:
    double product;
    
    product = pyth( double one, double two );     //this would be enough to calculate the hypotenuse
    cout << product << endl;
    PS take a careful look at the code sample above, look at where the variables are declared and how they are passed to the function

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Run dos program invisibly?
    By chico1st in forum Windows Programming
    Replies: 2
    Last Post: 01-07-2009, 10:07 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. Help me with my basic program, nothing I create will run
    By Ravens'sWrath in forum C Programming
    Replies: 31
    Last Post: 05-13-2007, 02:35 AM
  4. Replies: 2
    Last Post: 12-22-2006, 08:45 PM
  5. Making standalone APP run in background
    By hart in forum Windows Programming
    Replies: 3
    Last Post: 02-27-2005, 11:20 AM