Archived

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

BrianDra2

Clearing Strings

Recommended Posts

BrianDra2    122
How do you deinitialize a string? I made a Windows 95 program that has the user enter in text from a keyboard and stores it in a array of chars. Once the user presses enter, I want the string completely cleared out, as if it has never been used. Thanks, Saul-Paul

Share this post


Link to post
Share on other sites
krez    443
it depends...
if you use a char array you can go through it setting each char to NULL:

char MyString[255];
for (int t = 0; t < 255; ++t)
MyString[t] = 0;

if you are using a pointer, you can just delete[] it (i don''t know if this will actually "clear" the memory, but your program will consider it "gone" so you can use it anymore). or, you can do the same as above if you want to make sure:

char *MyString;
for (int t = 0; t < LEN_STRING; ++t)
MyString[t] = 0;

of course you''ll need to know how long the char arrya has been allocated for then (LEN_STRING), as "strlen()" will only tell you how many characters until the terminating "NULL" character...
i''m guessing that you want this data completely GONE, so i''d recommend changing all the positions in the array to 0, then delete[]-ing it...
HTH

--- krez (krezisback@aol.com)

Share this post


Link to post
Share on other sites
BrianDra2    122
I tried both of your suggestions, but it still doesn''t work:


char text[1000]; //1000 chars
int charindex=0;
char* response; //computers response

.
.
.

case WM_CHAR:
{

text[charindex] = WPARAM(wParam);
charindex = charindex + 1;

break;
}
case WM_KEYDOWN:
{
// Handle any non-accelerated key commands
switch (wParam)
{
case VK_RETURN:
{

DoCommands();
break;
}

.
.
.

void DoCommands()
{

//Depending on what the user enters, I want it to respond
if(!strcmp(text, "Hello"))
{
response = "Hello Saul";
}

else if(!strcmp(text, "How are you doing?"))
{
response = "I feel like throwing up.";
}
else
response = "";

//Here I want to clear the char out
memset(text, 0, 1000 );
charindex=0;
}

When I run it I type in "Hello" and it responds correctly. Then
when I try typing in "Hello" again, it doesn''t respond. How could I get this to work?

Share this post


Link to post
Share on other sites
Oluseyi    2103
quote:
Original post by BrianDra2
...
response = "Hello Saul";
...
response = "I feel like throwing up.";
...
response = "";

1.) I sincerely hope you initialized the memory response points to.
2.) You can''t set the value of a C-style string using =; you either use strncpy() (look it up), or you might want to look into std::string.

quote:
//Here I want to clear the char out
memset(text, 0, 1000 );
charindex=0;

text isn''t a dynamically allocated array, so you need to return a pointer to it.
memset(&text, 0, 1000); 

You might want to look for a good C/C++ reference, or peruse MSDN.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
Oluseyi    2103
quote:
Original post by Kylotan
He doesn''t need to initialise memory for ''response'' in this case, as it''s just moving a pointer between one constant string and another, all of which are automatically initialised at compilation time.

True, but it''s bad practice (especially for beginners; they get to thinking this is how you manipulate strings in C).

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites