Solved

C++ Builder newbie ?

Posted on 1998-09-01
5
385 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
[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
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

707 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