Sunday, September 29, 2013

C++ Tutorial #3: Getting Comfortable With Command Prompt

Note: Folders are going to become a frequent topic of conversation.  I highly recommend you create a special folder dedicated to your programming.  I have a one under "C:" entitled "ProgrammingProjects" (so it's C:\ProgrammingProjects).  It's just an easier way to keep things organized.

Okay, so you're feeling pretty accomplished right about now.  You have a basic understanding of how programming works, you've got Dev-C++ and a compiler, and at the end of the last post I said, "You're ready to learn how to program."

But I forgot one little thing.

I didn't think about it because it's included in computers already—no software to download.  But you do have to become familiar with a command line.

What the hell is a command line?  Here's a pretty drawn-out but necessary and non-skimmable explanation: an interface is a method by which a user (you) communicate with digital technology (your computer).  If you use Windows, OSX, or a similar operating system, you are using a graphical-user interface, or GUI.  This means that you and the computer communicate via graphics—you know, stuff you can see.  Menus, buttons, you use a mouse in addition to a keyboard, etc.  GUIs were developed because they're very intuitive and easy on the eyes, but computers haven't always used them.

Before the age of the GUI, there was something called a command line interface, or command line.  For example, if you remember Disk Operating System (DOS) (this goes way back to the late 80s/early 90s), that was a command line.  It's basically a black screen with white (or alien green if you were lucky) letters.  No mouse was used, everything was in plain text format, and you got the computer to do what you wanted it to do not by clicking buttons and scrolling through menus but typing out rather cryptic-looking commands.

Source: Wikipedia

This is what a command line looks like.  Yes, kind of creepy, but you'll need it to test-run your programs once they're written.  While DOS is no longer used, modern computers still have command line programs that will work the same way.

Unix-like operating systems (such as Linux and Mac OSX) have their own command lines that operate their own way with their own commands.  I have minimal experience programming with the Unix command line, and I actually have never owned any Unix-based OSes, so I won't be covering them.  If you ever take a programming class at a college or university, it's almost certain that you will use a Unix-based system.  However, the main point of this tutorial is to get you familiar with the C++ language, not any kind of OS, so I'm going to teach it based on the command line I'm most comfortable with.  If you use a different OS, then I'm sure you can find a guide to basic commands online that are equivalent to the ones I demonstrate here.  I don't want to try to explain them because, honestly, I don't understand Unix well enough and I don't want to give incorrect instructions.

Using the Windows Command Line

Modern versions of Windows (and by "modern" I mean in the last 10 years) use a program called Command Prompt.  It's a command line, just like DOS, but it exists as a smaller program within Windows (which is a GUI).  To get to Command Prompt, either search for it on your computer or (if you like using the "Run..." button), run cmd.exe.

A really scary-looking box like this one should open up.  (Note: I blotted out the name because I'm currently using someone else's computer and would like to respect their privacy.)


Every time you open up Command Prompt, you'll have that little copyright thing at the top.  For the sake of explaining this, we're going to pretend that the user on this computer (the name that I blotted out) is "LadyPakenham".  The weird C:\Users\LadyPakenham> thing is called a prompt.  You notice how it's got the little cursor thing after the "greater than" (>) sign?  That cursor is where you type the commands that tell the computer what to do.

Get it?  It's prompting you for commands.

Using Commands: cd.. and cd

Okay, simple enough.  But what are you supposed to type into this thing anyway?  What are you going to want your computer to do when you're programming?

Well, when you go to program, the first thing you're going to want to do is open Command Prompt and navigate to the folder where you're keeping your programming projects.  In this case, my special programming folder is C:\Programming_Projects.  When I open up Command Prompt, its default setting is to take me to C:\Users\LadyPakenham.  How do I navigate from one folder to another?

There are two commands you can use to do this: cd.. and cd.  In both cases, "cd" means "change directory" (remember, a directory is a folder).  The difference between the two of them?  cd.. (with the two periods) means to move out of a directory, where cd means to move into a directory.  Let me show you:

1) cd.. moves out of a directory

Let's say I want to get from C:\Users\LadyPakenham to just plain old C:.  I have to move out of the directories LadyPakenham and User, one at a time.  In this example, the prompt is going to be in bold, while the commands I type will not.  I'll type a command and then press Enter, producing another prompt on another line.

C:\Users\LadyPakenham>cd..
C:\Users>cd..
C:\>

You see what I did there?  Every time I typed cd.. it removed one directory from the prompt.  It's equivalent to going through folders in Windows Explorer and hitting the "Back" button.

2) cd moves into a directory

Now, let's say I'm in C: and I want to get back to C:\Users\LadyPakenham.  To enter into a directory (which is the same as double-clicking a folder you want to get into), you type the command cd (without periods), hit the Spacebar, and then type the name of the directory you want to enter, like so:

C:\>cd Users
C:\Users>cd LadyPakenham
C:\Users\LadyPakenham>

3) How to get to your programming folder

I personally recommend that when you make a folder for your programming projects, it be something simple, like C:\ProgrammingProjects.  You know, just so it's easy to get to when you use Command Prompt.  Let's say that I did that, and wanted to get from C:\Users\LadyPakenham to my programming folder.  This is how I would use both "change directory" commands to do so:

C:\Users\LadyPakenham>cd..
C:\Users>cd..
C:\>cd ProgrammingProjects
C:\ProgrammingProjects>

Ta-da!  I'm in a good place to test-run my programs.

4) What if I don't know where my desired directory is?

It's a truth of life: folders have subfolders, and sometimes you can't remember which folder has the subfolder you're looking for.  In this case, you can use a third command, called dir, to view the contents of a directory.  Look at the picture I got from Wikipedia again:


Look at the line directly under "FreeCom version blah blah blah".  You see the prompt C:\> with the command dir.  It displays a lot of stuff.  The first line that appears upon typing the command and pressing Enter tells you the name of the directory (in this case, C:).  The second line tells you the serial number (since this directory just happens to be the entire hard drive).  Finally, it tells you everything that's in the directory of C:.

In the list that follows, the leftmost column has the names of all files and sub-directories.  The second column has the name of file extensions.  Notice how FDOS is a sub-directory while AUTOEXEC.BAT is a file.  The file size of all files is given, then the date and time of modification.  If, in the file size column, <DIR> is listed, that means the given item is a sub-directory and not a file.

So, if the user wanted to get into the FDOS folder, he or she could just type cd FDOS and the prompt would change to C:\FDOS>.

Why am I telling you this?  If you can't remember where your programming folder is, you can always use dir to see what's in the directory you're currently in.  That way, you can keep searching directories until you find it.  (A lot of headache can be eliminated by just keeping the folder in an easy-to-remember place, like C:\ProgrammingProjects, but dir is still a helpful command to know.)

5) Finally, how to run a file

Running files is easy.  Just type the file name and extension.  Let's say you're in your designated programming folder in Command Prompt, and you wrote and compiled a program called Hello.exe.  Literally, all you have to do is type Hello.exe.

An important thing to note: make it a habit to type the file extension (the .exe part in this case) as well as the name.  Why?  Well, remember how I told you in Tutorial #1 that you have two files for every program you write (the source code version and the machine code version)?  Well, both of them are going to have the same name, but different extensions.  In this case, the source code file (written in C++) would be named Hello.cpp, and the executable file (translated into machine code) would be Hello.exe.  If you just type Hello, without any extension, you might not get the program you want.  Your computer may open one or the other, but not both, and closing out of the wrong program and opening the one you want is a time-waster.  Be specific when running files.


Okay, I think that really is all the preliminary work this time.  I know it's frustrating when you're first getting started—you just want to program already!  But getting your computer set up and becoming familiar with the tools you need will save you a lot of grief in the long run.  Trust me, it's tough to program when you know the environment you're in!  Don't make things more difficult for yourself than they need to be.

No comments:

Post a Comment