Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have the following code and i don`t 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

<pre>#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;};


What I have tried:

watch windows , call stacks windows
Posted
Updated 11-Mar-17 4:40am

Um.
Look at your contains method:
bool contains (Rechteck &){
    if (1){
        return true;}
    else
        return false;
    }
It always returns true because 1 is a non-zero constant and any nonzero value is "true" in C / C++
 
Share this answer
 
Comments
Member 13050643 11-Mar-17 5:30am    
Thats if i would have entered a (Rechteck)rectangle but i entered a (Punkt)point P4 ;Rechteck1.contains(P4)-> Punkt P1,P2,P3,P4; so the bool contains ( Punkt &p) method is applying and if i change P4 value to (77,-78) i actually do get a 0 output. Still dont know why .
OriginalGriff 11-Mar-17 5:37am    
So put a breakpoint on the line
Punkt *ConTemp1=&p;
and see what is happening.
Member 13050643 11-Mar-17 5:58am    
It actually works looked like i just mistook Rechteck1.ReCoordLu.setupCoord(3,3)with
Rechteck1.ReCoordRo.setupCoord(9,9); anyways thx
Problem solved !It actually works looked like i just mistook Rechteck1.ReCoordLu.setupCoord(3,3)with
Rechteck1.ReCoordRo.setupCoord(9,9)
 
Share this answer
 
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900