Advertisement

Online C++ Compiler

Write, run and test C++ code online, right in your browser. This free online C++ compiler needs no installation or signup — just type your code and click Run.

C++ extends C with classes, templates and the Standard Library. It is widely used for games, competitive programming, browsers and high-performance systems.

How to use the C++ compiler

  1. Write or paste your C++ code in the editor.
  2. If your program reads input, open the I/O tab and type it into the STDIN box (one value per line).
  3. Click Run — the output and any errors appear in the output panel.

Sample: read input in C++

#include <iostream>
#include <string>
using namespace std;

int main() {
    string name;
    getline(cin, name);
    cout << "Hello, " << name << "!" << endl;
    return 0;
}

Paste this into the editor, type your name in the STDIN box and click Run.

More C++ examples to try

Vectors and sorting

Reads numbers into a vector, sorts them with the Standard Library and prints the result. Try STDIN 5 42 7 19 3 25.

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    int n;
    cin >> n;
    vector<int> v(n);
    for (int &x : v) cin >> x;
    sort(v.begin(), v.end());
    for (int x : v) cout << x << " ";
    cout << endl;
    return 0;
}

Classes in C++

A class with private data, a constructor and member functions.

#include <iostream>
using namespace std;

class Rectangle {
    double w, h;
public:
    Rectangle(double w, double h) : w(w), h(h) {}
    double area() const { return w * h; }
    double perimeter() const { return 2 * (w + h); }
};

int main() {
    Rectangle r(4, 2.5);
    cout << "Area: " << r.area() << "\n";
    cout << "Perimeter: " << r.perimeter() << "\n";
}

Common C++ errors and how to fix them

error: 'cout' was not declared in this scope
Add #include <iostream> and either using namespace std; or write std::cout.
Time limit exceeded
Your loop never ends or the algorithm is too slow. Check loop conditions, and prefer efficient algorithms such as sort over nested loops for large inputs.
error: expected ';' before '}' token
A statement or class definition is missing a semicolon — remember classes end with };.
Reading a full line after cin skips input
After cin >> n, call cin.ignore() before getline(cin, line) so the leftover newline is skipped.

Frequently asked questions

Is this online C++ compiler free?

Yes. It is completely free to use, with no signup required to write and run code.

Do I need to install anything?

No. It all works from your web browser, so there is nothing to download or set up — it works on Windows, macOS, Linux, Chromebooks and phones.

How do I give input to my C++ program?

Before clicking Run, type the input into the STDIN box under the I/O tab, one value per line. Your program reads it as standard input, just like typing in a terminal.

Is this good for learning C++?

Yes. It is ideal for practising exercises, testing small programs and trying out ideas from tutorials, because you can run code instantly without configuring an environment.

Other online compilers

Editor powered by OneCompiler.