Your first program

It's best to start with a simple problem. Let's go through a solution to problem 8804, which asks you to read two integers and print their sum. For example, on the input 3 5 the answer is 8. The full solution is below.

#include <bits/stdc++.h>
using namespace std;

int main() {
    int a, b;
    cin >> a >> b;
    cout << a + b << '\n';
}

The first two lines include the library the program needs and set up a namespace. Together they're called the header. For now, take the header as part of a template. You'll learn how it works later.

After the header comes the function main. This is the body of your program, where you write your code. The line int main() { starts it, and the braces { and } mark where the body of main starts and ends.

The body of main has three lines, run from top to bottom, and each one is an operation:

  • int a, b; makes two integer variables a and b, named places that hold values.

  • cin >> a >> b; reads two numbers from the input into a and b.

  • cout << a + b << '\n'; prints their sum, then '\n', a line break.

Each of these lines is a statement, one instruction, and each one ends with ;. You can learn more about variables, input and output on this page.

Programs can also contain comments, notes you leave in the code for yourself or for other readers. The compiler ignores comments, so they are only for people who read your program. A comment runs from // to the end of the line, or from /* to */, even across several lines:

/* Reads two integers
   and prints their sum. */
cin >> a >> b;          // Read
cout << a + b << '\n';  // Print

Compiling

Before your program runs, the computer has to translate the C++ code into instructions it can carry out. This step is called compiling, and a program called the compiler does it.

If the program has mistakes, the compiler can't compile it, and you get a compilation error instead of a program to run. Its error messages explain what is wrong with your program and why the compiler can't make sense of it. They may look technical, but always read them carefully. After the file name, most messages start with the position where the compiler failed, in the form <line>:<column>. For example, if you forget the ; at the end of line 7, the compiler reports 7:26: error: expected ';' before '}' token: the error is at line 7, column 26. Fix the first error first, because the errors after it are often caused by the same mistake.

Submitting a solution

To submit the solution, sign in and open the problem Sum of two integers. Paste the whole file into the editor on the problem page, choose C++ under Language and press Submit.

The judge runs your program the same way you would run it on your own computer. A computer, not a person, compiles the file, then runs the program several times with different inputs, called tests, and compares each output with the correct answer. If every output matches, the solution is Accepted. If one doesn't, you get Wrong answer.

Common mistakes

Forgetting the semicolon at the end of a line

Without the ; at the end of line 7, GCC reports expected ';' before '}' token on line 7. Add the ; after '\n'.

Leaving out the using line of the header

Without using namespace std;, the first error is 'cin' was not declared in this scope; did you mean 'std::cin'?. Add the line, or write std::cin.

Leaving out the include line of the header

Without the include line, the error is 'cin' was not declared in this scope, followed by a note that 'std::cin' is defined in header '<iostream>'. Add the include line.

Writing Main with a capital letter

With int Main() {, the error is undefined reference to `main', with no line number: every line is correct C++, but there is no main to start from. Write main in lower case.

Losing the closing brace of main

Deleting the last } gives expected '}' at end of input, reported at the end of the file, far from where the brace was lost. Keep each } in line with the start of the line that opened its {, so a missing one is easy to see.

Copying old headers from textbooks

#include <conio.h> and a getch(); at the end, which old school materials use to keep the window open, give a compilation error on the judge, conio.h: No such file or directory, so remove both lines.

Continue reading

Practice

Was this page helpful?