Building C++ Program¶
Read time: 41 minutes (10487 words)
Now, some of you, who might have programmed in a language like C++ before, might think these is nothing in this lecture that will be of interest to you. You will just head off to some IDE and type away.
You are very wrong. We will work through a complete project, starting with nothing but a few basic tools, and end up with a program that demonstrates all of the basic skills you need to master to call yourself a programmer. Some of what you work through in this lecture will definitely be new, so pay attention!
Note
We will use a number of tools for this exercise. I will not explain everything you need to know about those tools in this lecture. Instead, I will show you only those features you absolutely need to know to build this project.
Note
If you are new to C++, do not worry. We will go over all of this in much more detail as we learn that language.
The Problem¶
We are going to create a program that adds up a bunch of numbers stored in a file. Just to make it interesting, I only want to add up every other number from the file!
We will work through this problem as though it is an important project. Maybe even one you get to set up during a job interview!
Step 1: Open up a Command Prompt Window¶
How you manage to get a term:command line` window depends on your system:
On a PC, you do this by launching a program called cmd.exe
. You can find it
on a Windows 10 machine by typing in “cmd” in the search box at the lower left
of your screen. You can also find it by scrolling through the list of
applications on your system, under Windows System -> Command Prompt
. Once
you launch this program, right-click on the new icon on the “Task Bar” which is
probably at the bottom of your screen. Select “Pin to taskbar” and this icon
will remain on the screen. You can simply click on it to launch the program
again.
On a Mac, you launch the “Terminal.app” application, which is hiding in the
“Applications/Utilities” folder. When you right-click on the icon on a Mac,
look under Options->Keep in Dock
On Linux, launch the “Terminal” program. On Ubuntu, right-click on the desktop,
and select “Open Terminal”. Once again, you can right-click on the icon on the
left side of the screen and select Add to Favorites
Customize the Window¶
I do not know what you like to look at, but I find the black background on these windows painful. On some systems, the font is too small to read comfortably. You can set things up to suite your tastes by doing the following:
PC Command Prompt¶
Right-click on the title bar at the top of the window. Select Properties
.
The tabs at the top of the window that appears let you tune things so the
window looks the way you prefer. I usually tweak the font size and the colors
so I end up with a light background, and dark letters. Since I often project my
desktop windows in class, I tend to overdo this sometimes. You get to decide! You will
be staring at this window a lot in this class.
Mac Terminal Window¶
On the Mac, select Terminal->Preferences
. There are many settings you can
play with here, but I usually just set up the font under the Text
tab.
Again, you should look around and tune this window to suit your style.
Linux Terminal¶
On Ubuntu Linus, select Edit Preferences
to get to the place where you set
things up. I usually tweak the font, and use a pre-defined color scheme.
Uncheck the Use colorsfrom system theme
and select one of the themes to see
how it looks. I use Solrized light
on my Mac systems.
Where am I?¶
Hopefully, you understand how the file system on your machine is set up. On a
PC, there is a file system on every drive on your machine, and each drive is
identified with a single letter. The C
drive is normally where you will
work.
Warning
If you use a lab machine, you will be working on your H
drive. We will
discuss that in class.
On Linux/Mac machines there is no drive letter to deal with.
All systems use a single letter, called a path seperator to show you where something is located on your system. The separator on Linux/Mac is a forward slash, on a PC it is a backward slash. After we get going in this project, I will be using Linux/Mac notation.
File System Paths¶
Your file system is made up of directories and files.
Note
Microsoft insists that we call a directory a folder. Their thinking is simple, most people are familiar with the filing system found in an office, so why not use that termnology. I flop between the two terms a lot.
The root directory` is the top most directory on your system. It is named with just that path seperator character. Inside that directory you will find other directories and files, each with a unique name. Inside each of those sub-directories, you will find yet more folders, and possible more simple files of some sort. The path to any directory or file on your system is made up of a series of directory names, possible ending in a file name, with the term:path seperator character between each part.
Warning
Linux and Mac systems are case-sensitive. You must use proper case for each name for it to be recognized. The PC could care less. Do yourself a favor, and pay attention ot case no matter what system you use!
Once you have the window looking the way you like, it is time to look at this command line`` and see what it tells us.
Home Directories¶
By default, you are ready to work in your home directory. That place is named with your user account name. Supposedly, you are supposed to store all personal files insife of this directory. However, you probably own the machine you are using, so you can put things in other places. We will pat attention to where we put things as we start working!
Here are the standard locations for the Home Directory:
PC:
C:\Users\account_name
Mac:
/Users/account_name
Linux:
/home/accountname
Looking Around¶
Let’s fire up a simple command and see what is currently in our Home Directory:
PC Systems¶
C:\Users\rblack> dir
COlume in Drive C is OS
Volumw Serial NUmber is B497-D1BE
Directory of C:\Users\rblack
12/07/2018 11:01 AM <DIR> .
12/07/2018 11:01 AM <DIR> ..
11/18/2018 07:19 AM <DIR> Contacts
11/18/2018 07:19 AM <DIR> Documents
11/18/2018 07:19 AM <DIR> Downloads
11/18/2018 07:19 AM <DIR> Pictures
I left off a bunch of other entries.
Notice that we see the time and date when this particular entry was created. We also see a marker (“<DIR>”) indicating that this entry is a directory. Then we see the name of that entry.
Notice also the first two lines in this listing. The “dot” and “dot-dot” names are special on all systems. “dot” is an alias for the current directory (the one you are working in). “dot-dot” is an alias for the directory above this one. These aliases help in forming the path to a file or folder you want to work with in commands.
Linux/Mac Systems¶
rblack@MacTex ~ ->
03:46 PM Fri Jan 18$ ls
Applications Dropbox Public
Desktop Downloads Pictures
VirtualBox VMs acc
Again, I left off some entries from my system.
I have customized my “prompt” so I can see useful information, and give me more room to enter commands. The actual prompt is on two lines. The first shows where I am at the moment (this is called the current working directory). The second line shows the current time and date, which is useful when I capture this display for my notes, for example. The last character on the second line is the actual prompt character on these systems, the dollar character.
Note
From here on out, I will shorten the entire thing to just that single dollas character. I will only show PC examples where things are different.
Detailed Directory Listings¶
Finally, there is a longer form of the ls
command on Mac/Linux. The result
looks more like that seen on the PC and has more useful information. Here is a
partial result from my system:
$ ls -al
total 512
drwxr-xr-x+ 72 rblack staff 2304 Jan 18 14:28 .
drwxr-xr-x 7 root admin 224 Jan 9 20:44 ..
-rw-r--r-- 1 rblack staff 857 Dec 12 05:11 .bash_aliases
-rw------- 1 rblack staff 7036 Jan 18 13:09 .bash_history
-rw-r--r-- 1 rblack staff 164 Nov 10 07:17 .bash_id
-rw-r--r-- 1 rblack staff 696 Aug 8 16:20 .bash_profile
-rw-r--r-- 1 rblack staff 115 Oct 23 18:24 .bashrc
drwx------ 8 rblack staff 256 Dec 11 08:49 .ssh
-rw-r--r-- 1 rblack staff 308 Dec 8 06:49 .vimrc
drwx------@ 3 rblack staff 96 Jan 3 16:35 Applications
drwxr-xr-x@ 4 rblack staff 128 May 21 2018 Books
drwx------+ 7 rblack staff 224 Jan 12 15:29 Desktop
drwx------+ 17 rblack staff 544 Jan 17 08:53 Documents
That pile of characters at the start of each line tells you a lot of important information.
If the first character is a “d, the entry is a directory name. If it is a dash, it is a normal file.
The next 9 characters are permission indicators. These characters are in three groups: “owner”, “group”, and “other”. The “owner” of the entry, the one who created it. The “group” is a special list of other users of this system with permission to access this entry. Many folks create groups when they set up a team for a project. (We will not get into that here.) The owner and group names are shown after this set of permission bits. The “other” permissions define the permissions for any other user of this system. The three characters define “read”, “Write”, and “execute” permissions.
The last items on each line identify the size of the entry in bytes, the date and time when it was created, and the name of the entry.
Phew!
Creating a directory¶
We need a place to store all work for this class. Yes, you can put things anywhere you like, even on that “desktop”, but you really need to start managing your system intelligently. Let’s create a folder for this class and store everything we do for the class in that directory!
$ mkdir cosc2325
We also need to set up a folder for each project we create for this class. Each
project needs a decent name, so let’s call this first project OddSummer
.
(Sounds catchy enough!)
Warning
You absolutely must create separate folders for each project. You should never trash a project to build another one. You will be very happy you can still get to an old project and see how things were done later in your work. In this class, each project you want to have graded must be in an appropriate directory, with a name I specify on the assignment.
$ cd cosc2325
$ mkdir OddSummer
$ cd OddSummer
Changing Directories¶
The cd
command changes your current working directory` to the new
directory you name after this command. On a PC, if you run cd
by itself,
you will see the full path to the current working directory. On
Linux/Mac, typing “cd” as a command will return you to your home
directory.
If you provide a new directory name (or a path) to the new directory you want to work in, the system will move you to that directory. Your prompt will change to show you where your are.
This is where the “dot” and “dot-dot” aliases are helpful.
On my system, I have directories for each class I teach. If I am in the
cosc2325
directory, and want to move to the cosc1337
directory, which
sits beside the current one, I would use this command:
$ cd ../cosc1337
The “dot-dot” says go up one level, then down into cosc1337
. Neat!
Note
You can always use the full path to a new directory, starting with the :term:path seperator` character (not the drive letter though! On a PC, if you want to move to another drive, just type in the drive letter followed by a colon.
Ready to Code?¶
Not quite.
Let’s see if all the needed development tools are ready for use.
Try this (after you go through the tool setup process we discussed in class!) You should see output similar to that shown below. (Your output will surely be longer)
$ g++ --version
Apple LLVM version 10.0.0 (clang-1000.11.45.5)
If you get a response like this:
$ g--
-bash: g--: command not found
This means you either have not installed this tool (there is no such “g–” tool), or the program is installed, but the program file cannot be found in the system PATH`. Go back to the tool installation notes and make sure you followed them correctly.
Continuing:
$ make --version
GNU Make 3.81
$ git --version
git version 2.17.2 (Apple Git-113)