Bool Function not returning bool values?

This code that checks whether a certain point (x,y,z) object created by the point class exists within a sphere (point object, radius) object. I know the code works because of the cout statements, but the code doesn't return "true" or "false" when it runs "return true" and "return false".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  bool Sphere::in(Point P){
    // true: point P is in the sphere
    // false: point is not on the sphere
    // not tested: point is on boundary of sphere
    double x1= getCenter().getX();
    double x2= P.getX();

    double y1=getCenter().getY();
    double y2=P.getY();

    double z1=getCenter().getZ();
    double z2=P.getZ();

    double d= sqrt( (pow(x2-x1,2)+(pow(y2-y1,2))+ (pow(z2-z1,2))) );

    if (d>getRadius()) {
        //std::cout<<"false"<<std::endl;
        return false;
    } else {
        //std::cout<<"true"<<std::endl;
        return true;
    }

};


Any ideas? I feel like it's something pretty simple, but I just don't see it. Thanks!

Edit:
Here is the function in the header file. It's a member function.

 
  bool in(Point P);



I'm calling it in my test file, main.

1
2
3
4
5
6
7
8
9
10
11
12
13
14

#include "sphere.h"

int main(){

  Point P(1,2,3);
  Point P2(10,22,22);
  Sphere S1(P,3);
  S1.in(P);
  S1.in(P2);

  return 0;
}
Last edited on
Can you show the part of the code which is calling this function and where you're expecting to see true or false being evaluated? If you say that the cout statements are being reached then it's probably not a problem with the function itself..
The function looks fine. Show where you are calling it.
This is in the header file
1
2
3

  bool in(Point P);


It's a member function.

I'm calling it in my test file, main.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include "sphere.h"

int main(){

  Point P(1,2,3);
  Point P2(10,22,22);
  Sphere S1(P,3);
  S1.in(P);
  S1.in(P2);

  return 0;
}
How do you know what it is returning to main()? You aren't actually printing out the result of the function call! Put some cout << statements before the calls.

Incidentally, in your original code, why do you need so many getters for self? You are inside the class, so it will know perfectly well what centre and radius are from the data members.
Well, thing is I'm using 2 classes. Meaning I have a Point class (point.h) and a Sphere class (sphere.h). The getCenter() is from the Sphere class, and the getX() and getY() and getZ() is from the point class. But you're right about getCenter(), I replced it with "radius".

Also, if you look at line 17 and 20 from the original block of code in the post, I have cout statements. Granted they're commented out, but I tested using the statements, and they work.

Generally when you do "return true" it prints out "true", right? Same as "return 0" and "return 1", they print out 0 and 1 respectively. The assignment wants me to return true or false. But it's not returning "true" nor "false"
Last edited on
When it returns a value to main ... it just returns that value to main. It doesn't suddenly jump up and print anything! You are returning a value, not outputting it.

Change line 9 in your snippet for main to
cout << S1.in(P2);
or, if you want to print something other than 0 and 1 to
cout << boolalpha << S1.in(P2);
Last edited on
Ah! I had to read that thrice to understand what you meant. So just returning something to main won't do. It needs ostream to print it out in the first place. Which is why using cout is necessary.

I understand now, thanks!

Though I still don't understand your second line with boolalpha. I've never encountered this before, so I don't understand what it is doing. Correct me if I am wrong but, the bool alpha returns both the bool value and the nonbool(int) value, right?
@infinite1212,
With regards to boolalpha, I think the best thing is for you to try with and without. It will be obvious then what difference it makes. It does NOT return both true/false and 1/0 output: it will switch from the 0/1 to the full word form.

Also, put boolalpha and a line break (<<endl) for BOTH outputs at the end of main, or you will make yourself more confused.
Last edited on
closed account (1vRz3TCk)
infinite1212,
boolalpha is a stream manipulator, read about it here:
http://www.cplusplus.com/reference/ios/boolalpha/
Topic archived. No new replies allowed.