long page of source code and one problem: where to put return(0)???

Started by
7 comments, last by laserbeak43 16 years, 8 months ago
hi, i've been reading this book that i've been determined to finish for a very long time now. all through the book, i've encountered syntax errors that i had to fix. i don't know if this is good or bad, but it's an extra part of the learning process for me. it teaches me how to get rid of errors. so i'm in the last chapter of the book where you make a game using windows programming and directx. i'm reading the code and typing it in and as i get to the end of the code, the final

return( 0 );
}
is outside the rest of the code. the last curly brace doesn't enclose return(0). it's totaly outside of the WndProc(); function. so i look up and down the code in my book and my editor, counth the braces and everything. but i can't seem to find out where to put this return. so i'm thinking ok i'll edit it to put it in myself. and then i think, is this return even necessary since the case statement will return default? very lost. if you want to looks at the code, i don't know which part to send. so i'll send the whole thing. but the problem is most noticable at the bottom(to a newb such as myself that is).

//pirates.cpp

#include "globals.h"

int WINAPI WinMain(/*Win32 entry-poin routine */
                        HINSTANCE hInst,
                        HINSTANCE hPreInst,
                        LPSTR lpszCmdLine,
                        int nCmdShow)
{
    //calll initApp to do initialization
    InitApp(hInst, nCmdShow);
    //call DoEventLoop to handle the event loop
    return DoEventLoop();
}

/*callback procedure*/
LRESULT CALLBACK WndProc( HWND hWnd, UINT messg, WPARAM wParam, LPARAM lParam)
//this function processes user input
//and the timers used to run the game
{
    static bool inCity = false; //stores if the player is in a city
    switch(messg)//figure out which message is being sent
    {
        case WM_CREATE://if the window is being CreateD
            //do directx initianlization
            initializeDirectX(hWnd);
            CreateTowns();//create all of the town objects

            //the title screen is displayed for
            //5 seconds (5000 milliseconds)
            //after this we enter the main part of the game
            //set the opening screen timer
            SetTimer(hWnd, OpeningTimer, 5000, NULL);
            //display the opening Screen
            DisplayScreen( OpeningScreen);

            break //end case WM_CREATE

            //this event is used to get rid of the mouse
            case WM_SETCURSOR:
                SetCursor(NULL); //set it to NULL
                break;//end case WM_SETCURSOR

            case WM_KEYDOWN://if a key is pressed
                //if layer is in city then
                //only the enter key works
                if(inCity)
                {
                    //if the key is Enter then
                    //we exit the city screen
                    if(wParam == VK_RETURN)
                        inCity = false;
                    break;
                }
                //handled if city screen,
                //so treat as normal game now
                switch(wParam)//figure out which key it is
                (
                    case VK_UP://the up arrow
                        //inform everyone else
                        //that we should move up
                        moveUp = true;
                        break;
                    case VK_DOWN://the down arrow
                        moveDown = true;
                        break;
                    case VK_LEFT:
                        moveLeft = true;
                    case VK_RIGHT:
                        moveRight = true;
                        break;
                    case VK_SPACE:
                        //we should fire
                        //note that firing has not been implemented yet
                        fire = true;
                        break;

                    case VK_ESCAPE:
                        //end the main timer
                        KillTimer(hWnd, MainTimer);
                        PostQuitMessage(0);//end the program
                        break;
    }
    break;//exit te case opening timer

    case WM_KEYUP//if a key is released
        switch(wParam)//figure out which key
        {
            //stop moving if key is Released
            case VK_UP:
                moveUp = false;
                break;
            case VK_DOWN:
                moveDown = false;
                break;
            case VK_LEFT:
                moveLeft = false;
                break;
            case VK_RIGHT:
                moveRight = false;
                break;
            case VK_SPACE:
                fire = false;
                break;
        }
        break://end case keyup

    case WM_TIMER://if a timer goes of
            //if player is in city then don't use the timer
        if(inCity)
            break;//exit the WM_TIMER message
            
        switch( wParam )//find out which timer is going off
        {
            //the timer for the opening screen
            case OpeningTimer:
                //starting the main timer
                SetTimer(hWnd, MainTimer, 15, NULL);
                //kill the opening screen timer
                KillTimer(hWnd,OpeningTimer);
                //draw the screen for the first time
                Draw();
                break;//exit case OpeningTimer
            
            //the timer for the main game
            case MainTimer:
                DoMove();//move the ship based on input
                Redraw();//redraw the screen
                //if player has entered a city
                if(isInCity(&ship->GetPosition()))
                {
                    //inform everyone else
                    inCity = true;
                    //display the city screen
                    DisplayScreen(CityScreen);
                    //put the player outside of city radius
                    ship->moveBack();
                }
                break;//end case MainTimer
        }
        break;//endcase WM_TIMER
        
    default://let windows handle all other messages
        return( DefWindowProc(hWnd, messg, wParam, lParam));
}

return(0);//default return value

}       

            




[Edited by - laserbeak43 on August 12, 2007 12:39:24 AM]
__________________________________________________________Maybe one day i can not be affraid of venturing out of the beginners section.
Advertisement
Looks like you're missing one more curly after the last return. The curly right before it is closing the main switch()
ByteMe95::~ByteMe95()My S(h)ite
Your gonna learn that many books have many errors in source code. Mainly because the people who proof read these books before they are published know nothing of programming code. Looks to me you saw just that. You seem to be right by saying there is no need to that return since default should be only possiable outcome if not anything other and that has a return. But many people like to be safe and sure and put checks and test in even where it might never be used just in case. I never realy got the point but they do it as good practice. And that seems to be just that.
Quote:Original post by ByteMe95
Looks like you're missing one more curly after the last return. The curly right before it is closing the main switch()


edited.
sorry about that. i removed it in my editor while i was messing around trying to fix things and forgot to put it back to show you what the problem was.. :/
__________________________________________________________Maybe one day i can not be affraid of venturing out of the beginners section.
Quote:Original post by LittleFreak
Your gonna learn that many books have many errors in source code. Mainly because the people who proof read these books before they are published know nothing of programming code. Looks to me you saw just that. You seem to be right by saying there is no need to that return since default should be only possiable outcome if not anything other and that has a return. But many people like to be safe and sure and put checks and test in even where it might never be used just in case. I never realy got the point but they do it as good practice. And that seems to be just that.


that takes a load off of my shoulders, but for the sake of following this tradition of "good practice"(for the sake of learning to be honest. i really don't think it's necessary but would like to see how it works) where would i stick this return? :P
__________________________________________________________Maybe one day i can not be affraid of venturing out of the beginners section.
Look over it again even tho my last post is right in general I see what is throwing you off in one spot you use a ( instead of a { and it throwing everything out of alignment. Also if you are using Visaul C++ you can use an auto format function that will align things for you that will help you spot these problems...

ByteMe95 is right your last braket is closing the first switch statement so there should be a return and one more... (still might be a good practice return, im tired and didnt realy go through every pit of the code just looked at the format)
Quote:Original post by LittleFreak
Look over it again even tho my last post is right in general I see what is throwing you off in one spot you use a ( instead of a { and it throwing everything out of alignment. Also if you are using Visaul C++ you can use an auto format function that will align things for you that will help you spot these problems...

ByteMe95 is right your last braket is closing the first switch statement so there should be a return and one more... (still might be a good practice return, im tired and didnt realy go through every pit of the code just looked at the format)


ahh there i go blaming the book :) darn i need glasses. i see it. and it really hurt my eyes to find it. sorry for taking up your time. i guess a newb is a newb :)

thanks guys

-edit-
and i do have 2005 standard but i find myself using code::blocks these days cause the bloat of 2005 makes me a bit irritable.
does code::blocks(nightly build) have this auto-formatting feature?
__________________________________________________________Maybe one day i can not be affraid of venturing out of the beginners section.
dont sweat it ive been programming 10 years (note im only 20 started young) and I have recently done the same thing so it happens.

I thought the same thing about 2005... i was a visaul C++ 6 fan forever but it stopped being supported by directx and forced myself over to 05. Its not all bloat at all(gotta customize your menus to get anything done tho). Dont know about code::block auto format. But ive used about 10 text editors in my life and they have all had auto format. Some dont work as well tho. We are all newbs in programming. You can program for years and something new gets thrown at you and you might as well know nothing lol. Good Luck
Quote:Original post by LittleFreak
dont sweat it ive been programming 10 years (note im only 20 started young) and I have recently done the same thing so it happens.

I thought the same thing about 2005... i was a visaul C++ 6 fan forever but it stopped being supported by directx and forced myself over to 05. Its not all bloat at all(gotta customize your menus to get anything done tho). Dont know about code::block auto format. But ive used about 10 text editors in my life and they have all had auto format. Some dont work as well tho. We are all newbs in programming. You can program for years and something new gets thrown at you and you might as well know nothing lol. Good Luck



words of wisdom and encouragement. i'll definitly remember them! :)
__________________________________________________________Maybe one day i can not be affraid of venturing out of the beginners section.

This topic is closed to new replies.

Advertisement