The C++ Builder CE Community Edition is a free version of professional C++ Builder that you can develop GUI based desktop and mobile applications in C++. In this post, we will give you five simple C++ console examples for beginners you can run using C++ Builder 11 CE.
The latest C++ Builder 11 CE was released in April 2023. If you are a start-up developer, student, hobbyist or just interested in learning to code then C++ Builder Community Edition may well be just the thing for you. Read the FAQ notes on the CE license and then simply fill out the form and download C++ Builder 11 CE.
Table of Contents
How to develop a C++ console application in C++Builder CE?
If you download C++ Builder Community Edition then we can start to coding,
- Create a new C++ Builder Console application from File->New-> menu
- In the New Console Application window leave Target Framework blank “None”
- Be sure that Source Type is C++ and press OK
This will open a new code editor window at the center.
When you start coding, first of all, you should include libraries that you wish to use. The C++ language has many libraries and each of them has commands or functions for specific tasks. For example, the iostream
library has standard input and output methods to display data and read from files and similar sources. Generally, for beginners, the iostream
header is enough to enable you to create simple apps. We can include this library header as below,
1 2 3 |
#include <iostream> |
Second, you should add a main
procedure. This is the main part of the program – hence the name – and it is executed first. In the simplest sense, all other parts of your program are launched from the main section either directly or indirectly. Things get a little more complicated than that once you start to write more complex programs but for now you can think of the main
section as the ‘main loop’ where things begin to happen in your program code.
1 2 3 4 5 6 |
int main() { } |
After that you should write your lines of program code into this procedure, between the { and } brackets. You can print texts by using std::cout
as below,
1 2 3 |
std::cout << "Hello LearnCPlusPlus\n"; |
In C and C++, after every command you use you should put a semicolon “;”. In our example we print a text with std::cout
method and then we wait to get a character input (key press) with system("pause");
method, then we exit and return 0;
which means our main app has run successfully and exits.
If we sum up all, here is a “Hello World” C++ example below that can be run with C++ Builder.
1 2 3 4 5 6 7 8 9 10 11 |
#include <iostream> int main() { std::cout << "Hello LearnCPlusPlus\n"; system("pause"); return 0; } |
In this example above we define main()
function as an integer (int
) function, that means we should return an integer value. Here, we print a text with printf()
function and then we wait to get a character input (key press) with getchar();
function, sometimes this is necessary to see results when running it. Then we exit and return 0;
which means our main app successfully ran and exited.
You can write these C++ code above into the Dev-C++ code editor or you can copy and paste it. If you are a beginner, we highly recommend you write code. This will help you to understand how C++ works and you can see your first mistakes when you are coding.
After this step, you can compile by pressing F9 run your code by pressing F10, or you can use Compile and Run buttons. This will print out the “Hello LearnCPlusPlus”. C++ compilers support C language too, you can compile the C example below,
How to do calculations in a C++ console application?
Here is another C++ example that calculates the weight of a mass by using F =m.a
formula (or W = m.g
),
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> int main() { int m = 10; float a = 9.81; F = m*a; // calculate std::cout << F ; // print F system("pause"); return 0; } |
After this step, you can compile by pressing F9 run your code by pressing F10, or you can use Compile and Run buttons. This will print the F Force value.
Normally, here you should use float
for the m variable, we use int to teach how you can use int variable.
How to enhance our C++ console application?
We can improve our code as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <iostream> #include <string> int main() { float m = 10; // mass (kg): integer number float a = 9.81; // gravity (m/s2): floating point number float F; // Force (Nt): floating point number std::string str = "LearnCPlusPlus.org"; // string F = m*a; // Force = mass x accelaration std::cout << "Mass:" << m << std::endl; std::cout << "Gravity:" << a << std::endl; std::cout << "Weight of mass=" << F << " Nt" << std::endl; std::cout << std::endl; std::cout << str << std::endl; system("pause"); return 0; } |
After this step, you can compile by pressing F9 run your code by pressing F10, or you can use Compile and Run buttons. Here is the output
1 2 3 4 5 6 |
Mass:10 Gravity:9.81 Weight of mass= 98.1 Nt LearnCPlusPlus.org Press any key to continue . . . |
How to get input in a C++ console application?
Now, let’s enter these all variables and our name as a string. This example is good to learn how you can use std::cin
to get inputs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#include <iostream> #include <string> int main() { int m = 10; // mass (kg): integer number float a = 9.81; // gravity (m/s2): floating point number float F; // Force (Nt): floating point number std::string str = "LearnCPlusPlus.org"; // string std::cout << "Mass (kg):"; std::cin >> m; std::cout << "Gravity (m/s2):"; std::cin >> a; std::cout << "Your Name:"; std::cin >> str; F = m*a; // Force = mass x accelaration std::cout << std::endl; std::cout << "Results:" << std::endl; std::cout << "Mass: " << m << " kg" << std::endl; std::cout << "Gravity: " << a << " m/s2" << std::endl; std::cout << "Weight of mass= " << F << " Nt" << std::endl; std::cout << std::endl; std::cout << str << std::endl; system("pause"); return 0; } |
After this step, you can compile by pressing F9 run your code by pressing F10, or you can use Compile and Run buttons. Here is the output:
1 2 3 4 5 6 7 8 9 10 11 |
Mass (kg):100 Gravity (m/s2):9.85 Your Name:Yilmaz Results: Mass: 100 kg Gravity: 9.85 m/s2 Weight of mass= 985 Nt Yilmaz Press any key to continue . . . |
How to use loops in a C++ console application?
In engineering, if you want to calculate precisely you can use double
instead of float
variables. Let’s calculate all masses between 0 and 100 (including 0 and 100). Todo this, we will use for()
loop, and we increase m with m+=1.0
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> int main() { double a = 9.81; // gravity (m/s2): floating point number double m, F; for (m = 0; m <= 100; m+=1.0) { F = m*a; std::cout << "Mass: " << m << " F =" << F << " Nt" << std::endl; } system("pause"); return 0; } |
After this step, you can compile by pressing F9 run your code by pressing F10, or you can use Compile and Run buttons. Here is the output,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
Mass: 72 F =706.32 Nt Mass: 73 F =716.13 Nt Mass: 74 F =725.94 Nt Mass: 75 F =735.75 Nt Mass: 76 F =745.56 Nt Mass: 77 F =755.37 Nt Mass: 78 F =765.18 Nt Mass: 79 F =774.99 Nt Mass: 80 F =784.8 Nt Mass: 81 F =794.61 Nt Mass: 82 F =804.42 Nt Mass: 83 F =814.23 Nt Mass: 84 F =824.04 Nt Mass: 85 F =833.85 Nt Mass: 86 F =843.66 Nt Mass: 87 F =853.47 Nt Mass: 88 F =863.28 Nt Mass: 89 F =873.09 Nt Mass: 90 F =882.9 Nt Mass: 91 F =892.71 Nt Mass: 92 F =902.52 Nt Mass: 93 F =912.33 Nt Mass: 94 F =922.14 Nt Mass: 95 F =931.95 Nt Mass: 96 F =941.76 Nt Mass: 97 F =951.57 Nt Mass: 98 F =961.38 Nt Mass: 99 F =971.19 Nt Mass: 100 F =981 Nt Press any key to continue . . . |
You can find many other examples on LearnCPlusPlus.org.
For more information on this feature, see Extending sizeof Proposal document.
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise version.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition