I have the following code and i dont understand why P4(3,7)is giving returnvalue true (1) , since here 7(ConTemp.XYCoord[1]) is bigger than 5 (ReCoordRo.XYCoord[1]), so i would like to know a way to monitor the boolean Rechteck1.contains(P4), watch windows doesnt give me that option, so how do i go about it ? Thx

#include <iostream>
#include <array>

 using namespace std;
    class Punkt {
        public: int XYCoord [2]={};
        void setupCoord (int x, int y){
            XYCoord[0]=x;
            XYCoord[1]=y;
        }
    };
    class Rechteck {
        public: Punkt ReCoordLu,ReCoordRo;

        double flaeche(double x, double y){
            double xy=x*y;
            return xy;
            }
        bool contains ( Punkt &p){
            Punkt *ConTemp1=&p;
            Punkt ConTemp ;
            ConTemp = *ConTemp1;

            if (ConTemp.XYCoord[0]>=ReCoordLu.XYCoord[0]&&ConTemp.XYCoord[1]<=ReCoordRo.XYCoord[1]){
                return true;}
            else{
                return false;}
                };
        bool contains (Rechteck &){
            if (1){
                return true;}
            else
                return false;
            }
        };
int main()
{/* Rechteck sharedRectangle (Rechteck a , Rechteck b){
            Rechteck c;
            return Rechteck c;
            } */

        Punkt P1,P2,P3,P4;
        P1.setupCoord(1,1);
        P2.setupCoord(5,5);
        P3.setupCoord(3,3);
        P4.setupCoord(3,7);
        Rechteck Rechteck1;
        Rechteck1.ReCoordLu.setupCoord(3,3);
        Rechteck1.ReCoordRo.setupCoord(9,9);
Rechteck1.contains(P4) 

        cout<<"hello"<<P2.XYCoord[0]<<Rechteck1.ReCoordLu.XYCoord[0]
        <<Rechteck1.flaeche(5,5)<<Rechteck1.contains(P4) ;

        return 0;};

Dani AI

Generated

Brief summary and practical pointers.

As found, the root cause in this thread was a setup/logic mix‑up (swapped or wrong corner values). That kind of mistake is common. Other frequent causes for a surprising boolean result are: incorrect inequality directions, assuming a coordinate system you don't have, or relying on a debugger to evaluate a function that the debugger refuses to call.

How to inspect booleans reliably

  • Print the value with readable text instead of relying on the watch window (many debuggers show 0/1). Example:
#include <iostream>
#include <iomanip>

std::cout << std::boolalpha
          << "contains: " << Rechteck1.contains(P4) << '\n';
  • Add watches for the raw pieces the function uses (e.g., P4.XYCoord[1], ReCoordLu.XYCoord[1], ReCoordRo.XYCoord[1]) rather than only the function call. That makes it obvious which inputs produce the result.
  • Use QuickWatch/Immediate to evaluate expressions like P4.XYCoord[1] <= Rechteck1.ReCoordRo.XYCoord[1]. Some debuggers will not evaluate member functions that might have side effects or that take non‑const refs, so examining fields directly is safer.
  • Compile without optimizations and with full debug info (MSVC Debug / g++ -g -O0) so local variables are preserved and evaluation works as expected.

Safer contains implementation

  • Take the point by const reference and mark the method const so it's clear it has no side effects.
  • Normalize corner order (min/max) so swapped setup calls won't break containment.
bool contains(const Punkt& p) const {
    int left  = std::min(ReCoordLu.XYCoord[0], ReCoordRo.XYCoord[0]);
    int right = std::max(ReCoordLu.XYCoord[0], ReCoordRo.XYCoord[0]);
    int top   = std::min(ReCoordLu.XYCoord[1], ReCoordRo.XYCoord[1]);
    int bottom= std::max(ReCoordLu.XYCoord[1], ReCoordRo.XYCoord[1]);
    return p.XYCoord[0] >= left && p.XYCoord[0] <= right
        && p.XYCoord[1] >= top  && p.XYCoord[1] <= bottom;
}

Quick checklist: print values with std::boolalpha, watch the raw coordinates, mark methods const and use const refs, normalize corners with min/max, run in Debug mode. That workflow catches swapped assignments and logic errors quickly.

Problem solved !It actually works looked like i just mistook Rechteck1.ReCoordLu.setupCoord(3,3)with
Rechteck1.ReCoordRo.setupCoord(9,9)

What IDE are you using?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.