Build with C++

Your First Program

1 of 23LabValues and Types

Programmed before? Try these 5 questions and skip what you already know. Nothing is scored, and every step stays open.

On this page

Your First Program

A C++ program is text. A second program, the compiler, reads that text and turns it into something the computer can run. This lesson shows what the text is made of, how to read it, and what happens when the compiler does not accept it.

You need no experience. Everything here runs in your browser.

A program is a list of statements

Here is a complete program. It draws a robot's head.

#include "scimigo.h"
using namespace scimigo;

int main() {
    canvas(600, 400);
    rectangle(200, 100, 200, 180, "orange");
    circle(260, 170, 15, "green");
    circle(340, 170, 15, "green");
}

Look at the four lines between the braces { and }. Each one is a statement: one instruction. Each statement ends with a semicolon ;.

The computer carries out the statements in order, one at a time, from the top. First it makes a canvas. Then it draws an orange rectangle. Then one green circle, then another.

Order matters, because a later shape is drawn over an earlier one. If the two circle statements came before the rectangle statement, the rectangle would cover both eyes and you would see an orange block.

The lines above int main() { are not statements of yours. They are explained in The frame around your statements. For now, read only what is between the braces.

One call, taken apart

Each of those statements is a call: it asks for a job to be done. Here is one of them with its parts named.

circle(260, 170, 15, "green");
Part What it is
circle the name of the job
( and ) parentheses around the values the job needs
260, 170, 15, "green" four values, separated by commas
; the end of the statement

The values have a fixed order, and each position has a meaning. For circle the order is: how far right the centre is, how far down the centre is, the radius, the colour. Swap two values and you get a different circle, or no program at all.

This course gives you four drawing jobs.

Call What it does
canvas(width, height); starts an empty picture of that size
circle(x, y, radius, "colour"); a filled circle with its centre at (x, y)
rectangle(x, y, width, height, "colour"); a filled rectangle with its top-left corner at (x, y)
text(x, y, "words", size); words starting at (x, y), in letters of that size

These four belong to this course. They are not part of C++ itself. The C++ you learn around them (statements, values, and everything in later modules) is the same C++ used everywhere.

Where is (x, y)?

Every drawing call takes a position as two numbers, x and then y.

  • x is the distance from the left edge. A larger x is further right.
  • y is the distance from the top edge. A larger y is further down.

The point (0, 0) is the top-left corner. On a canvas 600 wide and 400 high, the bottom-right corner is (600, 400).

This is not the way graphs are drawn in a mathematics class, where y grows upward. On a screen, y grows downward. Nearly every graphics system works this way.

#include "scimigo.h"
using namespace scimigo;

int main() {
    canvas(600, 400);
    circle(100, 100, 20, "red");
    text(130, 108, "(100, 100)", 18);
    circle(500, 100, 20, "blue");
    text(380, 108, "(500, 100)", 18);
    circle(100, 300, 20, "green");
    text(130, 308, "(100, 300)", 18);
}
x=100x=200x=300x=400x=500y=100y=200y=300(0, 0)(100, 100)(500, 100)(100, 300)

The red and blue circles have the same y, so they are level. The red and green circles have the same x, so one is directly below the other. The green one has the larger y, and it is the lower one.

Numbers, and text in quotes

The values in a call are of two kinds so far.

  • A number is written as digits: 260, 15.
  • Text is written between double quotes: "green", "My Robot".

The quotes are how the compiler tells the two apart, and they change the meaning.

You write The compiler reads
40 the number forty
"40" text made of the characters 4 and 0
"green" text made of five letters
green a name. Nothing called green exists, so this is an error

A call that expects a number will not accept text in that position, and a call that expects text will not accept a number. The compiler checks this before the program runs.

Printing with std::cout

A program can also produce text. The statement below sends five characters to the output panel.

#include <iostream>

int main() {
    std::cout << "Hello";
}
Hello

Read std::cout << "Hello"; as "send Hello to the output". std::cout is the output, and << points in the direction the text travels.

A second statement continues exactly where the first one stopped. Nothing is added between them: no space and no new line.

#include <iostream>

int main() {
    std::cout << "Hello";
    std::cout << "World";
}
HelloWorld

To end a line, put \n inside the quotes. Those two characters together mean "new line". They are not printed as a backslash and an n.

#include <iostream>

int main() {
    std::cout << "Hello\n";
    std::cout << "World\n";
}
Hello
World

The frame around your statements

Every program in this lesson has the same lines around its statements. Here they are, with the one statement you already understand.

#include <iostream>

int main() {
    std::cout << "Hello\n";
}
Hello
  • #include <iostream> makes std::cout available. A program that draws has #include "scimigo.h" as well, which makes the four drawing calls available, followed by using namespace scimigo;. Module 3 says more about #include.
  • int main() { and the final } mark where the program starts and ends. Your statements go between them. Module 6 explains every word of that line.

You can also write notes for yourself. Everything from // to the end of a line is a comment, which the compiler skips.

circle(260, 170, 15, "green");   // the left eye

Until those modules, treat the frame as fixed. Type your statements between the braces and leave the rest as it is.

When the compiler says no

Before any statement runs, the compiler reads the whole program. If it finds one thing it cannot accept, anywhere, it stops and reports it. Then nothing runs: not the statements after the mistake, and not the ones before it either.

This program has one mistake, in its last statement.

#include "scimigo.h"
using namespace scimigo;

int main() {
    canvas(600, 400);
    circle(300, 200, 80, "red");
    circle(300, 200, 40, "white")
}

The semicolon after the last call is missing. The compiler answers with a message like this one.

your_program.cpp:7:34: error: expected ';' after expression

Read it in three parts.

  • your_program.cpp:7:34 is the place: line 7, and 34 characters along that line.
  • error means the program was not accepted.
  • expected ';' after expression is what the compiler wanted to find there.

No red circle appears, although the statement that draws it is correct. The program as a whole was rejected.

Three mistakes account for most of the messages you will see in your first week.

Mistake A typical message
a missing ; expected ';' after expression
a misspelt name, such as cirle use of undeclared identifier 'cirle'
a missing closing quote missing terminating '"' character

A compiler message is information. It names a line. Go to that line, read the message again, and look at that line and the one before it.

Your turn

Now try it yourself

Predict what a program will do, run it, and step through it line by line. The checks tell you when your own code works.

Loading the lab…

In this lab

  1. Make it yours

    This program draws a robot. Press Run and look at the picture.

    Then change at least one colour or one number, and run it again. Colours are English colour names in double quotes, such as "purple" or "gold".

  2. Take one call apart

    Look at this one statement. It is not a whole program, so there is nothing to run yet.

    circle(300, 200, 40, "orange");
    
  3. Which shape is on top?

    This program draws a circle and a rectangle that overlap. Decide before you run it.

  4. Where does it land?

    The canvas is 600 wide and 400 high. This program draws one small circle.

  5. Move the eye

    The robot's first eye has ended up beside its head. Change one number so that the eye sits on the face, level with the other eye.

    The head starts at x = 200 and is 200 wide.

  6. Number or text?

    Four values, as you might type them into a call:

    40        "40"        "orange"        orange
    
  7. What does it print?

    This program has two output statements. Choose what appears in the output panel.

  8. Three lines

    This program should print an address on three lines. Run it and see what it prints.

    Add what is missing so that the output is exactly:

    Ada Lovelace
    12 St James's Square
    London
    
  9. Did any of it run?

    This program has one mistake: a semicolon is missing at the end of its last statement. The first two statements are correct.

  10. Finish the robot

    The robot has a head and one eye. Add three things.

    1. A second eye: the same size and colour as the first, 80 further to the right, at the same height.
    2. A mouth: a black rectangle 80 wide and 12 high, with its top edge at y = 230, centred under the two eyes.
    3. A title: the words My Robot above the head, in letters of size 28. Any position with y less than 100 will do.
  11. A sign from a description

    Only the canvas is given. Draw this road sign, and print its caption.

    • A black post, 20 wide, stands in the middle of the canvas. Its top is at y = 200 and it reaches the bottom of the canvas.
    • In front of the post is a yellow sign: a square 200 on each side, centred on the canvas, so that it hides the top half of the post.
    • On the sign is the word SLOW, in letters of size 40.

    The canvas is 600 wide and 400 high. Work out each position before you type it.

    The program also prints exactly these two lines:

    Sign: SLOW
    Post height: 200
    
  12. Stop sign

    Draw a stop sign.

    • A red disc somewhere on the canvas.
    • A white bar across the disc: a rectangle wider than it is high, with its centre at the centre of the disc, and narrower than the disc so that red shows on both sides.
    • The word STOP below the disc.

    You choose the sizes and the position.

  13. Put the statements in order

    This program should draw a face on a blue background. Every statement is correct, but when you run it you see only blue.

    Put the statements in an order that shows the whole face. Move lines; do not change what is inside them.

  14. Three broken programs

    This robot does not compile. It has three mistakes, each of a different kind.

    Press Run and read the first message. It names a line. Repair that line, run again, and read the next message. Repeat until the robot appears.

    The compiler often stops at the first mistake, so you will not see all three messages at once.

  15. A box of text

    Print a name in a box, exactly like this:

    +-----+
    | Ada |
    +-----+
    

    There are three lines. The top and bottom lines are a +, five - and a +. The middle line has one space on each side of the name.

  16. Your own flag

    Design a flag of your own, with at least four shapes. It can be a real flag or an invented one.

    Nothing checks this one. When you are happy with it, go through the list below.