TREE HELP!!!

Discussion in 'C++' started by Don Hedgpeth, Nov 9, 2005.

  1. Don Hedgpeth

    Don Hedgpeth Guest

    So I have this piece of code running and it comes from a program posted
    last night under nested problems.

    When I run this piece the program will return that the conditional on the
    line after the for loop is true and print but never enters the scope of
    the nested if statment.

    Please any help will be greatly appreciated.


    TreeNode * getFlagNode() {
    printf("IN GETFLAG!\n");
    vector< TreeNode *>::iterator p1;
    for(p1 = children->begin();p1 !=
    children->end();++p1){
    printf("%d\n", (*p1)->payload->getX()==maze->get_flag_row() &&
    (*p1)->payload->getY()maze->get_flag_col());
    if((*p1)->payload->getX()
    == maze->get_flag_row() && (*p1)->payload->getY() == maze->get_flag_col())
    {

    printf("RETURN NEXT");

    return *p1;
    }
    }
    }
    Don Hedgpeth, Nov 9, 2005
    #1
    1. Advertising

  2. Don Hedgpeth

    Kai-Uwe Bux Guest

    Don Hedgpeth wrote:

    >
    > So I have this piece of code running and it comes from a program posted
    > last night under nested problems.
    >
    > When I run this piece the program will return that the conditional on the
    > line after the for loop is true and print but never enters the scope of
    > the nested if statment.
    >
    > Please any help will be greatly appreciated.
    >
    >
    > TreeNode * getFlagNode() {
    > printf("IN GETFLAG!\n");
    > vector< TreeNode *>::iterator p1;
    > for(p1 = children->begin();p1 !=
    > children->end();++p1){
    > printf("%d\n", (*p1)->payload->getX()==maze->get_flag_row() &&
    > (*p1)->payload->getY()maze->get_flag_col());


    Shouldn't that be:

    (*p1)->payload->getY() == maze->get_flag_col());


    > if((*p1)->payload->getX()
    > == maze->get_flag_row() && (*p1)->payload->getY() == maze->get_flag_col())
    > {
    >
    > printf("RETURN NEXT");
    >
    > return *p1;
    > }
    > }
    > }



    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Nov 9, 2005
    #2
    1. Advertising

  3. I think that the code goes as follows:
    TreeNode * done;
    {
    printf("Found the flag\n");
    done = ((*myMoves)[x])->getFlagNode();
    printf("PEACE WE'RE OUT!");
    }

    ....

    TreeNode * getFlagNode() {
    vector< TreeNode *>::iterator p1;
    for(p1 = children->begin();p1 != children->end();++p1){
    printf("%d\n",((*p1)->payload->getX() ==
    maze->get_flag_row() && (*p1)->payload->getY() ==
    maze->get_flag_col()));
    if((*p1)->payload->getX() == maze->get_flag_row() &&
    (*p1)->payload->getY() == maze->get_flag_col()){
    printf("RETURN FLAG!\n");
    return *p1;
    }
    }
    }

    output looks like this:
    Found the flag
    1
    RETURN FLAG!


    What don't understand is what happens to the program is does fault it just
    hangs maybe someone can help me see what is wrong because I am new to c++
    this type of runtime error does not mean anything to me.

    On Wed, 9 Nov 2005, Kai-Uwe Bux wrote:

    > Don Hedgpeth wrote:
    >
    >>
    >> So I have this piece of code running and it comes from a program posted
    >> last night under nested problems.
    >>
    >> When I run this piece the program will return that the conditional on the
    >> line after the for loop is true and print but never enters the scope of
    >> the nested if statment.
    >>
    >> Please any help will be greatly appreciated.
    >>
    >>
    >> TreeNode * getFlagNode() {
    >> printf("IN GETFLAG!\n");
    >> vector< TreeNode *>::iterator p1;
    >> for(p1 = children->begin();p1 !=
    >> children->end();++p1){
    >> printf("%d\n", (*p1)->payload->getX()==maze->get_flag_row() &&
    >> (*p1)->payload->getY()maze->get_flag_col());

    >
    > Shouldn't that be:
    >
    > (*p1)->payload->getY() == maze->get_flag_col());
    >
    >
    >> if((*p1)->payload->getX()
    >> == maze->get_flag_row() && (*p1)->payload->getY() == maze->get_flag_col())
    >> {
    >>
    >> printf("RETURN NEXT");
    >>
    >> return *p1;
    >> }
    >> }
    >> }

    >
    >
    > Best
    >
    > Kai-Uwe Bux
    >
    Chad E. Dollins, Nov 9, 2005
    #3
  4. Chad E. Dollins wrote:
    > I think that the code goes as follows:
    > TreeNode * done;
    > {
    > printf("Found the flag\n");
    > done = ((*myMoves)[x])->getFlagNode();
    > printf("PEACE WE'RE OUT!");
    > }
    >
    > ...
    >
    > TreeNode * getFlagNode() {
    > vector< TreeNode *>::iterator p1;
    > for(p1 = children->begin();p1 != children->end();++p1){
    > printf("%d\n",((*p1)->payload->getX() ==
    > maze->get_flag_row() && (*p1)->payload->getY() ==
    > maze->get_flag_col()));
    > if((*p1)->payload->getX() == maze->get_flag_row() &&
    > (*p1)->payload->getY() == maze->get_flag_col()){
    > printf("RETURN FLAG!\n");
    > return *p1;
    > }
    > }
    > }
    >
    > output looks like this:
    > Found the flag
    > 1
    > RETURN FLAG!
    >
    >
    > What don't understand is what happens to the program is does fault it
    > just hangs maybe someone can help me see what is wrong because I am new
    > to c++ this type of runtime error does not mean anything to me.
    >


    Since your program is hanging when you execute a return statement the
    most likely thing is that you've corrupted the program stack.

    Hard to see what might have caused that in the code you've posted but
    the usual reason is that you're using an array somewhere and you've
    written past the end of that array.

    john
    John Harrison, Nov 9, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ramkumar Menon

    B+ Tree versus Ternary Search Tree

    Ramkumar Menon, Aug 16, 2005, in forum: Java
    Replies:
    2
    Views:
    1,584
    Roedy Green
    Aug 16, 2005
  2. Ramkumar Menon

    B+ Tree versus Ternary Search Tree

    Ramkumar Menon, Aug 16, 2005, in forum: Java
    Replies:
    0
    Views:
    420
    Ramkumar Menon
    Aug 16, 2005
  3. Ramkumar Menon

    B+ Tree versus Ternary Search Tree

    Ramkumar Menon, Aug 16, 2005, in forum: Java
    Replies:
    1
    Views:
    437
    Andrew Thompson
    Aug 16, 2005
  4. Joris Gillis
    Replies:
    2
    Views:
    1,521
    Joris Gillis
    Jun 16, 2006
  5. Stub

    B tree, B+ tree and B* tree

    Stub, Nov 12, 2003, in forum: C Programming
    Replies:
    3
    Views:
    10,090
Loading...

Share This Page