Codiqo

Learn C++ · lesson 1 of 61 · about 5 minutes

Hello, C++

Start your own text adventure: print its title screen with std::cout, and fix your first compiler error.

What you'll learn

  • Write a main() function
  • Print with std::cout and <<
  • Read a compiler error and find the line

Start your own game

Most big games are built with C++, because it's fast: a compiler turns your code into instructions the processor runs directly, with nothing in between. In this unit you'll build a text adventure, a game played entirely in words, and it starts with a title screen.

Every C++ program starts in a function called main.

The title screen

#include <iostream>

int main() {
    std::cout << "=== The Dark Cave ===\n";
    std::cout << "You wake up in a dark cave.\n";
}

Output

=== The Dark Cave ===
You wake up in a dark cave.

What each part does

  • #include <iostream> brings in the tools for input and output
  • int main() { ... } is where the program starts
  • std::cout is the console output, and << sends something into it
  • "\n" ends the line
  • Every statement ends with a semicolon

The compiler reads the whole program before it runs anything. If one line is wrong, nothing runs at all, and the compiler tells you which line it didn't understand.

Chaining with <<

#include <iostream>

int main() {
    std::cout << "Lives: " << 3 << "\n";
    std::cout << "Gold: " << 5 << " coins" << "\n";
}

Output

Lives: 3
Gold: 5 coins

Tip

std:: means the name comes from the standard library. Some code writes using namespace std; to skip it, but writing std:: makes it clear where things come from.

Now try it yourself

This lesson is free in Codiqo, with 7 questions on what you just read. You can only learn to code by writing code, so it ends with a short program you write yourself, which we run and check straight away.