iostream is the library you use to do input and output.

The most important parts to us this week are cin and cout.

cin is used for keyboard input and cout is used for character output.

Your first program

#include <iostream>
using namespace std;

int main() {
    cout << "Hello world!" << endl;
    return 0;

The first line of code enables input and output

The second line is magic. Ignore it for now.

The fourth line of code contains the main function. This is the entry point for every c++ program you will ever create.

The fifth line prints "Hello world!" to the user's screen.

The sixth line stops the execution of this function, and says that everything went ok (the value 0).

Exercise 1

Write a story!

Get into groups, and spend 10-15 minutes creating a story.

  • What do we see when we're introduced to the world?
  • Who's around?
  • Why are things the way they are?

Make sure to leave holes in the story for character names, special objects, and the like. Then, write a program asking the user for this information! Use that information to tell the story you've come up with.

Make sure no single line of code is longer than 80 characters! Also, some output formatting would be nice. Start a new line after every sentence!

If you want a challenge, start a new line every 80 output characters (notice that output characters don't necessarily line up with the code).

Exercise 2

Show me your ascii art!

Google "ascii art" and find some pieces that you think are aesthetically pleasing. Then, write a program to print those out!

Exercise 3 (optional)

Write an interactive story!

You can use your story from exercise 1, and modify it a little. If you don't want to do that, spend 10-15 minutes coming up with a new setting.

Next, come up with 10 decisions our main character has to make.

After you have the questions, put them code. The answers (for simplicity) should be binary; there are only 2 answers per question.

Also, you don't need to create a branching story tree! If the player answers incorrectly, end the story. I can provide an example of how to code that on the board.

This activity involves material from chapter 3 (branches)! Don't attempt it unless you're comfortable with going ahead.




The answers to the blank sections above are in white. Highlight them to reveal the answers! Make sure you at least attempt an answer first!