Hypotenuse problem...

Hello! I'm writing a program to calculate the hypotenuse of a right-angled triangle. At the moment, I'm just using the values 3 and 4.

Here are the two files I have so far...

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>


double distancecalculator (double a,double b);

int main()
{
   using namespace std;
   cout << "Please say 5...   " << distancecalculator(3,4) << endl;
   return 0;

}


...And the other program, distancecalculator.cpp:
1
2
3
4
double distancecalculator (double a, double b)
{
    return sqrt((a^2)+(b^2));
}


I'm not sure where I've gone wrong, although it says something about mixing "int" and "double" data types. Any help would be greatly appreciated!

Thanks,
Tom
Is it a warning or an error?
There are three errors and no warnings. They go:

error: invalid operands of types "double" and "int" to binary "operator^"
error: invalid operands of types "double" and "int" to binary "operator^"
error: "sqrt" was not declared in this scope

a^2 Rofl! Sorry.... (I knew this day would come.)
You want pow(a, 2);

Also you need to include <math.h> or <cmath> (not both) if you want to use the pow and sqrt functions.
Last edited on

^ is the xor bitwise operator. (Not what you want here.)
It takes two int's as operands so that's why you were getting that error.
Even if a had been an int you would not have gotten the correct answer.
Last edited on
Brilliant! It works now. Thanks a lot! :D
No problem.

1
2
3
while( shaking_finger )
  say( "Just don't do this again" );
jk();
Last edited on
Topic archived. No new replies allowed.