Sign in to follow this  
Dark_Glitch

Console Problem

Recommended Posts

I'll just attach the code to make this easier. I'm trying to make a very simple DOS menu where the user can click between the denoted field and activate the next function. As you can tell if you attempted to compile this, if you even move the mouse the console window just executes mainMenu() over and over again. Thanks ahead. EDIT: Before anyone screams "USE CODE TAGS" I did. It always does this when I post code. I don't know why.
[SOURCE]
#include <windows.h>
#include <iostream>
#include <cstdlib>
#include <fstream>

#define SCREEN_WIDTH 79
#define SCREEN_HEIGHT 24

using namespace std;

void mainMenu();
void createNewCharacter();
void deleteCharacter();
void cleanup();

HANDLE input_handle;
HANDLE output_handle;

CONSOLE_SCREEN_BUFFER_INFO screen_info;

int main()
{
	COORD pos;

	DWORD info;

	input_handle = GetStdHandle(STD_INPUT_HANDLE);
	output_handle = GetStdHandle(STD_OUTPUT_HANDLE);

	GetConsoleScreenBufferInfo(output_handle, &screen_info);

	SetConsoleMode(input_handle, ENABLE_PROCESSED_INPUT | ENABLE_MOUSE_INPUT);

	while(1)
	{
	    mainMenu();
	}
}

void mainMenu()
{
    INPUT_RECORD selection;
    DWORD Events=0;

	ReadConsoleInput(input_handle, &selection, 1, &Events);

    cout << "*******************************************************\n"
         << "*          The NEW MMO GAME TITLE IN PROGRESS         *\n"
         << "*                  1) New Character                   *\n"
         << "*                  2) Delete Character                *\n"
         << "*                  3) Quit                            *\n"
         << "*******************************************************\n";

    if(selection.EventType == MOUSE_EVENT)
    {
        if(selection.Event.MouseEvent.dwMousePosition.X >= 19 &&
           selection.Event.MouseEvent.dwMousePosition.X <= 38 &&
           selection.Event.MouseEvent.dwMousePosition.Y == 2  &&
           selection.Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)
        {
            system("cls");
            createNewCharacter();
        }


        else if(selection.Event.MouseEvent.dwMousePosition.X >= 19 &&
                selection.Event.MouseEvent.dwMousePosition.X <= 38 &&
                selection.Event.MouseEvent.dwMousePosition.Y == 3  &&
                selection.Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)
        {
            system("cls");
            deleteCharacter();
        }


        else if(selection.Event.MouseEvent.dwMousePosition.X >= 19 &&
                selection.Event.MouseEvent.dwMousePosition.X <= 38 &&
                selection.Event.MouseEvent.dwMousePosition.Y == 4  &&
                selection.Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)
        {
            system("cls");
            system("PAUSE");
            system("EXIT_SUCESS");
        }

        else
        {
            // reload
        }
    }
}   // end of mainMenu()

void createNewCharacter()
{
    cout << "It works!";
    system("PAUSE");
    system("EXIT_SUCESS");
}

void deleteCharacter()
{
    cout << "It works!";
    system("PAUSE");
    system("EXIT_SUCESS");
}[/SOURCE]
[/source] [Edited by - Dark_Glitch on December 21, 2008 1:02:17 AM]

Share this post


Link to post
Share on other sites
It seems to be mostly correct (if it is a console program then the console window will be created for you and you correctly obtain the handles).

What is wrong is printing the menu over and over in an infinite loop (each time there's an input event). You might move that out of the loop and only print that when you want to "return" to the menu from some other action.

And another thing wrong is this:


system("EXIT_SUCESS");


This is an unknown command. If you want to exit the program with return value EXIT_SUCCESS then that is


exit(EXIT_SUCCESS);


However, you might want to reconsider the general structure of the program, so that you would be able to exit normally by returning to main.

I guess the input loop should be within MainMenu (clear screen, print choices, then loop until you get good input, then return a code to the main). Upon receiving a value from MainMenu, the main function then (e.g in a switch) determines which function to call, or whether to break the main program loop. Upon returning from the functions that implement the menu choices, you loop back in main (unless the selection was to quit) and call MainMenu again (etc).

BTW, use SOURCE tags for longer code.

Share this post


Link to post
Share on other sites
Quote:
Original post by Dark_Glitch
EDIT: Before anyone screams "USE CODE TAGS" I did. It always does this when I post code. I don't know why.

GameDev's tags are lowercase. It's [source], not [SOURCE], and [code], not [CODE].

Don't complain; XHTML tags are all lowercase, too.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this