Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

funny error with putting values into char

Posted on 2004-11-02
3
Medium Priority
?
150 Views
Last Modified: 2010-04-24
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
Comment
Question by:richjo100
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 200 total points
ID: 12477180
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
 
LVL 16

Accepted Solution

by:
nonubik earned 200 total points
ID: 12482061
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
 

Author Comment

by:richjo100
ID: 12485456
Thanks for the help guys!
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question