Computer Science Canada Program Crashes After Checking for Console Mouse/Keyboard Input |
Author: | HRI [ Wed Apr 27, 2011 9:38 pm ] | ||||||||
Post subject: | Program Crashes After Checking for Console Mouse/Keyboard Input | ||||||||
I use the GNU G++ compiler (default for C::B). First of all, this is a small connect 4 game. What I'm trying to achieve is to let the user either press a key (continues when key hit is 1-7, ignores all others) or when they click inside the board on the column they want. It only crashes if I click in a valid position or press a valid key :/ It worked perfectly fine for just keyboard input with the following code:
Now please be aware that the following is a bit messy. I've just been adding, not cleaning until it works. Also, I'm pretty sure some of this is c, but checking where the mouse was and where it was when the left button was clicked worked like a charm in a test program I made.
Ask any questions you have. Basically the other one that worked drew an ASCII box in the middle of the console window and then checked the mouse like this one does and makes a message box upon clicking saying either that they clicked in the box or didn't click in the box. That one worked perfectly fine. By crash I mean it comes up with the application error window with the option to send a report to Microsoft and returns 0xC0000005. Thanks in advance for any help! |
Author: | DemonWasp [ Thu Apr 28, 2011 10:48 am ] | ||
Post subject: | RE:Program Crashes After Checking for Console Mouse/Keyboard Input | ||
I'm no C++ expert, but shouldn't it be:
if you're trying to assign to the value the pointer points to? |
Author: | crossley7 [ Sat Apr 30, 2011 7:00 pm ] |
Post subject: | RE:Program Crashes After Checking for Console Mouse/Keyboard Input |
I haven't done too many games with c++ as I tought myself it last semester and have mostly done dwite style problems and mathematics related programs (ie factoring), but I did write a chess program, and it may help and clean up you code a lot if you checked out SDL. It requires a download, but makes things easier specifically mouse events and there are easy tutorials on how to use it. It handles all events in 1 variable. I haven't used STD GUI so I can't really help you with the code other than to say trying to make a game that is run in the console is not very pleasing and limits what you can do with the appearance. If you want to make a game look good, you may as well go all out. Sorry if this post is of no use |
Author: | HRI [ Sun May 01, 2011 7:42 pm ] |
Post subject: | RE:Program Crashes After Checking for Console Mouse/Keyboard Input |
Sorry I haven't been able to check this for so long. @DemonWasp: What I'm doing when I'm specifying a char & parameter is telling the compiler to rather than make a copy of what gets sent to the function in the argument, send the actual object itself. In this case it would send the same char variable "column" that it uses in the other function and whatever changes are made to it are carried back. This is the way the whole connect 4 program is set up, passing along objects from function to function since I couldn't get declaring the objects I'd need in a namespace to work. I will try making it pointer-oriented though, and indeed, you are correct. The * dereferences it so that it pertains to the value contained in the address as opposed to the address itself. I think this is where the confusion lied as the & operator can also be used to reference the address of a variable, but in this case it is saying to pass by reference, rather than by value. @crossley7: I haven't actually gotten into SDL yet, although I very much plan to in the near future! The reasoning behind this is that the main focus is to get the actual engine and the AI working before making it look nice. This was supposed to be a pretty simple approach to add a neat option to the game as we haven't done any graphics or anything of the sort yet in class and I've been exploring the Windows API in my spare time. We aren't getting marked on appearance, but we will try to get it looking good after the AI is done. We were initially thinking of linking it with a java application while this runs in the background, doing the processing. It's a neat idea, but learning a C++ graphics library could go a long way. I do agree with your logic though, as I often find myself adding every little thing to make it look/act as perfect as I can muster. |
Author: | DemonWasp [ Sun May 01, 2011 9:28 pm ] | ||||
Post subject: | RE:Program Crashes After Checking for Console Mouse/Keyboard Input | ||||
What I meant is that:
is wrong because it's assigning the column pointer to be '1' (value 49, or 0x00000031). When you next try to dereference the column pointer after that, you're pointing at an invalid location because you've overwritten part of the pointer. This explains the crash, because you're making an invalid memory access (segmentation fault / violation). The correct code, as I suggested in my first response, assigns the value of the char address that column points to. You could also use the (subtly different):
though I wouldn't recommend that as if you ever modify the value that column points to after such an assignment, you will hit a different kind of crash (modifying read-only memory). |
Author: | HRI [ Mon May 02, 2011 7:32 pm ] | ||||||
Post subject: | RE:Program Crashes After Checking for Console Mouse/Keyboard Input | ||||||
The weird thing about that is that I have most of my other functions taking arguments of char &, Board &, Game &, whatever it is I'm passing around, but maybe I never changed any of those without using a set function O_o. I really could've sworn that when we learned about pass-by-value and pass-by-reference the following code worked (quick draft):
And using pointer logic, would this not be equivalent to:
What I'm saying is, when I pass in column, it should be passing that actual variable's value which can be changed as I please as if the function was not a function at all, but pasted into the other function where it is called. If the addOne example works (which I'm sure it did when I tried something of the sort) then this should work the same way. Anyways, I have a bit of time now that I'll try a couple things with it and let you know what happens. |
Author: | HRI [ Mon May 02, 2011 7:58 pm ] | ||
Post subject: | RE:Program Crashes After Checking for Console Mouse/Keyboard Input | ||
Ok, I changed the parameter to char *column, and every time column was assigned a value I put a * in front. Also, I put a & before the argument column for the two procedures. It's still doing the same thing. It didn't compile by simply just putting a * in front because I believe there wasn't an address to begin with. Think of it this way (forgetting about some certain syntax issues):
Next test, though I hate to say it, is not using new procedures for the events... duh duh duh |
Author: | HRI [ Mon May 02, 2011 8:10 pm ] |
Post subject: | RE:Program Crashes After Checking for Console Mouse/Keyboard Input |
Yeah, Windows doesn't like you taking out their procedures. I just succeeded in making the mouse input not work and the key input doing the same thing lol |
Author: | DemonWasp [ Mon May 02, 2011 10:46 pm ] | ||||
Post subject: | RE:Program Crashes After Checking for Console Mouse/Keyboard Input | ||||
I haven't taken the time to decipher everything you said above. What you should be doing is:
Which you call by:
|
Author: | HRI [ Thu May 05, 2011 6:34 pm ] |
Post subject: | RE:Program Crashes After Checking for Console Mouse/Keyboard Input |
I tried changing everything to your first solution of having a pointer parameter and supplying an address argument, but the same thing happened. |
Author: | HRI [ Thu May 05, 2011 6:57 pm ] |
Post subject: | RE:Program Crashes After Checking for Console Mouse/Keyboard Input |
Welllll....... I just took this code, and drew a generic, empty board in a separate program and outputted the return value. Guess what, everything worked fine :s So the issue then is not the way the functions take arguments or change the column, but how it is implemented within the game. I'll get back if I notice anything else. |
Author: | HRI [ Thu May 05, 2011 8:03 pm ] | ||||
Post subject: | RE:Program Crashes After Checking for Console Mouse/Keyboard Input | ||||
BREAKING NEWS: I'm not quite sure how it got there, but
(which is the style I use for keyboard-only inputs) should be
That explains why the mouse wasn't working at all and why the keyboard just ended up making it confused >.> Now for some reason it's not adding any tokens... I hope that's an easy fix though! EDIT: It's still acting as if a token was dropped there, it's just not showing them. I think I'm close! That's a good thing if I plan to add mouse capabilities elsewhere. It could add so much spice to the program, and there's really not enough time to learn a graphics library. Still need to do the AI... EDIT: heh heh heh...must've accidentally changed where it outputs the character to a space when I was transferring that to the test program... Stupid me >.> |