edit box text manipulation

Again, as it is my first time using BCB, I am having some trouble with the basics. My latest problem? Type mismatches. Im trying to read in a number value from a simple edit box to use it as a variable in an equation. For example, assume edit box "edit" exists:

int TempTickX, XAxis;

TempTickX = XAxis / (edit->Text);

The text property of edit returns an AnsiSting. My mistake was obvious once I had made it, but I can't seem to come up with a solution. I tried type casting, but to no avail. Any suggestions would be useful. Thanks.

I don't have C++Builder, but you need to use a built-in function or method that converts a string into a numeric value.

In C, there are functions like ASCII-to-integer/float/long or string-to-double/long/unsignedlong: atoi(), atof() atol(), strtod(), strtol(), strtoul(). These take a character array as their parameter and return the numeric value represented in that string.

String classes usually have similar conversion functions or can be used with library functions. If you try to use the standard C functions, then you will need to expose the character string contained in the AnsiString object. For that, you would need to read through AnsiString's methods.

Here's what you did:
NXTick and NYTick are char*, which means that they point to a character. It also means that they could be an array of characters, in which case they are pointing to the first character in the array. When you dereference a char* , then you are only looking at a single character, that first character in the array. So then, yes, you were actually trying to assign an AnsiString object to a single char, even though that is not what you wanted to do.

In class, you were probably taught to use STL string objects. In C, we only have character arrays, for which there is an entirely different set of rules that make extensive use of pointers. While with string objects and AnsiString objects you can copy a string simply with an assignment statement, in C you have to copy each character from one array to another. That is accomplished with the strcpy() function. Since you are not doing C and won't be using it for now, I'll just let you research that one on your own.

The thing is that deep down inside, STL string objects and BDB AnsiString objects and MFC CString objects all have a C-style character array -- or more likely a char pointer. A number of Win32 API functions require a LPSTR, which is essentially a char* . Therefore, these string classes include a method for exposing that hidden char array: string uses the c_str() method and CString uses the GetBuffer() method. I just don't know what the method is in the AnsiString class. But the point is that if you ever need to pass an AnsiString to a function that requires a char*, then you will need to use that method.

But in the meantime, BCB has provided functions that do the same thing as atoi(), only with AnsiStrings: StrToInt(). So go with that.

Thank you very much guys. That works quite well. I can now have users define things (::dance::). Really, thanks though, you are really making my life easier. I'm sure i'll have more trouble just give me a few minutes.

Originally posted by AGibel Thank you very much guys. That works quite well. I can now have users define things (::dance::). Really, thanks though, you are really making my life easier. I'm sure i'll have more trouble just give me a few minutes.

Hey, nobody can program or work with computers without ever having any trouble. I hope you have trouble for many more years to come.

To complete the discussion, if you want to use atoi(), then it should probably go like this:

int UserTickX = atoi(NewTicksX->Text.c_str());

The c_str() method of AnsiString returns a char * pointer, which is what atoi wants.

Also there's StrToIntDef(), that goes like this:
int UserTickX = StrToIntDef(NewTicksX->Text, -1);
In this case, if NewTicksX->Text contains some invalid characters (i.e. non-digits), then the value of UserTickX will be set to the default value that you supplied to StrToIntDef (in this case, -1).