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.