What is wrong with this?

I am trying to write this such that [teaching c++ online course]:
1) reads a Cartesian point
2) calculates distance from origin
3) finds distance compared to last point
4) outputs one which is closer
5) if point within 0.01 to origin it terminates.

However when I compile it says: " d1 was not declared in this scope : line 34"

I would highly appreciate any help. Many thanks.

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
45
  #include <iostream>
#include <cmath>


struct point{
    double x;
    double y;
};

double d1= pdistance (point p1, point p2);


int main(){

    point pa, pb, o;
    o.x = 0;
    o.y=0;
    double d2=0 ;

    do{
      std::cout<< " enter the co-ordiantes of a point" << std::endl;
      std::cin >> pa.x >> pa.y ;

      double d1 = pdistance(pa, o);

      if(d1<d2){
        std::cout << " the closest point to the origin so far: " << pa.x << " " << pa.y << std::endl;
      }
      else{
        std::cout << " the closest point to the origin so far: " << pb.x << " " << pb.y << std::endl;
      }

      double d2=d1;

  }while(d1||d2 > 0.01);

}


double pdistance(point p1, point p2){
    return std::sqrt(
      std::pow((p1.x-p2.x), 2)+
      std::pow((p1.y-p2.y), 2)
    );
}



After an hour re reading this over and over again I can confirm it is driving me mad!
Last edited on
I don't see a definition of p1 and p2. Lines 10 and 11 are creating variables, and need these definitions.
Aren't p1 and p2 declared as the point type in line 10 in the function declaration?
No. Variables aren't created in function declarations, and line 10 isn't a declaration anyway; it's the invocation of a function (though one with incorrect syntax).

If you wish line 10 to be a function declaration, remove the assignment:

double pdistance (point p1, point p2);
Topic archived. No new replies allowed.