Lesson 20 +25 XP

Project 1: Hello World Builder

Project 1: Hello World Builder

Time to build your first real program. You already know the pieces - #include, int main(), cout, and comments - so practice connecting them into one working, compile-able file.

The goal

Write a C++ program that prints a hello message, then prints your name and your age on separate lines.

What you practice

  • Writing the #include preprocessor directive
  • Setting up int main()
  • Printing with cout and the stream operator <<
  • Leaving comments your future self can read

Starter code

Start from this real program:

#include <iostream>
using namespace std;

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

Save it as hello.cpp and compile with g++ hello.cpp -o hello.

Step-by-step

  1. Inside int main(), declare string name; and an int age;.
  2. Give them values - your own name and age.
  3. Print with cout << "My name is " << name << "!" << endl;.
  4. Print cout << "I am " << age << " years old." << endl;.
  5. Add a comment above main() that describes the program in one sentence.
  6. Recompile and run. Tweak the wording until the output reads naturally.

Checklist

  • [ ] #include <iostream> is the first content line
  • [ ] using namespace std; lets you use cout
  • [ ] int main() returns an integer
  • [ ] Your name and your age are both printed
  • [ ] A comment explains the program
  • [ ] The program compiles and runs without errors

TL;DR

  • Start with the right #include so cout works.
  • Code runs inside int main().
  • cout << value << endl prints text and numbers.
  • // starts a single-line comment.