Clearing Strings

Started by
6 comments, last by BrianDra2 22 years, 3 months ago
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
Advertisement
Look up memset. You want to set the array contents all to zero.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]
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)
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
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?
Set the first character of the array to ''\0'' (null character)
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!
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.


[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]
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!

This topic is closed to new replies.

Advertisement