
-----------------------------------
HRI
Wed Apr 27, 2011 9:38 pm

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:


char column = NULL; // used for the character the user inputs
cout = 49 && int(column)  23 && xCoord < 52 && yCoord < 153) // if mouse is in first column
            {
                column = '1'; // set value of column passed as argument to '1' (remember the user was pressing '1', not '0')
                return; // exit procedure
            } // end if

            if (xCoord > 52 && xCoord < 83 && yCoord < 153) // repeat the rest for other columns (and yes, I'm going to clean it up after)
            {
                column = '2';
                return;
            }

            if (xCoord > 83 && xCoord < 116 && yCoord < 153)
            {
                column = '3';
                return;
            }

            if (xCoord > 116 && xCoord < 148 && yCoord < 153)
            {
                column = '4';
                return;
            }

            if (xCoord > 148 && xCoord < 179 && yCoord < 153)
            {
                column = '5';
                return;
            }

            if (xCoord > 179 && xCoord < 209 && yCoord < 153)
            {
                column = '6';
                return;
            }

            if (xCoord > 209 && xCoord < 240 && yCoord < 153)
            {
                column = '7';
                return;
            }
            break;
        }
    }

    column = 1; // set column's int value to 1 if none of the above
}



// KeyProc (takes KEY_EVENT_RECORD and char &)

{
    if (key.bKeyDown) // if key was pressed
    {
        if (key.wVirtualKeyCode == 0x31) // if key code is code for '1'
        {
            column = '1'; // set column to '1'
            return; // exit function
        } // end if

        if (key.wVirtualKeyCode == 0x32) // repeat for other columns (I'm obviously planning on cleaning this one up after >.>)
        {
            column = '2';
            return;
        }

        if (key.wVirtualKeyCode == 0x33)
        {
            column = '3';
            return;
        }

        if (key.wVirtualKeyCode == 0x34)
        {
            column = '4';
            return;
        }

        if (key.wVirtualKeyCode == 0x35)
        {
            column = '5';
            return;
        }

        if (key.wVirtualKeyCode == 0x36)
        {
            column = '6';
            return;
        }

        if (key.wVirtualKeyCode == 0x37)
        {
            column = '7';
            return;
        }
    }

    else
    {
        column = 1; // set column to char (1) if none of the above
    }
}


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!

-----------------------------------
DemonWasp
Thu Apr 28, 2011 10:48 am

RE:Program Crashes After Checking for Console Mouse/Keyboard Input
-----------------------------------
I'm no C++ expert, but shouldn't it be:

[code]*column = '1';[/code]

if you're trying to assign to the value the pointer points to?

-----------------------------------
crossley7
Sat Apr 30, 2011 7:00 pm

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

-----------------------------------
HRI
Sun May 01, 2011 7:42 pm

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.

-----------------------------------
DemonWasp
Sun May 01, 2011 9:28 pm

RE:Program Crashes After Checking for Console Mouse/Keyboard Input
-----------------------------------
What I meant is that:

[code]column = '1';[/code]

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):

[code]column = &'1';[/code]

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).

-----------------------------------
HRI
Mon May 02, 2011 7:32 pm

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):

void addOne (int &);

int main()
{
  int x = 0;
  cout 