Skip to Content
Course content

3: Your First C++ Program

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Alright, you've got your environment set up. Now comes the part where most people just copy-paste a "Hello World" snippet and move on. We aren't doing that. I want to show you how C++ actually thinks, because the first time you hit a compiler error, it's going to feel like the computer is shouting at you in a language you don't speak. Let's walk through this together.

Wait, where is cout?

I'm going to start by writing the absolute bare minimum I think I need to print a message to the screen. Here is my first attempt:

int main() {
    cout << "Welcome to the machine!";
    return 0;
}

If I try to run this, the compiler is going to lose its mind. It'll give me a wall of text, but the key phrase is 'cout' was not declared in this scope. Essentially, the compiler is saying, "I have no idea what a 'cout' is. Is that a variable? A function? Who is she?"

In C++, the core language is actually very small. Basic input and output aren't built into the language itself; they live in a library. To use them, I have to explicitly tell the compiler to bring in the Input/Output Stream library.

#include <iostream>

int main() {
    cout << "Welcome to the machine!";
    return 0;
}

The Namespace Hurdle

I'll bet you're thinking, "Great, fixed it." But if I compile this now, I'll get another error: 'cout' is not a member of 'std' (or something very similar). This is a quirk of C++ that trips up everyone.

C++ uses "namespaces" to prevent naming collisions. Imagine if you and I both wrote a function called print() and then tried to use both our libraries in one project. The computer wouldn't know which one to use. To solve this, almost everything in the standard library is wrapped in a namespace called std (short for standard).

I have two choices here. I could put using namespace std; at the top, but honestly? Don't do that. It's a bad habit that leads to massive headaches in larger projects. Instead, I'll just be explicit and use the scope resolution operator :: to tell the compiler exactly where cout lives.

#include <iostream>

int main() {
    std::cout << "Welcome to the machine!";
    return 0;
}

Now it works. The std::cout is the "character output stream," and those arrows << are the insertion operators. I'm essentially "pushing" the text string into the output stream.

Making it Interactive

Printing text is fine, but a program that doesn't take input is just a fancy typewriter. Let's try to make this program ask for the user's name. I'll need a place to store that name, so I'll use a std::string. But wait—if I just add std::string, I'll probably get another error because strings aren't part of the basic iostream library.

Let's adjust the code to include the string library and use std::cin (character input) to grab a value from the keyboard.

#include <iostream>
#include <string>

int main() {
    std::string userName;
    
    std::cout << "Enter your name: ";
    std::cin >> userName;
    
    std::cout << "Hello, " << userName << "! You've just written your first C++ program.";
    
    return 0;
}

Notice how I used std::cin >> userName;. Notice the arrows? They point the opposite way of cout. We are now "extracting" data from the input stream and pushing it into our variable. I also chained multiple items together in the final cout line—C++ lets you string together as many pieces of data as you want in one line, as long as they are separated by those insertion operators.




📋 Practical Task

Build a Simple Age-in-Days Calculator

Write a program that asks the user for their age in years (as an integer) and then calculates and prints out approximately how many days they have been alive.

  • Include both <iostream> and <string> (though you might only need the former for this specific task).
  • Use std::cin to capture the user's age.
  • Perform the math (Age * 365) inside the std::cout statement or in a separate variable.
  • Ensure you use the std:: prefix for all standard library components.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.