Solved

C++ Builder newbie ?

Posted on 1998-09-01
5
377 Views
Last Modified: 2008-02-01
I am a moderately experienced C programmer with a good basic understanding of C++ in a DOS environment.  I am learning Borland C++ Builder and would like some help with string conversions.

I simply want to be able to use the VCL standard "AnsiString" you get from a text field, etc. with regular C conversion functions, such as atoi() or _ltoa().  Is there a data member of an AnsiString I should access, or are there VCL conversion functions I should use instead?

Example:

long Calc1=1.3, Calc2=0.25;

Control->Text=(_ltoa(Calc1/Calc2));  // Simply won't work

Thanks much in advance!
Terry
0
Comment
Question by:tchris
  • 2
  • 2
5 Comments
 
LVL 2

Accepted Solution

by:
duneram earned 50 total points
ID: 1171839
in windows we typically use wsprintf something like this

First you should use float or double for your 'calc variables' otherwise they will be truncated

so (I'm just using c for this example)

float calc1=1.3;
float calc2=.25;


wsprintf(Control->Text, "%f", calc1/calc2);

This says the answer will be stored as a string/text in floating point format and to div the two for an answer and store the answer in string format in Control->Text;


0
 
LVL 1

Author Comment

by:tchris
ID: 1171840
Great!  Thank you... How about going the other way?

i.e.

long Val1;

Val1 = atol(Control->Text); // Nope...

Thanks,
Terry
0
 
LVL 2

Expert Comment

by:duneram
ID: 1171841
you could use sscanf or if val1 was float

val1 = atof(control->text);


0
 
LVL 1

Author Comment

by:tchris
ID: 1171842
Thanks for the effort, but that doesn't work.  There is a VCL method called GetTextBuf() that allows you to access the actual char* for the text.  This allowed me to send it on through the atol() mill for further processing.

Also, as an aside, I used a different approach for the floating point-to-AnsiString conversion than you provided:

long Value1;
char buf[32];

_ltoa(Value1,buf,10);
AnsiString ABuf(buf);  // A constructor is provided for this...
Control->Text = Abuf;

It proved to be rather simple once I found the constructor.

However, since your answer got me in the right direction, I am happy with the answer anyway.

Thanks!

Terry
0
 

Expert Comment

by:dj1encore
ID: 1171843
I have no problem in doing it that way but how about getting the text from the user and manipulating it?

but to answer your answer try

Control->Text = atol(Buffer);


0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

856 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