Archived

This topic is now archived and is closed to further replies.

nookiepl

Console problem - please help!

Recommended Posts

Hi , i have huge problem (for me of course) i''m trying to implement console, but i''m doing something wrong! header looks like this: ========================================================================= class CConsole { public: CConsole (); ~CConsole (); //update the console, and check for inputs and everything else void Update (float deltatime); //render the console void Render (); //process an input by the user void ProcessInput (int key); //add a line to the history void AddToHistory (char *line); //add a line to the command history void AddToCommandHistory(char *CommandLine); //execute a given command void ExecuteCommand(char *string); //activate/deactivate console void Activate(); void Deactivate(); bool Active; //is console active bool Visible; //is the console visible //get the state of Active flag //bool IsActive(); //get visibility flag //bool IsVisible(); private: float HistoryYPos; //y position of the history window float CommandXPos; //x position of the command line char *History[15]; //all lines displayed on the history window char *CommandHistory[15]; //last 15 commands executed char *CommandLine; //commnad line text int CommandIndex; //index into the command string int CMHistoryIndex; //index into the list of commands int CMHistoryHeight; //how much of the command list has already been filled void SplitString(char string[64], char left[64], char right[64]); }; extern CConsole Console; #endif ============================================================================= and the source file: ============================================================================= CConsole::CConsole() { //initially the console is deactivated HistoryYPos = 800; CommandXPos = 600; Active = false; Visible = false; //clean the command line CommandLine = ""; CommandIndex = 0; CMHistoryIndex = 0; CMHistoryHeight = 0; //clean the history lines for (int i=0; i<15; i++) History = ""; //clean the command history lines for (i=0; i<15; i++) CommandHistory[i] = ""; } //================================================================== // sConsole:rocessInput //================================================================== void CConsole:rocessInput(int key) { switch (key) { case VK_TAB: break; case VK_LEFT: break; case VK_RIGHT: break; case VK_CAPITAL: break; case VK_SHIFT: break; case VK_RETURN: { if(strlen(CommandLine)) { //exectue the command ExecuteCommand(CommandLine); //add it to the command history AddToCommandHistory(CommandLine); //clear it CommandLine = ""; //set the index to 0 CommandIndex = 0; //point to the last line in the history CMHistoryIndex = 15; if (CMHistoryHeight<15) CMHistoryHeight++; } break; } case VK_BACK: { if (CommandIndex>0) { //delete the last character CommandIndex--; CommandLine[CommandIndex] = ''\0''; } break; } case VK_UP: { if (CMHistoryIndex > 15-CMHistoryHeight) { //move up in the history index CMHistoryIndex--; //get the the command from the history CommandLine = CommandHistory[CMHistoryIndex]; //set the position of the cursor CommandIndex = strlen(CommandHistory[CMHistoryIndex]); } break; } case VK_DOWN: { if (CMHistoryIndex < 14) { //move down in the history index CMHistoryIndex++; //get the the command from the history CommandLine = CommandHistory[CMHistoryIndex]; //set the position of the cursor CommandIndex = strlen(CommandHistory[CMHistoryIndex]) - 1; } break; } default: { if (CommandIndex < 57) //If not outside the command line { //add the character to the command CommandLine[CommandIndex] = (char)key; CommandLine[CommandIndex+1] = ''\0''; CommandIndex++; } break; } } } ================================================================================ Of course it''s not a whole source file - i''ve missed all renderidng stuff etc. When i run my app, i push te TAb key (to run console) and it shows, but when i''m trying to type something the prog crushes (it compiles witout any errors) with information -unhandled exception!!! When i run the debugger, VS6 shows error in line: CommandLine[CommandIndex] = (char)key; In this line i''m trying to catch the key that was pushed - but program crushes ! Whaaat i''m doing wrong ? Wrong initializing of my "CommandLine" varible or what ?? If somebody could help - somehow, i''d be greatful! Maybe YOU have better implementation and you want it to share it ?? )

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You have char *CommandLine;
and then initialize it as CommandLine = ""; that only allocates space for the /0, if you try to access anyting else it will crash, so you either put a limite like CommandLine[128] or alloc space for it at runtime.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I think your CommandLine init is indeed wrong.

You declare it as :
char *CommandLine; //commnad line text

then init it in the constructor as
CommandLine = "";

Which sets the CommandLine ptr to point to an array of char[1], just the null char. Therefore, when you try to write to CommandLine[x] and x is anything larger than 0, you''re writing out of bounds.

You need to init CommandLine to point to c char[57] (I think 57 is your limit) with a
CommandLine = new char[57];

or similar.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Yeah, whoops, you need your limit to be 58, as you max out at 57 chars but append a ''/0'' to the end manually -- you''ll need room for that too

Share this post


Link to post
Share on other sites