473,546 Members | 2,244 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about my program

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(HWN D);
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(wpara m)== 1){
SendMessage(Get Parent((HWND) lparam), WM_DESTROY, 0, 0);
}
if(LOWORD(wpara m)==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.

Feb 15 '06 #1
9 1531
JoeC wrote:
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
--
Please remove capital As from my address when replying by mail
Feb 15 '06 #2
"JoeC" wrote:
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(HWN D);
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(wpara m)== 1){
SendMessage(Get Parent((HWND) lparam), WM_DESTROY, 0, 0);
}
if(LOWORD(wpara m)==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

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.

Feb 15 '06 #3
* 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(HWN D);
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(wpara m)== 1){
SendMessage(Get Parent((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.program mer.win32].

if(LOWORD(wpara m)==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.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 15 '06 #4
JoeC wrote:
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(HWN D);
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
Feb 15 '06 #5
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(wpara m)==2){ stuff is straight out
of a book on how to do the COMMAD:...

Feb 16 '06 #6
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(wpara m)==2){ stuff is straight out
of a book on how to do the COMMAD:...

Feb 16 '06 #7
Oh, thank you for that explination, it all makes sence now. Thanks
much I will give it a try.

Feb 16 '06 #8
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.

Feb 16 '06 #9
* 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.program mer.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.program mer.win32] only.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 16 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2964
by: Hal Vaughan | last post by:
I think a lot of this is definately a question of personal programming style, but I'm new to Java and would like to hear a few opinions. I'm writing a control panel for an application that runs separately. The control panel is basically (almost) fully self contained. It consists of a tabbed pane with 5 different tabs. Each tab has a...
3
1389
by: Tony Johansson | last post by:
Hello experts!! I reading in a book about C++ and there is something that I'm not sure about. I don't belive that the book is wrong but I will just ask you out there what you think. The book says the following "Note that you cannot assume that all resources are automatically released when the entire program tetminates. While this is true...
6
1353
by: jalkadir | last post by:
As I wrote the subject of this message, I got a cold feeling about this question, I have realized that this is not only a programmer question, but it is also a question about one the most detestable operation systems in the market, but I will post it anyway, hoping that I will not get fertilized by the any one here. So, my question about a...
20
6531
by: __PPS__ | last post by:
Hello everybody in a quiz I had a question about dangling pointer: "What a dangling pointer is and the danger of using it" My answer was: "dangling pointer is a pointer that points to some area of ram that's not reserved by the program. Accessing memory through such pointer is likely to result in core dump (memory access violation)"
11
4232
by: Mark Yudkin | last post by:
The documentation is unclear (at least to me) on the permissibility of accessing DB2 (8.1.5) concurrently on and from Windows 2000 / XP / 2003, with separate transactions scope, from separate threads of a multithreaded program using embedded SQL. Since the threads do not need to share transaction scopes, the sqleAttachToCtx family of APIs do...
4
4392
by: I_AM_DON_AND_YOU? | last post by:
There is one more problem I am facing but didn't get the solution. In my Setup Program I am not been able to create 2 things (when the program is intalled on the client machine ) : (1) create shortcut to my program/utility (2) Entry in Windows' Start --> Program Menu. Actually in my VB.Net solution I have two projects (1) MYPROGRAM (2)...
29
3538
by: MP | last post by:
Greets, context: vb6/ado/.mdb/jet 4.0 (no access)/sql beginning learner, first database, planning stages (I think the underlying question here is whether to normalize or not to normalize this one data field - but i'm not sure) :-) Background info:
6
5387
by: Andy Leese | last post by:
Beginner Question: ASCII Symbols I am using Borland C++ and programming under DOS. I wish to display the symbols of the early ASCII character set... For example: cout << char(7); Obviously this is assigned to the BELL signal and therefore sounds the beep
0
1349
by: DBC User | last post by:
Hi all, I have a quick question on vista elevation. I have 2 application, one main GUI which user interacts with and another one is a tray program. User have options to install products from the main GUI program, which in turn writes the requests and pass it to the tray program. The tray program will download and then runs the setup.exe in...
12
3000
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope. The global variables and global functions are hidden to prevent from accessing by the programmers. All global functions share global variables....
0
7698
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7794
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6030
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5361
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3492
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1922
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1046
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.