# help making a program run

• 02-22-2002
Dummies102
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.
• 02-22-2002
Invincible
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 :)
• 02-22-2002
Dummies102
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);
```double product; product = pyth( double one, double two );    //this would be enough to calculate the hypotenuse cout << product << endl;```