Global variable not recognized as 'global'?

Here is the code:

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
46
47
48
49
50
51
52
#include<iostream>
#include<cmath>
using namespace std;

double distance=0;

struct Point {
       int x;
       int y;
       };
       
struct Circle {
       Point centre;
       int radius;
       };
       
void print_circle(const Circle& circle)
{
     cout<<"Circle #1"<<endl;
     cout<<"The Centre is: ("<<circle.centre.x<<", "<<circle.centre.y<<")"<<endl;
     cout<<"The Radius is: "<<circle.radius<<endl;
}

double circle_distance(const Circle& circle1,const Circle& circle2)
{
       double a, b, c;
       a=abs(circle1.centre.y-circle1.centre.x);
       b=abs(circle2.centre.y-circle2.centre.x);
       c=pow(a,2)+pow(b,2);
       
       distance=sqrt(c);
       
       return distance;
}

int circle_test(const Circle& circle1, const Circle& circle2)
{
    int x;
    x=abs(circle2.radius-circle1.radius);
    
    if (circle1.centre.x==circle2.centre.x&&circle1.centre.y==circle2.centre.y&&circle1.radius==circle2.radius)
    return 0;
    
    if (distance<x)
    return 1;
    
    if (distance>(circle2.radius+circle1.radius))
    return 2;
    
    else return 3;
    
}


The error is that "distance" is undefined, but I thought that since I put it at the top it would count as a global variable and it can be used anywhere in the program...? Any help would be great.

Also, in the last function, what is the syntax to be used to tell the program to perform various actions depending on whether the function returned 0,1,2 or 3?

Thanks for your time.
distance is not undefined. In opposite it's defined more than once. You have a name clash with std::distance(). That's an example why using namespace std; is evil

Also, in the last function, what is the syntax to be used to tell the program to perform various actions depending on whether the function returned 0,1,2 or 3?
I guess you mean switch
You the man
Topic archived. No new replies allowed.