Question about my program

J

JoeC

I am trying to create a simple maze program on a window. I am trying
to create a global var to handle the commands.

I declare:

#include "space.h"
#include "player.h"
#include "Command.h"

static char gCmd = '$'; <--- My global command.

#ifndef DUNFUNCS_H
#define DUNFUNCS_H

int roll(int, int);
LRESULT CALLBACK WinProc(HWND, UINT, WPARAM, LPARAM);
void DrawScreen(HDC);
void ScreenSetup(HWND);
int setup(space (&b)[30][30], player*, const int);
ifstream& MapIn(ifstream&, char&);
void fill(space (&spaces)[30][30], const int);
void loop(HWND, space (&spaces)[30][30], const int);


#endif

In My WinProc:

case WM_PAINT:
HDC hdc;
PAINTSTRUCT ps;
hdc = BeginPaint(hwnd, &ps);
DrawScreen(hdc);
EndPaint(hwnd, &ps);
return 0;

case WM_COMMAND:
if(LOWORD(wparam)== 1){
SendMessage(GetParent((HWND) lparam), WM_DESTROY, 0, 0);
}
if(LOWORD(wparam)==2){
Command = 'n';
gCmd = Command; <--Assigns the Command to global

Command Cmd(gCmd);<--Puts the Global var in the command object

Here in my loop it seems that the var dosn't take the value of 'n'
when I display gCmd it is still '$'

How can I fix this, I think it is a problem of scope and static
variables.
 
V

Victor Bazarov

JoeC said:
I am trying to create a simple maze program on a window. I am trying
to create a global var to handle the commands.

I declare:

#include "space.h"
#include "player.h"
#include "Command.h"

static char gCmd = '$'; <--- My global command.

Replase the 'static' with 'extern'. Drop the initialiser. It will
become a declaration. Place the definition in one of the source files.
(the definition will have the initialiser, no 'static').

Read about 'static' in global scope.

V
 
O

osmium

JoeC said:
I am trying to create a simple maze program on a window. I am trying
to create a global var to handle the commands.

I declare:

#include "space.h"
#include "player.h"
#include "Command.h"

static char gCmd = '$'; <--- My global command.

#ifndef DUNFUNCS_H
#define DUNFUNCS_H

int roll(int, int);
LRESULT CALLBACK WinProc(HWND, UINT, WPARAM, LPARAM);
void DrawScreen(HDC);
void ScreenSetup(HWND);
int setup(space (&b)[30][30], player*, const int);
ifstream& MapIn(ifstream&, char&);
void fill(space (&spaces)[30][30], const int);
void loop(HWND, space (&spaces)[30][30], const int);


#endif

In My WinProc:

case WM_PAINT:
HDC hdc;
PAINTSTRUCT ps;
hdc = BeginPaint(hwnd, &ps);
DrawScreen(hdc);
EndPaint(hwnd, &ps);
return 0;

case WM_COMMAND:
if(LOWORD(wparam)== 1){
SendMessage(GetParent((HWND) lparam), WM_DESTROY, 0, 0);
}
if(LOWORD(wparam)==2){
Command = 'n';

Command seems to be a char variable, but I can't see the declaration.
gCmd = Command; <--Assigns the Command to global

Command Cmd(gCmd);<--Puts the Global var in the command object

The syntax and your narrative suggest that *this* Command is a class we
can't see. I have the impression that you want to declare a variable named
Cmd of type Command. Is this so? Perhaps you stripped too much out of your
upload.
I see:
<thing of unknown kind> <thing of unknown kind> ( character variable )

where "kind" includes both types and variables
 
A

Alf P. Steinbach

* JoeC:
I am trying to create a simple maze program on a window. I am trying
to create a global var to handle the commands.

I declare:

#include "space.h"
#include "player.h"
#include "Command.h"

Presumably the above #include defines some command-related things.

You should probably have included or described that.

We're not telepathic.

static char gCmd = '$'; <--- My global command.

In general it's not a good idea to use globals.

On the other hand, for the kind of program you're doing (beginner's
Windows API) it does make sense and is The Right Thing To Do.

On the third and most important gripping hand, you're making the wrong
thing into a global. A command is not part of the window state, it's an
action. Before using globals in a good way for novice windowing
program, you must master how to /not/ use globals.

#ifndef DUNFUNCS_H
#define DUNFUNCS_H

If this is a header file you should not define variables in it anyway.

int roll(int, int);
LRESULT CALLBACK WinProc(HWND, UINT, WPARAM, LPARAM);
void DrawScreen(HDC);
void ScreenSetup(HWND);
int setup(space (&b)[30][30], player*, const int);
ifstream& MapIn(ifstream&, char&);
void fill(space (&spaces)[30][30], const int);
void loop(HWND, space (&spaces)[30][30], const int);

You'll avoid a lot of problems if you simply refrain from
forward-declaring things.

Anyway, don't put internal implementation details in a header file.

That will also help you avoid a lot of problems.
#endif



In My WinProc:

case WM_PAINT:
HDC hdc;
PAINTSTRUCT ps;
hdc = BeginPaint(hwnd, &ps);
DrawScreen(hdc);
EndPaint(hwnd, &ps);
return 0;

It's a good idea to put such code in a separate function.

case WM_COMMAND:
if(LOWORD(wparam)== 1){
SendMessage(GetParent((HWND) lparam), WM_DESTROY, 0, 0);
}

Here you have at least one bug, and possibly two. Instead of analyzing
low-level API data at the bits & bytes level, use the relevant API
facilities (in this case, the macros from <windowsx.h>). And know your
APIs! I won't discuss the details here since this group is about C++,
but because you don't know your APIs, you're using the
(Windows-specific) message WM_DESTROY incorrectly. Put this question up
for discussion in e.g. [comp.os.ms-windows.programmer.win32].

if(LOWORD(wparam)==2){

Again, don't analyze data at the bits & bytes level.

Command = 'n';

Is Command a type or a variable, and where the heck did it come from?

gCmd = Command; <--Assigns the Command to global

Command Cmd(gCmd);<--Puts the Global var in the command object

D O N ' T D O T H A T !

The main problem is you're trying to tackle Windows API programming
while you're still struggling with the most fundamental programming
concepts (like, what's a variable and what's a type and so on, not to
mention the divide between states and actions).

Best advice: do something _very much_ simpler.
 
M

Matt

JoeC said:
I am trying to create a simple maze program on a window. I am trying
to create a global var to handle the commands.

I declare:

#include "space.h"
#include "player.h"
#include "Command.h"

static char gCmd = '$'; <--- My global command.

#ifndef DUNFUNCS_H
#define DUNFUNCS_H

int roll(int, int);
LRESULT CALLBACK WinProc(HWND, UINT, WPARAM, LPARAM);
void DrawScreen(HDC);
void ScreenSetup(HWND);
int setup(space (&b)[30][30], player*, const int);
ifstream& MapIn(ifstream&, char&);
void fill(space (&spaces)[30][30], const int);
void loop(HWND, space (&spaces)[30][30], const int);


#endif

You shouldn't define static variables in a header file. A static
variable at file scope creates the storage for that variable and
it also confines the visibility of that variable to the file that
it's defined in. So if, say, you put that "static char gCmd;"
in "command.h" and include that in "main.c" "command.c" and
"maze.c", then you'll wind up with three variables called gCmd,
one in main.c, one in command.c and one in maze.c. and none of
them can be accessed or referenced outside of those modules.

What you probably want to do is define one non-static gCmd
variable to allocate the storage for it, "char gCmd;" and then
use "extern char gCmd;" in the files that use that variable.
"Extern" doesn't create storage, it just tells the compiler
about gCmd so the compiler can do type checking and so forth.
You can put extern variable declarations in a header if you
want.

Incidentally, I agree with Alf about avoiding globals, but
I think you should learn the syntax first. You can worry
about designing things once you learn the language.

Matt
 
J

JoeC

Thanks for the advice. There are pleanty of resources on how to C++
code but I can't find much on how to design a program. I havn't had
any formal progamming training in highschool about twenty years ago. I
want to learn programming and the only way to do it is just do it.

I do try to avoid golbal variables because I know they are bad but I am
not sure how to have the WinProc loop control the program. I have to
get the actions chosen in that function to the main program loop.
Finally the > if(LOWORD(wparam)==2){ stuff is straight out
of a book on how to do the COMMAD:...
 
J

JoeC

Thanks for the advice. There are pleanty of resources on how to C++
code but I can't find much on how to design a program. I havn't had
any formal progamming training since I was in highschool about twenty
years ago. I want to learn programming and the only way to do it is
just do it.

I do try to avoid golbal variables because I know they are bad but I am
not sure how to have the WinProc loop control the program. I have to
get the actions chosen in that function to the main program loop.
Finally the > if(LOWORD(wparam)==2){ stuff is straight out
of a book on how to do the COMMAD:...
 
J

JoeC

Oh, thank you for that explination, it all makes sence now. Thanks
much I will give it a try.
 
J

JoeC

Thanks so much you helped me greatly with that aspect of my design.
There are a great deal of books about code but not many that I have
seen about design. I am writing a rather complex program. I am trying
to make maze program in a window. The only way to get experience is to
just do try and ask for help. I still have pleanty of other bugs, I
will work on them but if I am stuck I will ask.
 
A

Alf P. Steinbach

* JoeC:
Thanks for the advice. There are pleanty of resources on how to C++
code but I can't find much on how to design a program. I havn't had
any formal progamming training [since] highschool about twenty years ago. I
want to learn programming and the only way to do it is just do it.

Yes, that's a good way -- and perhaps the only way that works.

I do try to avoid golbal variables because I know they are bad but I am
not sure how to have the WinProc loop control the program.

I just realized that you've /crossposted/ the thread to [comp.lang.c++]
and [comp.os.ms-windows.programmer.win32].

Please don't do that, since Windows-specific stuff is completely
off-topic in [comp.lang.c++].

I'll respond to the last Windows-specific paragraph, which I've snipped
here, in [comp.os.ms-windows.programmer.win32] only.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top