Site icon Learn C++

Five Simple C++ Console Examples for Beginners

Five Simple C++ Console Examples for Beginners

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.

How to develop a C++ console application in C++Builder CE?

If you download C++ Builder Community Edition then we can start to coding,

  1. Create a new C++ Builder Console application from File->New-> menu
  2. In the New Console Application window leave Target Framework blank “None”
  3. 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,

[crayon-6743a829c8f09690619427/]

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.

[crayon-6743a829c8f38855351177/]

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,

[crayon-6743a829c8f3a251910969/]

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.

[crayon-6743a829c8f3b063188198/]

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),

[crayon-6743a829c8f3c608338825/]

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.

[crayon-6743a829c8f45890473650/]

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

[crayon-6743a829c8f46907176212/]

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.

[crayon-6743a829c8f47045271477/]

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:

[crayon-6743a829c8f4a244902908/]

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.

[crayon-6743a829c8f4b995740875/]

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,

[crayon-6743a829c8f4c643956402/]

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.

Exit mobile version