There are many different ways to program computers. However, the programmer himself chooses how to do this. However, there are many guidelines on how to use styles and functions to make compilation easier and get a safer program. We also recommend that you write your code so that it is understandable to other programmers, such as your colleagues.
Steps

Step 1. Download IDE (Integrated Development Environment) for C ++
For example, download NetBeans, Eclipse, or CodeBlocks. You can also use a regular text editor, for example, VIM or Notepad ++ - in this case, the programs can be run from the command line. If you want, choose an editor that highlights syntax and line numbers. Most programmers believe that programs are best written on Unix systems (Linux, OS X, BSD).

Step 2. Create the main program file
It should contain the main () function, and the execution of the program begins in it. In this file, functions will be called, class instances will be instantiated, and so on. Other program and library files can be included in the main file.

Step 3. Start writing the program code
Below you will find examples of program code. First, you need to become familiar with syntax, semantics, object-oriented programming paradigms, data structure, algorithm design (such as bulleted lists), priority queues, and the like. Programming in C ++ is not very easy, but with it you will learn the basics that will come in handy when working with other programming languages.

Step 4. Add comments to the code
In them, specify what the functions do and what the variables are for. Remember that global variable names are in capital letters. Try to make your code understandable to other programmers.

Step 5. Use appropriate indentation in your code
(See examples below.)
6 Compile the code using the command
g ++ main.cpp

Step 7. Start the program To do this, enter
./a.out
Examples of program codes
- Example 1:
/ * This is simple code to introduce you to the basics of g ++ style. This is a program with a g ++ compiler. * / #Include / * include input and output functions * / using namespace std; / * use std (standard) functions * / int main () / * declare the main function; it is also possible to write int main (void). * / {cout << "\ n Hi Dad"; / * '\ n' - new line, (t - tab) * / cout << "\ n Hello Mom"; cout << "\ n This is my first code"; cout << "\ n Date 2019-06-11"; return 0; }
- Example 2:
/ * This program will calculate the sum of two numbers * / #include using namespace std; int main () {float num1, num2, res; / * declare variables; you can also use int, double, long * / cout << "\ n Enter the first number ="; cin >> num1; / * Assign the entered value to the num1 variable * / cout << "\ n Enter the second number ="; cin >> num2; res = num1 + num2; cout << "\ n Sum of" << num1 << "and" << num2 << "=" << res '\ n'; return 0; }
- Example 3:
/ * This program will calculate the product of two numbers * / #include using namespace std; int main () {float num1; int num2; double res; cout << "\ n Please enter the first number ="; cin >> num1; cout << "\ n Enter the second number ="; cin >> num2; res = num1 * num2; cout << "\ n Product of two numbers =" << res '\ n'; return 0; }
- Example 4:
// Code with a loop to find mathematical equality. In this case, there is a solution // to problem # 1 from the "Euler" project #include using namespace std; int main () {// Open the main part. int sum1 = 0; int sum2 = 0; int sum3 = 0; int sum4 = 0; // Create integer variables that are needed to find a solution. for (int a = 0; a <1000; a = a + 3) {sum1 = sum1 + a;} // We loop until the variable a is equal to 1000 or more, while adding 3 to the variable a with each cycle We also add the variable a to the variable sum1. for (int b = 0; b <1000; b = b + 5) {sum2 = sum2 + b;} // Loop until b is 1000 or more, adding 5 to b with each loop We also add the variable b to the variable sum2. for (int c = 0; c <1000; c = c + 15) {sum3 = sum3 + c;} // We loop until the variable c becomes equal to 1000 or more, while adding 15 to the variable c with each loop We also add the s variable to the sum3 variable. sum4 = sum1 + sum2 - sum3; // Add sum1 and sum2, subtract sum3 and assign the found value to sum4. cout << sum4; // The value of the variable sum4 is displayed on the screen (this is the solution). cin.get (); // Wait for the user to hit Enter. return 0; // This is a return statement. } // Close the main part.
- Examples of different styles:
int main () {int i = 0; if (1 + 1 == 2) {i = 2; }} / * This is Whitesmiths style * / int main () {int i; if (1 + 1 == 2) {i = 2; }} / * This is GNU style * / int main () {int i; if (condition) {i = 2; function (); }}
Advice
- Better to use the ISO compiler.
- By default, the computer creates an executable file "a.out".
- If your code contains many different variables or functions, add comments to make the code easier to understand and debug.