-1

I have a problem with my C++ code where Im doing two steps in one with regards to pythagoras.

Im getting the length of a and b at the same time as doing the pythagoras function but my variable always equals 0 even though the X and Y points show up with their respective values.

Unfortunatly due to this when square rooting the resut it procceds with an error as you cannot square root 0.

It looks kinda complicated but this was the neatest I could get it

hypPrevious = ( 
                ( *(previousCalcp->getX()) - *(currentCalcp->getX()) ) 
                *   
                ( *(previousCalcp->getX()) - *(currentCalcp->getX()) )
              ) 
              +
              ( 
                ( *(currentCalcp->getY()) - *(previousCalcp->getY()) ) 
                *   
                ( *(currentCalcp->getY()) - *(previousCalcp->getY()) )
              );
tbodt
  • 16,609
  • 6
  • 58
  • 83
Harvey
  • 1,320
  • 3
  • 13
  • 33

1 Answers1

1

With the information you gave us, the best advice I can give you is rewriting your code and add a debugging line to check what is going wrong:

int x1 = *(previousCalcp->getX());
int y1 = *(previousCalcp->getY())
int x2 = *(currentCalcp->getX());
int y2 = *(currentCalcp->getY());
int dx = x1 - x2;
int dy = y1 - y2;
hypPrevious = dx*dx + dy*dy;
std::cout << "(" << x1 << "," << y1 << ") and (" << x2 << "," << y2 << ") resulted in " << hypPrevious << std::endl;

Don't forget to change the type int to whatever you are using in your code.

Danvil
  • 22,240
  • 19
  • 65
  • 88
  • This is probably a good idea, Its getting a little confusing... Sorry if this sounds silly but i've used cout many times, but what does it do when you put 'std::' in front? – Harvey Apr 18 '14 at 17:16
  • You have to add `std::` when you do not use `using namespace std;` and minimize namespace pollution. – Danvil Apr 18 '14 at 17:20