Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker.

Building A Hello World Example C++ Application

TwitterFacebookRedditLinkedInHacker News

I decided to take some time every month to go back to the basics when it comes to development. As you might know, I earned a Computer Science degree from the University of California, Merced, and most of the courses I took involved C++ development. While some of the more modern code schools teach JavaScript, the common university will continue to teach C++ for the foreseeable future.

Getting started with something like C++ can seem like a daunting task, especially since C++ is such a powerful development language. We’re going to see how to write, compile, and run a simple C++ application to help you get started with one of the more seasoned development options.

Before we get invested in the example, let’s figure out what this tutorial is not. This is not a finding and installing a compiler tutorial. I’m on a Mac, but the experience for getting started with a compiler is different for all platforms. If you’re confused about compilers, you might start by looking at g++ and MinGW.

Output Simple Data to the Screen

As the tutorial suggests in the title, we’re going to be doing a simple “Hello World” type application, common when going through the getting started content for a particular programming language.

Create a file titled main.cpp somewhere on your computer. Within the file, include the following:

#include <iostream>

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

By using cout we’re going to be using the standard output stream to write simple text to the screen followed by a new line character. By returning a zero we’re saying that our application ran without error, where as, a one would indicate an error.

To compile our application we can execute the following from the command line:

g++ main.cpp -o app

Assuming you’re using g++ as your compiler, the above command will compile our application and produce an output app file. The app file can be ran by executing the following:

./app

If everything went well you should see the text we defined previously outputted to the command line. We can do a little better when it comes to our application design though.

In our current code, notice that in the std::cout operation that we’re including the std namespace. Rather than including the namespace with each of our commands, we can actually define the namespace up front. Take the following revision to our project:

#include <iostream>

using namespace std;

int main() {
    cout << "Hello World\n";
    return 0;
}

While not a huge step up from what we had, it could potentially make your life a bit easier as the application grows.

Collecting Input from the User

Now that we know how to output data, we can attempt to collect user input and output what we’ve collected. Take the following changes to our application:

#include <iostream>
#include<string>

using namespace std;

int main() {
    cout << "Hello World\n";
    cout << "Input: ";
    string data;
    cin >> data;
    cout << "Output: " << data << "\n\n";
    return 0;
}

In the above code we’ve included a dependency for string data. After we display our “Hello World” text, we display some text to show the user that we’re looking to collect input. Before we collect the input we define a variable to hold it. Once we have our string in place, we can use the cin operator for standard input stream data. When a terminating character is used, such as the return key, the data is output and at which point the application ends.

Try to run the application and use a whitespace character such as tab or space. You’ll probably notice that the first part of the input is displayed, but not the rest. When using cin, the whitespace characters are considered terminating characters in the string. To get around this, we can do the following:

#include <iostream>
#include<string>

using namespace std;

int main() {
    cout << "Hello World\n";
    cout << "Input: ";
    string data;
    getline(cin, data);
    cout << "Output: " << data << "\n\n";
    return 0;
}

Notice that instead of using cin the way we had previously, we are now using the getline method. Unless you’re collecting numeric or boolean information, the getline approach is probably better.

Conclusion

You just saw how to build a simple “Hello World” style application using C++. In this application we saw how to output data, input data, compile, and run the application. Compared to languages like JavaScript, it is very unforgiving, but incredibly powerful.

If you’re still trying to start started with a compiler, check out the GNU Compiler Collection or MinGW.

Nic Raboy

Nic Raboy

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in C#, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Unity. Nic writes about his development experiences related to making web and mobile development easier to understand.