• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 152
  • Last Modified:

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
0
richjo100
Asked:
richjo100
2 Solutions
 
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
 
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
 
richjo100Author Commented:
Thanks for the help guys!
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now