The basics of programming in c++ for beginners

Writing the first program in C++

In a previous articleInstalling the Microsoft Visual Studio environment 2015 Express (IDE)   we with you learned how to create a project in the development environment and add to it a new element (файл с расширением .cpp). Exactly in this file we will give clear commands to our computer.

Unfortunately, the computer can not understand us– it understands only one language –  machine code. but program thus it is impossible, many programming languages have been created. One of these is the C++ language. This language has a certain number of special words reserved logical, whereby in Microsoft Visual Studio(as with any other development environment) you can write commands to our computer. And then using the built-in compiler, this code will be converted into machine code, that will be processed, and we will show the result of the work program.

Well, start writing program. To do this, you need to open your created project. You go in the Microsoft Visual Studio. Before you open Home.

написание первой программы на c++

Here is there, where it is written last, must be a newly created project – click on it. If you have not yet created projects – create, as shown in this article. Start to write the following code. Strongly recommend not copy code, and manually type. Without practice in programming it is hard…  ))

Let's understand now, what we wrote. In 1-St and 2-nd lines are comments to our code. The entire text of the line, what is behind the double slash –  //  – completely ignored by the compiler. It simply does not see it. So we can anywhere in our code to leave comments – any information, which we deem necessary. Something like notes, to remember or pay attention.  Comments there are multiline, because if a comment is big, It is uncomfortable in front of the beginning of each line a double slash.  To create a multi-line comment, need a whole text or code, you want to ignore, placed between /*   and*/

More about the rules and coding standards, read our article Formatting source code.

In string 4 of program we see#include <iostream> – this preprocessor directive. While we will not delve into – just write so each new program.  The same applies to the line 5 – this is, the so-called, namespace connection std that contains the word commands, that we will use when writing each program.

Lines 7 – 14 located the main function: main ( ) { /*program code * / } When you run the program, it is always executed first and it between the two braces { } are our commands. Here's an entry in a row 9functionsetlocale(LC_ALL, “rus”); – will properly display on the screen Cyrillic signs. Prescribe the function in those programs, where you will be required to display the Russian text.

Moving to a string 11 – Here we entered the command cout (keyword from namespace std), which is responsible for data output to the screen, operator << followed by the text. The text should be placed in quotation marks. So we show the compiler, that it is not a command for it, and the usual string. In line write any text. When the text is written, quotation marks are closed again and write the operator <<  for which again the compiler command – endl;  (the transition to the next line).  Semicolon ; to put definitely. It said tocompiler, the command finished and can proceed to the next command.

At the end of the function block main()  visible command return 0;  It said tocompiler, that at the end of the programme it is necessary to return a value 0. Also didn't hesitate so far on how, what does it mean. Think of it, as a kind of rule – so it is necessary to write in each program. Any of our program in the following lessons, will be contain such records:

Use it as a template, before writing new programs.

How to run our program, to see the text on the screen, which has been placed in the source quoted? To do this, press Ctrl + Shift + B-will begin compiling a program.

If you make some mistakes in the code, Ddbugger to detect them and notify you. At the bottom of the window you will see a list of these errors. They need to be corrected. Well, if there are no errors on the bottom line of the window we see Assembling: successfully: 1, with errors: 0 etc.  Then press the Ctrl key + F5 and see in the window that opens, a message is, that requested:

написание первой программы на c++

The program has worked and performed our instructions properly. If you have any difficulties or errors, that you can not fix yourself, ask questions in the comments to this article. Together, we will look :)

If everything is clear and you are not tired, proceed to the next lesson– Data types, variables and constants in c++. And if you are tired, it still does not stop and watch related videos :)

40 thoughts on “Writing the first program in C++

  1. Similarly,! That's just me wondering why it is necessary to press Ctrl + Shift + B, I have a combination of Ctrl has + F7 (kompilirovaty), F7 (compose, otherwise as I understood – compile and assemble), and then I click or F5 to start debugging, or simply press Ctrl + F5 to run the file without debug ekzeshnogo, so to speak: “Piping hot, directly on the screen”!!! I have Visual Studio 2013 Express, And so I have everything just… F7, control + F5 and run… the only thing that had to do, it's a bit to adjust itself Wednesday (IDE) With a programming language + + (as this medium can be used by C #, F#, Visual Basic, but I needed it With + +), for this I went to the tab “Service”, then chose “Import and export settings”, hit “reset all settings”, I chose “without saving any of the settings such as” and voila, I became my AyDishka “sharpened” With it under your tongue + +, a set of keyboard shortcuts for the commands described above (and many other) It is automatically set to “the plus” YOU + + 6 standard keyboard…
    All successful zahabritsya…
    Z.Ы. there is one but… the size and position of windows again exposed by default, and if you change something for themselves, you'll have to reconfigure everything again… too, first, better to do it, and then everything else!!!
    IMHO: so comfortable with the layout, when everything is installed under the C language + +…

  2. good afternoon!!
    There was a question about the display…When the program starts,by pressing Ctrl + F5 , there is a window just for a moment longer.
    What could it be?

    1. All I understood, just 2013 function “_ getch” – not determined,
      and here “_ getchar_nolock()” – determined

  3. Excellent lesson. Authors Respect. I have 2015, everything works well. If the rest of the classes will have the same informative, I volyus the army of programmers!!!!

  4. I have a problem: if you run any code, there is a window with the inscription “”Exist:\users(my yuzerneym)\documentsvisual studio 2015ProjectsConsoleApplication1DebugConsoleApplication.exe”” not an internal or external command, operable program or batch file.

    1. I think, you are missing out, that run code written primarily to collect (compile), in an operation menu build, and only after that you can run the compiled application.

      How do you start?

  5. After the assembly program with a combination of Ctrl + F5 starts normally, but in the last line writes, “Press any key to continue”

    When you run the .exe file all works, but the console is closed immediately although I used _getch

    When I click Local Windows Debugger (with the green arrow on top)))
    That's what gives me:

    ‘Lesson1.exe’ (Win32): Loaded ‘C:\UsersBaroshaDocumentsVisual Studio 2013ProjectsProgrammingLessonsDebugLesson1.exe’. Symbols loaded.
    ‘Lesson1.exe’ (Win32): Loaded ‘C:\WindowsSysWOW64ntdll.dll’. Cannot find or open the PDB file.
    ‘Lesson1.exe’ (Win32): Loaded ‘C:\WindowsSysWOW64kernel32.dll’. Cannot find or open the PDB file.
    ‘Lesson1.exe’ (Win32): Loaded ‘C:\WindowsSysWOW64KernelBase.dll’. Cannot find or open the PDB file.
    ‘Lesson1.exe’ (Win32): Loaded ‘C:\WindowsSysWOW64msvcp120d.dll’. Cannot find or open the PDB file.
    ‘Lesson1.exe’ (Win32): Loaded ‘C:\WindowsSysWOW64msvcr120d.dll’. Cannot find or open the PDB file.
    The program ‘[2680] Lesson1.exe’ has exited with code 0 (0x0).

    I use MVS 2013 express for desktop skachennыy with Microsoft website

  6. Hello, I'm new to this field.. Tried to write your first program So here's what happened.

    // эта программа будет выводить на экран любое
    // сообщение, which we will write

    #include
    using namespace std;

    int main()
    {
    setlocale(LC_ALL, “rus”);

    cout << "Пишем нашу первую программу на языке С++" <—— Building started: project: Project 1, Configuration: Debug Win32 ——
    1> mycode.cpp
    1>LINK : fatal error LNK1123: failure during conversion to COFF: the file is invalid or corrupted
    ========== Construction: successfully: 0, with errors: 1, without changes: 0, missing: 0 ==========

Leave a Reply

Your email address will not be published. Required fields are marked *