Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting AnsiString to Int

Status
Not open for further replies.

bitshifter05

Programmer
Jul 15, 1999
13
US
Using Borland builder when I try to use an edit box as in input for an integer it says that you can't convert an ansistring to an integer. How do I get integers from the user.

Thanks
bitshifter [sig][/sig]
 
Use the function atoi [sig]<p>hnd<br><a href=mailto:hasso55@yahoo.com>hasso55@yahoo.com</a><br><a href= > </a><br> [/sig]
 
hnd's solution may work but I believe that AnsiString may need it's own method to convert. What you will need to do is accept a AnsiString input from the user, then convert the answer to an integer. This is what hnd suggested. I think that you may need to use StrToInt. This should correctly convert Borland's AnsiString to integer. One suggestion is to put this into a try ... catch statement in case the user inputs something that cannot be converted to an integer.

[sig]<p>James P. Cottingham<br><a href=mailto: > </a><br><a href= Veneer Co., Inc.</a><br>All opinions are mine alone and do not necessarily reflect those of my employer.[/sig]
 
Ok ffat there are two ways:


String str;
int value;
str=EditBox->Text;
1. value=atoi(str.c_str());
2. Value=StrToInt(str);

But they are equivalent.
I personally prefer the first way. Probably because i come from a UNIX/C environment about 15 years ago and there were no String variables but characters.

[sig]<p>hnd<br><a href=mailto:hasso55@yahoo.com>hasso55@yahoo.com</a><br><a href= > </a><br> [/sig]
 
hnd,

[tab]I wasn't certain since AnsiString is Borland's Pascal String implimentation. I was afraid that the standard C++ atoi wouldn't work with it. I do know that I have to use c_str() to go from AnsiString to string. Thanks for the tip, though.
[sig]<p>James P. Cottingham<br><a href=mailto: > </a><br><a href= Veneer Co., Inc.</a><br>All opinions are mine alone and do not necessarily reflect those of my employer.[/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top