June 11th, 2003, 08:46 AM
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.
June 11th, 2003, 11:48 AM
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.
That should give you an idea of what to look for.
June 11th, 2003, 12:37 PM
Ok, I found a function.
int atoi(const char *s)
however, I am still having some problems. Heres what I have
*NXTick = NewTicksY->Text;//<--right here it tells me I cant
*NYTick = NewTicksX->Text;//convtert AnsiString to char.
int UserTickX = atoi(NXTick);
int UserTickY = atoi(NYTick);
Another dumb mistake?
June 11th, 2003, 12:47 PM
Use StrToInt() instead
int UserTickX = StrToInt(NewTicksX->Text);
June 11th, 2003, 03:30 PM
Follow Scorpions' advice.
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.
June 11th, 2003, 04:07 PM
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.
June 11th, 2003, 04:22 PM
Hey, nobody can program or work with computers without ever having any trouble. I hope you have trouble for many more years to come.
Geraldine Chaplin at end of "Talk to Her":
"Soy maestra de baile. Nada es sencillo."
(I teach dance. Nothing is simple.)
June 11th, 2003, 07:05 PM
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).
Personally, I tend to use StrToInt() or StrToIntDef()