[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

C++ Builder newbie ?

Posted on 1998-09-01
5
Medium Priority
?
398 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 200 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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.
Suggested Courses

650 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