Your First C++ Program

#include <iostream>
using namespace std;

int main(){
    cout << "Hello World" << endl;
    return 0;
}

Running your programs

You can use one of the many online IDEs to run your programs directly from your browser.

However, I recommend having a compiler installed locally (I'll be using g++) and getting familiar with an editor/IDE of your choice (I'll be using VSCode).

Typically I would keep the input in a file called in.txt and the expected output in a file called out.txt.

If the program is in filename.cpp, then I will typically run the following commands from a terminal:

g++ filename.cpp -O2 -o run; ./run < in.txt > myout.txt; diff out.txt myout.txt

This has the effect of:

  1. Compiling the program into the executable run.
  2. Feeding the executable with the input from in.txt.
  3. Writing the output of the program into the file myout.txt.
  4. Comparing the contents of myout.txt with out.txt.
  5. If the terminal shows no output, then 😎 — the program worked exactly as expected, at least on this particular input file. Otherwise, it's debugging time! 🧐

When still fleshing out a solution we might just want to run the following:

g++ filename.cpp -O2 -o run; ./run < in.txt

This will have the effect of:

  1. Compiling the program into the executable run.
  2. Feeding the executable with the input from in.txt.
  3. Writing the output of the program to the console for visible examination.