C++ Builder Tutorial

In this tutorial we will create a conversion calculator which will convert miles to kilometers, centigrade to Fahrenheit and vice versa.

Below is a view of the completed project.

The first thing to do is create a new project and then save it under an appropriate name, I used convert.

Next place 2 Button components on the form and in the properties dialog box change the caption of button1 to Calculate and the caption of button2 to Clear, also change the Font property to character size 10 and highlight Bold for the font style for each button.

Now add 2 Editboxes to the form and delete the text in the text property for each box. Next add 2 labels and change the caption property for label1 to Miles and label2 to Kilometres.

Now add 4 radio buttons and change the captions as in the picture above. The next thing to do is change the name of each of the radio buttons to MtoK KtoM CtoF FtoC.

Now double click on the Calculate button and you should now be looking at the code below

void __fastcall TForm1::Button1Click(TObject *Sender)

{

}

Between the { and } add the following code.

const double MILETOKILO = 1.60934;

const double KILOTOMILE = 0.62137;

 

The above 2 lines of code set constant values to the words MILETOKILO and KILOTOMILE

Note: constants are used to give a name to a value and cannot be changed throughout a program or procedure. In C++ the const modifier is used to set a constant.

Now add the next 2 lines of code.

double inputval, outputval;

AnsiString s;

These 2 lines of code declare the variables we'll be using throughout the procedure.

The first line sets up 2 type double variables, variables unlike constants can have their value changed by the program or procedure.

 

Note: the two variables we have just declared are called local variables, this means that they can only be used inside the procedure they are defined in and cannot be used out side of it (this is called the scope of the variable). If the variables had been declared out side of any procedure they would have been global variables and could have been used by any procedure within the program.

The second line of code declares an AnsiString this is not a standard C++ declaration type, but does make string handling in C++ Builder much easier than the standard C++ method which involves using pointers and arrays. (these will be covered in later tutorials)

Enter the code below:

s = Edit1->Text;

inputval = atof(s.c_str());

The first line assigns the text entered in the top edit box Edit1 to the AnsiString variable s which is then assigned to the float variable inputval.

Note: to assign the AnsiString variable s to the float variable inputval we have to use the function atof which means convert assci to float or convert string to a floating point number, (the c_str() part of the statement is required because atof() expects a standard C type string rather than an AnsiString type string).

Enter the code bellow.

if (MtoK->Checked == 1) {

outputval = inputval * MILETOKILO;

}

if (KtoM->Checked == 1) {

outputval = inputval * KILOTOMILE;

}

if (CtoF->Checked == 1) {

outputval = ((inputval - 32) + 5) / 9;

}

if (FtoC->Checked == 1) {

outputval = ((inputval * 9) / 5) + 32;

}

 

s = outputval;

Edit2->Text = s;

 

This code uses an if statements to check each of the checked methods of the RadioButtons until it finds one that = 1 (or true) it then does the conversion calculation and prints the answer in Edit2.

We now need to set the labels on the left of the Edit boxes to reflect which of the Radiobuttons we have selected and to show which conversion is being done.

 

To do this we need to create an event handler for each RadioButton, this is done by double clicking on each of the RadioButtons in turn and adding the following code.

To MtoKClick add

Label1->Caption = "Miles";

Label2->Caption = "Kilos";

Edit1->Text = "";

Edit2->Text = "";

Edit1->SetFocus();

To KtoMClick add

Label1->Caption = "Kilos";

Label2->Caption = "Miles";

Edit1->Text = "";

Edit2->Text = "";

Edit1->SetFocus();

To CtoFClick add

Label1->Caption = "Centigrade";

Label2->Caption = "Fahrenheit";

Edit1->Text = "";

Edit2->Text = "";

Edit1->SetFocus();Edit1->Text = "";

Edit2->Text = "";

Edit1->SetFocus();

To FtoCClick add

Label2->Caption = "Centigrade";

Label1->Caption = "Fahrenheit";

Edit1->Text = "";

Edit2->Text = "";

Edit1->SetFocus();

Now whenever we click on a RadioButton the labels on the left of the Editboxes indicate which conversion we are doing, also both of the editboxes are cleared and Edit1 receives the focus, (the cursor is placed in the Editbox ready for input).

The only button we now need to write code for is the Clear button or Button2 we do this with the following code

Edit1->Text = "";

Edit2->Text = "";

Edit1->SetFocus();

This code is the same code we used in the Click procedures for the RadioButtons so the explanation for those applies here.

The program is now complete and if you run it should work fine.

There is an improvement we could make to the code which although it wouldn't make a difference to the way the program works would use less code and make the program more compact.

To do this we could write a procedure that could be called from each of the RadioButton and Clear procedures that would execute the three lines of code for us.

Edit1->Text = "";

Edit2->Text = "";

Edit1->SetFocus();

 

The following is an explanation of how this can be done.

When we write functions in C++ we first need to declare the functions prototype, we do this inside the Tform1 class in the programs header file. To get to the header file you right click the mouse over the edit window and select open source/header file. You should now see another tab on the edit window which ends in .h, press this tab if it is not already selected and scroll down the code until you reach the public: //User declarations part of the class Tform1 and under this declaration enter the following line:

void __fastcall clean(void); // 2 under scores

This line declares a public function that returns no value and takes no parameters (this is what the 2 void statements mean). We now move back to our program code using the tab and scroll down to the end of the code and enter the following

void __fastcall TForm1::clean()

{

Edit1->Text = "";

Edit2->Text = "";

Edit1->SetFocus();

}

We now have the function declared and written and all that is left to do is delete the code bellow from each of the procedures:

Edit1->Text = "";

Edit2->Text = "";

Edit1->SetFocus();

And replace it with:

clean();

and run the program.

That is the end of this tutorial I hope you find it useful. I plan to write more tutorials in the future so please keep checking my home page. If you have any comments on this tutorial or anything else on the site please Email me my Email address is on my home page.

The number of visitor to this page =

Return to index page

1