this code COMPILES, but DOESNT WORK

This is a discussion on this code COMPILES, but DOESNT WORK within the C++ Programming forums, part of the General Programming Boards category; the problem is that in the set of if-statements (not the one in the function), the second two situations never ...

  1. #1
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    this code COMPILES, but DOESNT WORK

    the problem is that in the set of if-statements (not the one in the function), the second two situations never are true. the code in them is never executed. but, i know that i am actually checking for the right things because if i put an exit(0) in the one that checks for page1, it exits. if i put exit(0) in the others, it never exits. in the beginning i set active_page to page 1 with: active_page = page1;

    now, will that make things actually go to page1, not active_page when i blit like this:

    masked_blit(ship, active_page, 0,0, x, y,640,480);

    ???? (because thats what i want to happen)

    get what i mean? i want things to always blit to either page1 2 or 3, not actually onto active_page. active_page is used for telling what page to blit to.

    Code:
    BITMAP *ship, *page1, *page2, *page3, *active_page, *title;
    
    flip_page(&wPage, &page1, &page2, &page3, &active_page);
    
    if(active_page == page1) //this statement WORKS OK
    		{
    			clean_bitmap(page3);
    		}
    		if(active_page == page2) //statement never becomes TRUE
    		{
    		    clean_bitmap(page1);
    		}
    		if(active_page == page3)
    		{
    			clean_bitmap(page2);
    		}
    
    void flip_page(int *wPage, BITMAP** page1, BITMAP** page2, BITMAP** page3, BITMAP** active_page)
    {
    	if(*active_page == *page1)
    	{
            active_page = page2;
    		*wPage       = 2;
    	}
        if(*active_page == *page2)
    	{
            active_page = page3;
    		*wPage       = 3;
    	}
    	if(*active_page == *page3)
    	{
    		active_page = page1;
    		*wPage       = 1;
    	}
    
    }
    END_OF_FUNCTION(flip_page);

  2. #2
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    now that i think of it, active_page might not really be POINTING to the pages. it BECOMES the pages. therefore, we always blit onto active_page....or something like that. I DONT KNOW IM SO CONFUSED

  3. #3
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    flip_page(&wPage, &page1, &page2, &page3, &active_page);

    I think you should at least change this... to

    flip_page(wPage, &page1, &page2, &page3, &active_page);
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  4. #4
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    .....reading my reply to my own post....i came up with this: and i think i am CLOSER to achieving what i want, it still does not work though.
    Code:
    void flip_page(int *wPage, BITMAP** page1, BITMAP** page2, BITMAP** page3, BITMAP*** active_page)
    {
    	if(*active_page == &(*page1))
    	{
            *active_page = &*page2;
    		*wPage       = 2;
    	}
        if(*active_page == &(*page2))
    	{
            *active_page = &*page3;
    		*wPage       = 3;
    	}
    	if(*active_page == &(*page3))
    	{
    		*active_page = &*page1;
    		*wPage       = 1;
    	}
    
    }
    END_OF_FUNCTION(flip_page);
    
    flip_page(&wPage, &page1, &page2, &page3, &active_page);
    
    BITMAP *ship, *page1, *page2, *page3, **active_page, *title;
    
    if(active_page == &page1) //always true
    		{                        
    			clean_bitmap(page3); 
    		}
    		if(active_page == &page2) //never true
    		{
    		    clean_bitmap(page1);
    		}
    		if(active_page == &page3)
    		{
    			clean_bitmap(page2);
    		}

  5. #5
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    CHANGE IT TO...

    flip_page(wPage, &page1, &page2, &page3, &active_page);

    what you had...
    flip_page(&wPage, &page1, &page2, &page3, &active_page);

    IS WRONGGGGGG!!!!!!!!!!!>>>>>>>>>>>>................. .

    Also, seriously I would consider getting rid of the (**) double pointers all together since they are cuasing so much headaches and they are not needed or being used correctly. If you need Double pointers, create them initially and not in the functions.

    Like...
    BITMAP** active_page;
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  6. #6
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    wPage is NOT THE PROBLEM!!!!! DONT WORRY ABOUT wPage at all!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! and look at THIS:

    BITMAP *ship, *page1, *page2, *page3, **active_page, *title;

    that is how its been declared my reply!

  7. #7
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    also, i want active_page to POINT to the other pages. i dont want it to have the same "value" of the other pages! so active_page is a pointer to a pointer!

  8. #8
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    now, with all taht said, here is the code which has NO SYNTAX ERRORS:
    Code:
    BITMAP *ship, *page1, *page2, *page3, **active_page, *title; 
    
    active_page    = &page1;
    
    flip_page(&wPage, &page1, &page2, &page3, &active_page);
    
    if(active_page == &page1) //always true
    		{                        
    			clean_bitmap(page3); 
    		}
    		if(active_page == &page2) //never true
    		{
    		    clean_bitmap(page1);
    		}
    		if(active_page == &page3)
    		{
    			clean_bitmap(page2);
    		}
    
    void flip_page(int *wPage, BITMAP** page1, BITMAP** page2, BITMAP** page3, BITMAP*** active_page)
    {
    	if(*active_page == &(*page1))
    	{
            *active_page = &*page2;
    		*wPage       = 2;
    	}
        if(*active_page == &(*page2))
    	{
            *active_page = &*page3;
    		*wPage       = 3;
    	}
    	if(*active_page == &(*page3))
    	{
    		*active_page = &*page1;
    		*wPage       = 1;
    	}
    
    }
    END_OF_FUNCTION(flip_page);

  9. #9
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    A pointer holds an address! 2 pointers can hold the same address, ie. they both can point to the same VALUE/DATA.

    At least your pages, page_1 etc., don't need to be double ptrs(**). Active_page can be a double but it also does not need to be, and there is no real justification for the Triple ptr.

    Usually you use a double-ptr when you have a matrix or vector and want to swap in the rows or columns into the ptr.

    Code:
    //Here's the code without any double pointers
    BITMAP *ship, *page1, *page2, *page3, *active_page, *title;
    
    active_page    = page1;
    
    flip_page(wPage, page1, page2, page3, active_page);
    if(active_page == page1) //always true
    		{                        
    			clean_bitmap(page3); 
    		}
    		if(active_page == page2) //never true
    		{
    		    clean_bitmap(page1);
    		}
    		if(active_page == page3)
    		{
    			clean_bitmap(page2);
    		}
    
    void flip_page(int *wPage, BITMAP* page1, BITMAP* page2, BITMAP* page3, BITMAP* active_page)
    {
    	if(active_page == page1)
    	{
            active_page = page2;
    		*wPage       = 2;
    	}
        if(active_page == page2)
    	{
            active_page = page3;
    		*wPage       = 3;
    	}
    	if(active_page == page3)
    	{
    		active_page = page1;
    		*wPage       = 1;
    	}
    
    }
    END_OF_FUNCTION(flip_page);
    Here's a question, DOES Clean_Bitmap() fxn take a pointer or not??
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  10. #10
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    ok, i have a copy of it like that, but with that example, and i blit things onto active_page, will they actually go onto page1, 2, or 3?
    thats whati want it to do

  11. #11
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    Try it, it should work. If it doesn't you can change it to explicitly set the values of the active_page , *active_page = *page1.
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  12. #12
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    AHH, also something else - the if-statements outside of your flip_page() are not needed, only the first one will ever be true.

    do this...

    GET RID of the if-statements and add the clean_bitmap() to the flip_page() functions...

    Code:
    void flip_page(int *wPage, BITMAP* page1, BITMAP* page2, BITMAP* page3, BITMAP* active_page)
    {
    	if(active_page == page1)
    	{
            active_page = page2;
    		*wPage       = 2;
             clean_bitmap(page1); 
    	}
        if(active_page == page2)
    	{
            active_page = page3;
    		*wPage       = 3;
              clean_bitmap(page2); 
    	}
    	if(active_page == page3)
    	{
    		active_page = page1;
    		*wPage       = 1;
                                    clean_bitmap(page3); 
    	}
    
    }
    END_OF_FUNCTION(flip_page);
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  13. #13
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    still nothing happens on the screen...

  14. #14
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    take a look at my main program, it does not work now, but i think the pages are actually "flipping" at least:
    Attached Files Attached Files

  15. #15
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    Yeah, I don't see where you actually initialized the pages to something. I know they were created but what is in them, show me where you have put a bitmap in them.
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why doesn't the code work on right part of tree?
    By lastrial in forum C Programming
    Replies: 1
    Last Post: 05-16-2007, 07:42 PM
  2. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Linux Programming
    Replies: 0
    Last Post: 10-14-2002, 02:30 PM
  3. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Windows Programming
    Replies: 0
    Last Post: 10-14-2002, 02:29 PM
  4. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21