Hello World

This is the first article in the series OK. Let's do this.

I keep trying to think of something big and interesting to write about, but that is just not working. All my time is spent trying to think of a blockbuster idea, which just gets in the way of writing. So, let's start with the small things. If I can get into the habit of writing something a couple of times a week, maybe we can work up to interesting stuff.

Step one: Write about something I know. C++; we now start the "So you want to learn C++" series of posts.

I am going to assume two things.

  • You know how to use the compiler
  • That you have some basic programming experience C/Java/C#/Perl/PHP (nearly anything) So, you understand the basics of programming but are unfamiliar with C++.

The first thing everybody needs is to get something working; here is the classic "Hello World" in C++.

helloworld.cpp

#include <iostream>

int main()
{
    std::cout << "Hello World\n";
}

The next step is to accept user input and generate a response based on that input. Let's move on to the not quite as classic "Hi There Bob" :-)

hitherebob.cpp

#include <iostream>
#include <string>

int main()
{
    std::cout << "Hi there what's your name?\n";

    std::string  line;
    std::getline(std::cin, line);

    std::cout << "It was good to meet you " << line << "\n";
}

The above code is relatively simple, and only a few things to note:

  • #include <iostream> Imports the standard input and output facilities so you can print messages to the user and read user input.
    • std::cin Is the standard input stream. From this, you can read user input.
    • std::cout Is the standard output stream. You can print text to the user console.
  • #include <string> Imports the standard string handling function. Most importantly, it imports the type std::string.
    • std::string This is one of the standard types and holds strings (a list of characters). In subsequent articles, we will go over types (and variables) in more detail. But for now, accept that line is a variable (of type std::string) used to hold a line of user input.
    • std::getline() This function reads a line of text from a std::istream into a std::string. In this case, we use std::cin as the input stream (it is a specialization of an std::istream and can thus be used as the input). Therefore, we read a line of input from the user.

There are many other concepts encapsulated above that I don't want to discuss yet, but don't worry—I will cover them all.

Related Posts

C++ Wrapper for Socket

The last two articles examined the "C Socket" interface provided by the OS. In this article, I wrap this functionality in a simple C++ class to provide guaranteed closing and apply a consistent except

Read More

Common Mistakes

### 1: using namespace Every new developer who comes to C++ always starts writing code like this: #### myfirstprog.cpp ```c #include <iostream> using namespace std; ``` It seems reasonable, and e

Read More

Control Flow

So far, we have created basic programs that perform a single task without making any decisions. Most (all but the most trivial) programming languages provide decision-making constructs (Conditional B

Read More