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. What to do, If such errors:

    name followed by ‘::’ must be a class or namespace name – 5 string, 10 string

    Error C2653 ‘mycode’: is not a class or namespace name -5 string

    C4430 missing type specifier – int assumed. Note: C++ does not support default-int – 6 string

    C4508 ‘mycode’: function should return a value; ‘void’ return type assumed
    – 7string

    C2653 ‘mycode’: is not a class or namespace name -10 string

    C4430 missing type specifier – int assumed. Note: C++ does not support default-int – 11 string

    C2084 function ‘int mycode(void)’ already has a body – 11 string

    1. “C++ does not support default-int” – missing type variable in the description, add, … such as int.

      “function should return a value; ‘void’ return type assumed” – you do not have a return statement in the function.

      With mycode you do something written at all is not so.

    2. Most likely you wrote the program in the header file (mecode.h), not in the source file (mycode.cpp)

    1. seocod.ru/forum/viewforum.php?f=31
      mylinuxprog.blogspot.com/2018/01/c-2.html
      flibusta.is/a/36262
      it links. But since this site does not allow the publication of references, simply copy it into your browser.

  2. The seriousness of the Code Project Description File status bar suppression
    Ошибка (actively) E1696 Can not open source file “iostream” Project1 C:\UsersUSersourcereposProject1Project1Source.cpp 4

  3. WOW. And you think that one line “HELLO WORLD”, This program. Well hello world i can go anywhere, yes, at least write in a notebook!

    1. Technically yes – one line hello world is the program. You can write something more difficult – well done, although I doubt that you would have written something more complicated the first time.

  4. Programs are copied through the clipboard in different ways
    to *.cpp files in the CodeBlock environment. Possibly in other environments , did not check. I copied the programs from the book electronically via the clipboard. If you copy line by line (or enter text manually), then it's all right , the program compiles and runs normally . But if you copy the entire text of the program to the clipboard at once , then there are errors when compiling.. Although outwardly the text looks the same in both cases.
    What would explain this ?

  5. 1. Hello World!!!
    2. D:\My projectsFirst_project64DebugFirst_project.exe (process 6464) finished working with the code 0
    3. Press any key, to close this window:

    How to remove it in Visual Studio 2022 second line.

Leave a Reply

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