Wrong with this code?

Page 1 of 2 12 LastLast
  1. #1
    Cloud's Hair Spray Reputation: 10

    Join Date
    Aug 2006
    Posts
    60
    Rep Power
    8

    Default Wrong with this code?

    Ok I'm using Microsoft Visual c++ express edition for this programming book by Jonathan S. Harbour. Now this code below I type dit all in exactly now here is the error I get and I don't know how to fix it:

    ------ Build started: Project: Project1, Configuration: Debug Win32 ------
    Compiling...
    maintest.c
    .\maintest.c(6) : fatal error C1083: Cannot open include file: 'windows.h': No such file or directory
    Build log was saved at "file://e:\Documents and Settings\Sean\My Documents\Visual Studio 2005\Projects\Project1\Project1\Debug\BuildLog.htm "
    Project1 - 1 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========













    // Beginning Game Programming
    // Chapter 3
    // WindowTest program

    //header files to include
    #include <windows.h>
    #include <stdlib.h>
    #include <time.h>

    //application title
    #define APPTITLE "Hello World"

    //function prototypes (forward declarations)
    BOOL InitInstance(HINSTANCE,int);
    ATOM MyRegisterClass(HINSTANCE);
    LRESULT CALLBACK WinProc(HWND,UINT,WPARAM,LPARAM);


    //the window event callback function
    LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    PAINTSTRUCT ps;
    HDC hdc;
    char *szHello = "Hello World!";
    RECT rt;
    int x, y, n;
    COLORREF c;
    switch (message)
    {
    case WM_PAINT:
    //get the dimensions of the window
    GetClientRect(hWnd, &rt);

    //start drawing on device context
    hdc = BeginPaint(hWnd, &ps);

    //draw some text
    DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER;

    //draw 1000 random pixels
    for (n=0; n<3000; n++)
    {
    x = rand() % (rt.right - rt.left);
    y = rand() % (rt.bottom - rt.top);
    c = RGB(rand()%256, rand()%256, rand()%256);
    SetPixel(hdc, x, y, c);
    }

    //stop drawing
    EndPaint(hWnd, &ps);
    break;

    case WM_DESTROY:
    PostQuitMessage(0);
    break;
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
    }

    //helper function to set up the window properties
    ATOM MyRegisterClass(HINSTANCE hInstance)
    {
    //create the window class structure
    WNDCLASSEX wc;
    wc.cbSize = sizeof(WNDCLASSEX);

    //fill the struct with info
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = (WNDPROC)WinProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = NULL;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackround = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = APPTITLE;
    wc.hIconSm = NULL;

    //set up the window with the class info
    return RegisterClassEx(&wc);
    }

    //helper function to create the window and refresh it
    BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
    {
    HWND hWnd;

    //create a new window
    hWnd = CreateWindow(
    APPTITLE, //window class
    APPTITLE, //title bar
    WS_OVERLAPPEDWINDOW, //window style
    CW_USEDEFAULT, //x position of window
    CW_USEDEFAULT, //y position of window
    500, //width of the window
    400, //height of the window
    NULL, //parent window
    NULL, //menu
    hInstance, //application instance
    NULL); //window parameters

    //was there an error creating the window?
    if (!hWnd)
    return FALSE;

    //display the window
    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    return TRUE;
    }

    //entry point for a Windows program
    int WINAPI WinMain(HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow)
    {
    // declare variables
    MSG msg;

    // register the class
    MyRegisterClass(hInstance);

    // initialize application
    if (!InitInstance (hInstance, nCmdShow))
    return FALSE;

    //set random number seed
    srand(time(NULL));

    //main message loop
    while (GetMessage(&msg, NULL, 0, 0))
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    return msg.wParam;
    }


    Thanks in advance .

  2. #2
    Keyboard Warrior Reputation: 193
    Raiyne's Avatar
    Join Date
    May 2006
    Location
    The Middle of the Stairs
    Posts
    13,571
    Rep Power
    18

    Default

    Dont they show you the line on which the error is on? o_O Free C++ Compilers ftw.

    Everytime I post, I think Murph is about to BROFIST me. -- Loric
    Last.fm
    Hey Raiyne

  3. #3
    Cloud's Hair Spray Reputation: 10

    Join Date
    Aug 2006
    Posts
    60
    Rep Power
    8

    Default

    Will yes here is the error code like i put on top:

    ------ Build started: Project: Project1, Configuration: Debug Win32 ------
    Compiling...
    maintest.c
    .\maintest.c(6) : fatal error C1083: Cannot open include file: 'windows.h': No such file or directory
    Build log was saved at "file://e:\Documents and Settings\Sean\My Documents\Visual Studio 2005\Projects\Project1\Project1\Debug\BuildLog.htm "
    Project1 - 1 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  4. #4
    Crumbly, yet Good Reputation: 75
    Eriond's Avatar
    Join Date
    May 2006
    Location
    Montreal
    Posts
    1,798
    Rep Power
    0

    Default

    There seems to be a problem with your include directory.. even if you don't have the platform SDK installed, it shouldn't be a problem, windows is a standard header file.

    Try downloading the Windows SDK:
    http://www.microsoft.com/downloads/d...displaylang=en

    It has all the header files and libraries you'll ever need. Just make sure you add that to your include directory (somewhere in options).
    THIS MA SIGNATURE

  5. #5
    Cloud's Hair Spray Reputation: 10

    Join Date
    Aug 2006
    Posts
    60
    Rep Power
    8

    Default

    Ok thanks.

  6. #6
    Cloud's Hair Spray Reputation: 10

    Join Date
    Aug 2006
    Posts
    60
    Rep Power
    8

    Default

    Sorry for double post but Nothing happened Still an error. Any other solutions?

  7. #7
    Orc's Breathmint Reputation: 10
    pb_destiny's Avatar
    Join Date
    May 2006
    Location
    Ottawa, Ontario, Canada
    Posts
    102
    Rep Power
    9

    Default

    I've ran into a similar problem in the past, and it had something to do with the registry of the compiler interacting with the directories. Basically, I ran into the same problem, except I was trying to include an sdl header. What I did was simply save all my source files (.cpp, .exe, and .o files - if you find including the machine code version of your programming is important) in separate folder not found in the directory. I than made sure I had the backups/install files for all the included files and extra libraries I added, saved somewhere else in a different folder not using the same directory.

    Once I knew I had everything else I didn’t want to lose, I uninstalled the compiler and deleted all the directories left over. I than reinstalled the compiler and added all the extra libraries back into the include directory. Now this may seem a little extreme, so I would use this as a last resort. If you can’t find any other solutions to the problem than this should work, remember to move all your saved source code files to your original saving place.

    Hope this helps,

    -PB
    Last edited by pb_destiny; 08-19-2006 at 06:10 PM.
    Been A Member Since May of 2005

  8. #8
    Cloud's Hair Spray Reputation: 10

    Join Date
    Aug 2006
    Posts
    60
    Rep Power
    8

    Default

    So should i Re-Download it or Just reinstall it after i'v deleted everything?

  9. #9
    Orc's Breathmint Reputation: 10
    pb_destiny's Avatar
    Join Date
    May 2006
    Location
    Ottawa, Ontario, Canada
    Posts
    102
    Rep Power
    9

    Default

    ***Important Point***
    I just like to add that you should make sure to take Eriond's advice first and install the latest Windows SDK platform as you will run into problems with including the standard header files when using visual c++ express.

    I just use Dev-C++ for all my source code compiling/linking.

    Well I think reinstalling it should be fine, but if you run into the same problem you may have to redownload it, although I find that highly unlikely. If reinstalling everything doesnt do it, than there must be a problem somewere else on your system thats prevent you from including this single header file for some very strange, very random reason.
    Last edited by pb_destiny; 08-19-2006 at 06:09 PM.
    Been A Member Since May of 2005

  10. #10
    Retired Staff Reputation: 10
    Chrono's Avatar
    Join Date
    May 2006
    Posts
    284
    Rep Power
    0

    Default

    Heres a guide on how to install the Platform SDK, also while your at it you should also downlaod the DirectX SDK and install that.
    http://msdn.microsoft.com/vstudio/ex...alc/usingpsdk/

    Goodluck with your book.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •