Lesson 2: Hello Beautiful World of Programming!

Let’s do this thing!  Let’s finally become a programmer!

I’m going to use Visual Studio Professional 2017 for this lesson.  If you are using a different IDE, the same options should be available, they will just be in a slightly different place in your IDEs GUI.

I’m going to hopefully cover the following in this lesson, if I forget something, holler at me in the comments below.

  1. Create a new project.
  2. Write a “hello world” program.
  3. Build and run your program.

If you know this information and would like to skip to the coding, scroll down to “Now let’s write a little bit of code”.

Creating your first project:

Whenever you develop new C++ programs, you will create a project that all of the source files will fit inside (for now).  On pretty much any IDE, you can go to:

[File > new > project]

:to get the new project window.  It will look something similar to the below screenshot.

makeProject

  1. Pick your language
    1. C++ in our case.
  2. If there are options under C++, you’ll want to pick Windows Desktop.
  3. In the middle section, you’ll want to pick a Windows Console Application.
    1. Basically our code will output to a cmd window.
  4. You’ll then want to go to the bottom and name your project.
    1. Since this is the fabled Hello World project, that’s what I named mine.
    2. It’s good practice to NOT include spaces in your project names.
  5. Finally, you’ll need to pick a location for your project.
    1. You can leave this at the default if you’d like, just remember where it is.
    2. Or you can move it to the location of your choosing.

Once your IDE finishes building your project, you’ll see something similar to the following.

For right now, we are going to be focusing on the HelloWorld.cpp file, which should have automatically opened when you created this project.

As you can see below, mine already has basic code in it, as should yours.

NOTE: the (#include “stdafx.h”) portion of my code is specific to Visual Studio (at least in my experience).  It needs to be the highest include statement in your code, but other than that, we can ignore it for now. You should not need to add this if you are using another IDE or editor.

firstScreen

The Solution Explorer window on the left is commonly displayed on the right hand side of the window in many IDEs.  I just like mine on the left, so I manually moved it once I got to this part.

Now let’s write a little bit of code. 

This lesson goes over how to write the code to print text to the screen.  In Lesson 3, I will break down the code and explain it a bit further.

To begin, we’re going to need to include the iostream library.  This is the input/output library and will be very useful as you get further in the lessons.

Above the main() function, include the following bit of code.

#include <iostream>

Now that we have included iostream, we will be able to output words to the screen.

To print words to the screen, include the following bit of code inside the curly brackets of the main function.

std::cout << "Hello Beautiful World of Programming!\n"

NOTE: you can also do the following:

std::cout << "Hello Beautiful World of Programming!" << std::endl;

You can see the orange part is different here from above.  Adding the endl portion to the end will create a new line and will also call flush(), which basically makes sure your file stream is updated with the data in that line. This is not necessary on Windows, but if you’re using another OS, try it this way if the original way doesn’t work.

Your completed code should look something like the following:
(don’t forget, we can ignore stdafx.h line of code for now).

completeCode

Build our program!

Each IDE is going to have a different way to build/run your program.

In Visual Studio:

  • Keyboard shortcuts
    • Build: ctrl+shift+B
    • Run: ctrl+F5
  • Or if you just hit ctrl+F5 and the program needs to build, it will prompt you to ask if you want to build the changes before running it.

 

When we run our code, we should get the following:

results

OR, it could display in the “console” window in your editor.  It just depends on what editor your using.

Visual Studio and Code Blocks will pop an actual console window (I’m fairly certain).

 

Bonus Time:

OK, so we ran the program from the IDE/Editor, but now where is the program that we wrote?  Shouldn’t it create an executable file that we can run outside of the IDE?

If you’re asking these questions, YOU ARE RIGHT!

I honestly think this is the coolest part of this lesson. What we just wrote in the editor, is now built into a standalone .exe file that you can give to someone else, they can put it on their computer, and run it successfully! (if it’s Windows based)

Navigate to: C:\Users\[user name]\Documents\Visual Studio 2017\Projects\HelloWorld\  (or wherever you saved your project)

After building the project above, there should be a “Release” or a “Debug” folder in the directory.  Open one of those folders.

Inside that folder you should see your .exe file.

exeFile

 

 

 

LOOK AT US!  WE’RE OFFICIAL PROGRAMMERS NOW!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s