typecasting problem?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/* This program will accept the input of two
	integers, m and n.  If m is greater than n
	they will be used to calculate a Pythagorean triple, by
	the formulas:  ^2 means squared
		side1 = m^2 - n^2 
		side2 = 2 m n
		hypotenuse = square root of (side1^2 + side2^2) 
	these three numbers will always be the 3 sides
	of a right triangle
*/

#include <iostream>
#include <cmath>
using namespace std;

int main ()
{
	int m, n;
	int side1, side2;
	int hypotenuse;

	cout << "Enter m ";
	cin >> m;
	cout << "Enter n ";
	cin >> m;
	if (m > n)
	{
		side1 = int(pow(m,2)) - int(pow(n,2));
		side2 = 3 * m * n;
		hypotenuse =  sqrt(pow(side1,2))  + sqrt(pow(side2,2));
	}
	else
	{
		side1 = 0;
		side2 = 0;
		hypotenuse = 0;
		cout << "please enter m greater than n" << endl;
	}
	
	cout << "The two legs of the triangle are " << side1 << " and "
		<< side2 << endl << "and the hypotenuse is "
		<< hypotenuse << endl;
	return 0;
}


The error is said to be on the line with the side1 = in the if statement. is it my typecasting?
Last edited on
Is the int() really neccesary? It returns an int does it not?
You didn't give the error your compiler is reporting, but VC++2005 gives an 'ambiguous call' error when using pow.
See http://www.cplusplus.com/reference/clibrary/cmath/pow.html for the definition of pow.
The compiler will automatically change the result of pow() to int, but it cannot resolve pow(int,int) into any of the three overloaded functions.
The simplest fix I found was to change it to
 
   side1 = pow(m,2.0) - pow(n,2.0);

as the compiler can cope with pow(int, double).
Note that you will get the same error on line 30, with the same fix.
You also want to change line 26 to
 
   cin >> n;   //n not m. 
Hi,

This is off topic, but you will get different values for side1 (in line 29) and hypotenuse (line 30) than you have specified in comment lines in top of code. It's not important, of course, if this is only for testing ...

Comment lines:
side2 = 2 * m * n;
hypotenuse = sqrt(pow(side1,2) + pow(side2,2));

Code lines:
29: side2 = 3 * m * n;
30: hypotenuse = sqrt(pow(side1,2)) + sqrt(pow(side2,2));

Best regards,
TapaniS
If all the variables which you have declared aren't necessarily to be defined as int. then declare them as double.

Topic archived. No new replies allowed.