Jump to content
  • Advertisement
Sign in to follow this  
pbbm2000

Help creating a Consol.

This topic is 5456 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to create a consol and Im getting some really wierd things are happening with a char array. Heres the code. class cData { public: char *dat; int length; cData() { dat = new char[50]; length = -1; } }; class GnarlyConsol { public: int Init(XtremeBase *Xb) { X = *Xb; font.Init("vSmall.bmp", 8, 16, 10, 10, &X, D3DCOLOR_ARGB(0,255,0,255)); CopyBitmap("consol.bmp", &consol, X.d3dDevice); state = 0; max = 50; text = new cData[17]; for(int a = 0; a < 17; a++) { for(int b = 0; b < max; b++) { text[a].dat = NULL; } } return OK; } int Main(XtremeBase *Xb) { X = *Xb; if(state == 0) { if(X.KeyDown(DIK_F10)) { state = 1; } } else { CopyBitmapHardcore(&X, consol, 0, 418, 1024, 350, FALSE, NULL); font.PrintString("12345678901234567890123456789012345678901234567890", 0, 418, 8, &X, TRUE); font.PrintInt(DIK_X,0,438,&X,TRUE); font.PrintChar('>', 10, 748, &X, TRUE); EnterData(); PrintData(); } return OK; } private: int PrintData() { font.PrintString(text[0].dat, 20, 748,8, &X, TRUE); font.PrintString(text[1].dat, 20, 728,8, &X, TRUE); return OK; } int Enter() { for(int a = 0; a < 16; a++) { for(int b = 0; b < max; b++) { text[a+1].dat = text[a].dat; } } for(a = 0; a < max; a++) { text[0].dat[a] = NULL; } text[0].length = -1; return OK; } }; The Enter() function is called whenever the Enter Key is pressed. When the user types the keys they hit are stored in text[0].dat The Enter function is soposed to move the data from text[0].dat to text[1].dat and from text[1].dat to text[2].dat etc... The problem is that when I hit enter it moves the data correctly but then when I set text[0].dat[a] = NULL it also sets text[1].dat = NULL. Also if I set text[0].dat[a] = 'j' (or any char) then text[1].dat[a] = 'j'; Whenever I make a change to text[0] it will make the same change to text[1]. I've never seen anything like this. Any help would be great. Thanks

Share this post


Link to post
Share on other sites
Advertisement
Put away your cData class and replace it with a solid std::string.
Also get rid of static allocation sizes.

Use something like std::list<std::string> or std::vector<std::string> to store your console input.

Other than that, why the heck do you set a char to NULL?
text[n] is a cData instance. text[n].dat is a char pointer. text[n].dat is a single character.
Consider refactoring. cData (std::string does a better job[wink]) needs to have a reset() method and a proper destructor:


class cData // <-- misleading name, consider renaming to cConEntry or something
{
private:
char *dat;
int length;
public:

// avoid literals
const int MaxLength = 50;

cData() : dat(new char[MaxLength+1]), length(0) // <-- -1 is semantically incorrect
{
dat[0] = 0;
}

// always returns a valid null-terminated string
const char *Data() const { return dat; }

// length property is read-only
const int Length() const { return length; }

// add a letter
void Push(char letter)
{
if (length < MaxLength)
{
dat[length++] = letter;
dat[length] = 0;
}
}

// add many letters or null-terminated string
void Push(const char *string, const int count = 0)
{
const int len = count > 0 ? count : strlen(string);
int copy = min(MaxLength-length, len);
if (copy > 0)
{
strncpy(dat + length, string, copy);
length += copy;
dat[length] = 0;
}
}

// clear content
void Clear()
{
length = 0;
dat[0] = 0;
}

// Free allocated data.
~cData()
{
delete [] dat;
}

// use copy operator - removes lots of your code,
// you can just use text[n+1] = text[n]; with this.
const cData & operator = (const cData &rhs)
{
length = rhs.length;
if (length)
strncpy(dat, rhs.dat, length);
dat[length] = 0;

return *this;
}
};
[/quote]

Also consider better naming for methods and variables. An Init() function smells like a class constructor for example.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!