funny error with putting values into char

Hello all,

I have the following code:

char* dest3 = new char(textBox1->Text->Length);
for(int f=0; f < textBox1->Text->Length; f++){
     dest3[f] = (char)textBox1->Text->Chars[f];
     MessageBox(NULL, dest3, "inside", 0x00010);
}

I need to put the value of a text box into a char* which I then use later on. I had this code workgin this afternoon but have just tried it again and it does not work. it works when the textbox->text->length is short. I have debugged it with the message box and it seems to error when f gets to around 17. It says that "Unhandled exception....[...]...Object reference not set to an instance of an object"
Im sure this was working before for paths of any size not just those below 16/17. I had this error yesterday but it seemed to disappear this morning.
Any ideas?

Cheers
Richard
richjo100Asked:
Who is Participating?
 
nonubikCommented:
char* dest3 = new char[textBox1->Text->Length + 1]; //an extra char for the terminating NULL;
ZeroMemory(dest3, textBox1->Text->Length + 1);        //fill with null chars

for(int f=0; f < textBox1->Text->Length; f++){
     dest3[f] = (char)textBox1->Text->Chars[f];
     MessageBox(NULL, dest3, "inside", 0x00010);
}

//and (as previously said) you'll have to "delete[] dest3;"
0
 
Jaime OlivaresSoftware ArchitectCommented:
I guess it is something like this:

char* dest3 = new char[textBox1->Text->Length+1];
for(int f=0; f < textBox1->Text->Length; f++){
     dest3[f] = (char)textBox1->Text->Chars[f];
}
dest3[f] = 0;  // Close string with a null character
MessageBox(NULL, dest3, "inside", 0x00010);   // Put messagebox outside the for loop

// Remember: somewhere you have to delete dest3 after using
0
 
richjo100Author Commented:
Thanks for the help guys!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.