C++. An example of application development of calculating of cylinder volume in C++ Builder

An example of application development of calculating of cylinder volume

The task

The radius R and the height of the cylinder H are given. We need develop the application, which calculates the volume of cylinder.

Application must be realized as “VCL Forms Application“.

Formula of volume of cylinder:



  1. Run Borland C++ Builder.

Save the project.

  1. Creating the form.

Place on the form such components:

  • three components of type TLabel (for outputting text “R = “, “H = “, “V = “);
  • two components of type TEdit (input field) to input the values of radius and height;
  • one component of type TButton for beginning calculation.

Components of type TLabel, TButton and TEdit are located in tab Standard of Tool Palette (Fig. 1).


Fig. 1. Components TLabel, TButton and TEdit on tool palette


2.1. Component TLabel.

After placing component on the form, automatically the object-variable with name “Label1” are created.

Upon request, the name can be changed in the Name property in the Object Inspector (Fig. 2). The same applies to the other components (TButton, TEdit, etc.).

Set the text message in Label1 by changing the property “Caption” to value “R = ” (Fig. 2).


Fig. 2. Setting up the text “R = ” in component Label1

After performing, the application form will look as shown in Figure 3.


Fig. 3. Application form in design mode

Also we place on the form the component of TLabel type with name “Label2“. In property “Caption” enter the text “H = “. The application form has the form as shown in Figure 4.


Fig. 4. Application form after placing components Label1 and Label2


2.2. Component TEdit.

Place on the form two components of type TEdit. Automatically, two objects (variables) with names “Edit1” and “Edit2” are created. Change the property Text of components Edit1 and Edit2 so, that the text must be empty.

As a result, the application form has the following view (Fig. 5).


Fig. 5. Application form with components Edit1 and Edit2 which are placed on the form


2.3. Component TButton.

Place on the form component of type “TButton“. The object-variable with name “Button1” is created.

In the Caption property of component Button1 we type the text “Calculation” (Fig. 6).


Fig. 6. Changing the property “Caption” of component “Button1”


2.4. Output of result.

Result will be showed in component of type TLabel. Property Caption set to value “V = “.

Before inputting program code, the application form will have the following view (Fig. 7).


Fig. 7. Application form after performing operations


2.5. Changing of form’s title.

Select the form (activate Form1 to Object Inspector). Changing the title text of form by using property Caption (Fig. 8). Set the property Caption to value “Calculating of volume of the cylinder“.


Fig. 8. Changing the form’s title in property “Caption


  1. Programming the event handling of clic at the button “Calculation”.

The Next step is programming the code snippet, which will be called after that, when user clic on the button “Calculation“.

En example of programming of event handling you can see here.

To call the snippet of event handling you need to do following steps:

  • select the component Button1;
  • in the Tool Palette go to tab “Events“;
  • double click opposite the name of the event “OnClick“.

As a result, C++ Builder will generate the following code:

void __fastcall TForm1::Button1Click(TObject *Sender)


Between braces we make the text of calculating the volume of a cylinder. Overall, the event handler will look like this:

void __fastcall TForm1::Button1Click(TObject *Sender)
     double r, h, v;
     const double pi = 3.1415;
     r = StrToFloat(Edit1->Text); // reading R
     h = StrToFloat(Edit2->Text); // reading H
     v = pi*r*r*h;
     Label3->Caption = "V = " + FloatToStrF(v,ffFixed,8,2);

We explain some of the code fragments.

Getting of inputting data r and h is given from the input strings Edit1->Text and Edit2->Text.

Since, the variables r and h are the double type, and properties Edit1->Text and Edit2->Text are the strings, then for correctly assignment is used the conversion function StrToFloat.

The inverse transform from the numerical type into string is done by the function FloatToStr.

This function gets the input value (variable v) and converts it to string type corresponding to given format. In our case is given fixed format – ffFixed. Also, is given precision (2 decimal places) and output width (8 characters).


  1. Running the application.

Running the application is done by key F9 or by command Run from menu Run. Result of application running is shown in figure 9. 05_01_00_007_09_

Fig. 9. Application running

Related themes:

Delphi – Calculating of the area of circle and volume of a sphere.

C# – An example of creating of Web-application in MS Visual Studio.