Small Steps VI — Growing into C++

Writing down the tools you need

Write down the tools you need.

What you will learn

A header provides declarations so you can use tools from other code. #include is a directive to include that header.

Try it

Example · example1.cpp
#include <iostream>
#include <string>

void small_main()
{
    std::string name;

    std::cout << "Name: ";
    std::getline(std::cin, name);

    std::cout << "Hello, " << name << "\n";
}

Include iostream for standard input/output and string for std::string. std::cout << value produces output; std::getline(std::cin, name) reads a line. Entering Alex produces Hello, Alex.

Keep small_main this time and change only the input/output tools.

Exercise

Write #include <iostream> and use std::cout to print Hello C++ and a newline. Keep using small_main() for now.

Exercise starter · exercise1_starter.cpp
#include <iostream>

void small_main()
{
    // print Hello C++ with std::cout.
}

Hint

Send values to the output stream as in std::cout << "Hello C++" << "\n";.

show solution
#include <iostream>

void small_main()
{
    std::cout << "Hello C++" << "\n";
}
← Previous lessonNext lesson →