1. That really depends on your CPU (16, 32, or 64 bit) since the math chip is different. See the program below from
Thinking in C++, 2nd Edition by Bruce Eckel on how to determine what size each is.
CODE
//: C03:Specify.cpp
// Demonstrates the use of specifiers
#include <iostream>
using namespace std;
int main() {
char c;
unsigned char cu;
int i;
unsigned int iu;
short int is;
short iis; // Same as short int
unsigned short int isu;
unsigned short iisu;
long int il;
long iil; // Same as long int
unsigned long int ilu;
unsigned long iilu;
float f;
double d;
long double ld;
cout
<< "\n char= " << sizeof(c)
<< "\n unsigned char = " << sizeof(cu)
<< "\n int = " << sizeof(i)
<< "\n unsigned int = " << sizeof(iu)
<< "\n short = " << sizeof(is)
<< "\n unsigned short = " << sizeof(isu)
<< "\n long = " << sizeof(il)
<< "\n unsigned long = " << sizeof(ilu)
<< "\n float = " << sizeof(f)
<< "\n double = " << sizeof(d)
<< "\n long double = " << sizeof(ld)
<< endl;
} ///:~
2. To display numbers in text boxes, you need to convert the numbers to an AnsiString. (Note: In newer verions of C++Builder, you would want to use UnicodeStrings.) There are different functions to convert different values to AnsiStrings. For example, to convert doubles to AnsiStrings, you would want to use the
FloatToStrF function.
static AnsiString __fastcall FloatToStrF(long double value, TStringFloatFormat format, int precision, int digits);
Thus
IND_OUT->Caption=L;
should become
IND_OUT->Caption=FloatToStrF(L, ffExponent, 15, 4);
This will display L in scientic notation. Since L is a double you should use 15 digit precision and I just put in 4 digits for the decimal.
Look in your help file for AnsiString methods.
3. Correct. Multiplying numbers of lesser precision with number of higher precision will result in lesser precision numbers. Thus an integer times a double will result in an integer. In C++, you can get around this by throwing the lesser precision number to a higher number.
CODE
int Aint = 355;
int Bint = 113;
double Pi = (double)Aint / (double)Bint; // this will result in a double float number
4. One of the things I would do is create a function that is called by your ButtonClick function. To me this would clean things up. Also see my answer to Question 5.
5. One of the things that C++ does is free memory for variables once they go out of scope. This is one reason why you should rarely declare global variables. Declare needed variables in the function then pass them back to the calling function. All the variables you declare in TSDIAppForm::Button1Click(TObject *Sender) are freed once you exit that function.
6. All data from text boxes will be AnsiStrings. Thus you would do the reverse of Question 1; however, there is a gotcha. What would happen if you a non-numeric entry in the textbox? The answer is you would get an exception thrown if you tried to convert a non-numeric entry to a numeric one. So you either have to wrap your conversion routine in a try..catch statement or use
TryStrToFloat.
Quote (Help File):
Use TryStrToFloat to convert an AnsiString, S, to a floating-point value. S must consist of an optional sign (+ or -), a string of digits with an optional decimal point, and an optional mantissa. The mantissa consists of 'E' or 'e' followed by an optional sign (+ or -) and a whole number. Leading and trailing blanks are ignored.
The DecimalSeparator global variable defines the character that must be used as a decimal point. Thousand separators and currency symbols are not allowed in the string. If S doesn't contain a valid value, TryStrToFloat returns Default.
TryStrToFloat places the result in Value, and returns true if the conversion was successful, false
otherwise.
CODE
AnsiString In_Out_Str = IND_OUT->Caption;
if (!TryStrToFloat(In_Out_Str, L)) // There is an error
{
MessageDlg("You do NOT have a valid number!", mtError, TMsgDlgButtons() << mbOK, 0);
// Do something about it here
}
7. I would look at
Thinking in C++ 2nd Edition by Bruce Eckel for a refresher/update course on C++.
James P. Cottingham
I'm number 1,229!
I'm number 1,229!