Home » Tutorials » C++ Programming » 1.1. Getting started with C++ » 1.1.2. Basic input and output, introducing libraries

This section will introduce a new, and critical area in c++, the library. A library is called by using #include in your program. In c, the standard library for input and output was "stdio.h" (Standard Input / Output), but the functions printf and scanf had limitations and have been superseded by cout and cin. You will still see programs that use printf and scanf, and there is nothing wrong with that, but by using the cout and cin, more checks are made on your code and you get fewer problems long-term.

#include the hash (or pound, #) symbol indicates that this is a preprocessor directive.

Time for another sample of basic code:

#include <iostream>
int main()
{
std::string name;
std::cout<<"Enter your name: ";
std::getline(std::cin, name);
std::cout<<"\nHello "<<name<<"!\n";
return 0;
};

There is quite a lot of new stuff going on here, so don't worry too much about specifics for now, as I'll go through it all gradually. The first thing to note is that there is "#include <iostream>" at the beginning of the program. All this does is instruct the preprocessor to write the code from the file "iostream.h" into that position in the code (NOTE: older compilers may still need to have "#include <iostream.h>" to work correctly). This accomplishes three important things:

  1. It keeps your code tidy by keeping out unnecessary clutter
  2. It allows you to reuse code from both the standard libraries, and any that you write yourself
  3. It means that you don't have to rewrite anything that's already been done before

The third point is a tricky one, because only experience tells you which standard library to use for which function, and what functions are available. Although, unless it's something highly specialised, the chances are that someone has implemented something for it before. One of the libraries that I'll be working with later in the tutorial will be SDL as this has many functions that simplify cross-platform game coding.

The next addition to the code is "std::string name". This tells the compiler to create a variable of type string. There is the odd looking "std::" in front of it because the string type isn't standard to C, and this tells the compiler to look in the namespace "std".

The next line: "std::cout<<"Enter your name: ";" outputs the string "Enter your name: " to the default output stream (cout). By default this is the monitor, but it can be redirected to other devices for other purposes.

Similarly, this line: "std::getline(std::cin, name);" uses the standard input stream (by default, the keyboard), and puts it into the variable "name". "getline" is a function (denoted by the parentheses), which is also in the std namespace.

Finally, for this section, you can tell the compiler to look for objects in the program to use certain namespaces by using the following:

#include <iostream>
using namespace std;

int main()
{
string name;
cout<<"Enter your name: ";
getline(cin, name);
cout<<"\nHello "<<name<<"!\n";
return 0;
};

This helps clean up the code even more by getting rid of the "std::", as the compiler will know to look there now.

Key:

  • Keyword - a name reserved for c++ that cannot be used for any other purpose, ilke "if", "include", "char", etc.
  • Library - a collection of objects usually involving tasks in a related area; e.g. "string" is a collection of string handling routines, variables, etc, and can be called with "#include <string>"
  • Namespace - this is a collection of functions and variables collated together with a common identifier. There can be several namespaces within one library for slightly different areas of a library "topic".
  • Object - a specific element of a program that can be referenced by name. This includes variables, classes, functions, namespaces, etc.
  • Preprocessor directive - an instruction that is performed before the compiler works on the code; e.g. "#include" inserts code from the file that follows it directly into the page. Another use can be to set a parameter at the start of the program to add debugging options that only work in a test build of an application (i.e. using #define and #if).
  • Type - indicates what type of data a variable contains: e.g. char for a character, or int for an integer.
  • Variable - a reference to a value in memory.