Intro to C++

Posted by admin on Feb 14, 2008 in Misc., Old News, Programming |


This is an article I wrote that won First place for the “Instructions: Soft Copy” category from the Society for Technical Communication and UNC Charlotte. Hit the jump to check it out.

Complete Beginner’s Guide to C and C++ Programming in Windows

Introduction

Step 1: The materials

Step 2: The Install

Step 3: Create a Working Directory

Step 4: Open Quincy 2005

Step 5: Create The C++ File in Quincy 2005

Step 6: The Basics

Step 7: Our Code

Step 8: Checking Our Code

Step 9: Using Your Program

Step 10: “The Works”

Step 11: Finding your Program

Step 12: More Information

Troubleshooting

Introduction:

When I first leaned how to program, I took a class at college. (Actually, I was required to take the class, and I hated every minute of it.) The main reason I hated that programming class was that everyone who teaches stuff expect you to learn theory and all this other stuff first. I’m not saying that stuff isn’t important, however, I believe that seeing something in action first, then getting an explanation helps you learn better.

After the class was over, I realized how helpful a skill programming can be. I’ve written many programs for my own personal use and projects. A good example is a simple calculator program. If you need to do the same complicated formula a bunch of times, its easier and quicker to write a program to do it for you. Or a simple database application to store information for use later.

This article is going to help you write a simple C++ program. Basically I’ll go step by step explaining in general what I am doing and why. The program goes a little further than the simple “Hello World” program. This program is a “Hello <your name>” program. Its basically just shows the functions of simple input and output.

Back to top.

Step 1: The materials

Well, there’s not any “material” involved really, unless you count electrons. By that I mean you only need software. (Well, and a computer to run it on.)

We’ll be using a GREAT little IDE (Integrated Development Environment) for C and C++ called Quincy 2005. It’s simple, it’s pretty, and has some graphical libraries for making all those nice pretty (what I call “fluffy”) windows you are used to called GUIs (Graphical User Interface.) The GUI stuff is a little complicated for a beginner, so that’ll come later. You’ll actually have to learn how exactly this stuff works before attempting to make a GUI.

Back to top.

Step 2: The Install

Get Quincy and install it. Not hard to do, just follow the on screen directions. Sadly, this program is only for windows, however the libraries it uses are cross platform. So most of the programs you write in Quincy could be compiled on a Linux machine using GCC or G++.

Back to top.

Step 3: Create a Working Directory

This will be our working folder. Its where we’ll same the entire project so everything for the program you write will go into that folder.

First, open “My Computer” then go into the C drive and create a folder called “CPP”.

Working Folder

Back to top.

Step 4: Open Quincy 2005

Now start up Quincy 2005 (Start–>All Programs –> Quincy 2005 and click on the program with the cat’s face).

Back to top.

Step 5: Create The C++ File in Quincy 2005

When you start up the program, It should be empty. Go to “File”–> “New” and click on “C++ Source File” and click OK. Go ahead and save this file into your “CPP” folder as “Hello.cpp”

Back to top.

Step 6: The Basics:

Below is the basic template of C++ code. I’ll explain it as I go:

First off, here is the basic structure of any C or C++ program.

#include<include file>
int main()
{
PROGRAM CODE
}

Explanation:

#include<include file>

The include statement is required for commands used in the actual program code. There are many different include files that can be used, and each of them allows you to do certain things.

Here are a couple examples of include files and what they are for:

iostream.h allows you do simple input from the keyboard and simple output to the screen.

math.h allows you to do math stuff. You don’t need this for simple things like addition, subtraction, multiplication and division, however, certain things, like sin, cos, square root, etc. are stored there.

fstream.h allows you to save and open files outside of your program.

int main()
{
}

This part is the main “function”. It’s where all the actual code goes. Other “functions” can exist, but that’s more advanced for this tutorial. To learn more on this, refer to the links in the last step.

Back to top.

Step 7: Our Code

#include<iostream.h>			//1
char name[25];			//2
int main()				   //3
{
cout<<"What is your first name?\n";	//4
cin>>name;				//5
cout<<"Hello "<<name<<"\n";	//6
system("PAUSE");			    //7
}

Copy the above code, and paste it into your “Hello.cpp” file, or type it out by hand. Make sure not to forget the semicolons at the ends of the lines. Also, if you copy and paste, make sure the quotations are pasted correctly. If not, delete the symbol that replaced them and type them in. Click the button on the button bar above with the little picture of the guy running or hit the F9 button on your keyboard. This will “build” your program, meaning your code is compiled and turned into a program (an executable or .exe file). When it asks to build Hello.cpp, click OK.

Back to top.

Step 8: Checking Our Code

Two boxes should pop up. For now, ignore the black window. The light colored window shows you the build process. If there are any errors or warnings, they should be listed in that box. At the end of the text it should say “successful build”.

Build Log

Our program has a problem in it that causes a warning. We are using an “antiquated header file” This is nothing to worry about. It is caused by the fact that we used #include<iostream.h> and the “.h” isn’t required, however, it simplifies things for the beginner.

If you look at the third line form the bottom in that text box, it shows what kind of problem it is and where the problem occurs. Double click that warning and it will open up a file that explains what the problem is:

#warning This file includes at least one deprecated or antiquated header. \
Please consider using one of the 32 headers found in section 17.4.1.2 of the \
C++ standard. Examples include substituting the <X> header for the <X.h> \
header for C++ includes, or <iostream> instead of the deprecated header \
<iostream.h>. To disable this warning use -Wno-deprecated.

Back to top.

Step 9: Using Your Program

The second window that pops up is a black console window. That’s your program! Type your name and hit enter. Cool huh?

Running your program

Well, now I’ll explain how it works:

Back to top.

Step 10: “The Works”

Line 1:

#include<iostream.h>

As you can see here, we are going to only use iostream.h. This is going to allow us to do simple input output (I/O) operations. This caused the warning I mentioned before. If we left off the “.h” it would give us another error though. We would also have to type “using namespace std” before the int main(). Like I said, it complicated stuff for the beginner. Since the program will compile with the warning, it

is not really a problem anyway.

Line 2:

char name[25];

declares a variable. In order to use variables in C++, there must be space in the memory of the computer to store them. In this case, we have 25 characters. Meaning the compiler will reserve that much memory for us to use the variable “name”.

“char” declares the type of variable. Char stands for “character”. This tells the compiler how to treat the variable.

“name” is the name of the variable. You can name this pretty much anything you want. There are a few limitations though. Don’t use variable names that start with an underscore and stuff like that because that’s how the makers of C++ named variables they used in include files and stuff, and you don’t want your program to become confused.

“[25]” is what makes this variable an array. You can change the number in the brackets to change the number of characters in the string.

Line 3:

int main(){

Again, the “main” function.

Line 4:

cout<<"What is your first name?\n";

“cout<<” is the operand that says “print to the screen”. This can be used in a couple different ways. You can print direct text, or variables. If you want direct text, use quotation marks. “”

The “\n” at the end of the text is a command for “go to the next line”. It won’t be printed on the screen. There are several command you can use in this way to do different things like tabs, center text, etc.

Line 5:

cin>>name;

“cin>>” This is the operand that says “get data from keyboard”. It will wait for you to type something and press the Enter key.

It has to store the information from the keyboard somewhere right? Well, this stores the information in the variable after the >> marks. So in this case, its going to store what you type into the character array called “name”.

Line 6:

cout<<"Hello "<<name<<"\n";

Here’s the “cout” again. We know what this does right? Well, this time its a bit different. It prints the phrase “Hello” directly to the screen. The added << shows that its going to print something else as well. In this case, it will print what is stored in the variable “name”. Notice that the variable has no quotation marks around it. We don’t want it to print the WORD “name” we want the data stored in the variable called name. Again, we have it skip to the next line after printing that variable.

Line 7:

system("PAUSE");

This is used to pause the program until you press enter. Without the system pause, the program would close that window immediately after printing your name. Quincy adds its own system pause after the program, but ONLY when running form inside Quincy. When you find your program in the next step, Quincy’s pause won’t be in it.

Back to top.

Step 11: Finding your Program

Navigate your way to your CPP folder in your C drive that we made in the beginning. Once there, you should see the program called “Hello.exe” Open the program with a doubleclick. Enter your name again. Now you can copy that wherever you want. Keep it and show you mom :)

Finding your program

Back to top.

Step 12: More Information

Now that you’ve written a simple program. And understand the ins and outs of using Quincy 2005, all you need is a better understanding of the C++ language. There are some GREAT websites and tutorials out there. The ones I list here are certainly not the only ones out there. A quick Google search will help you find even more. If anyone knows of any, feel free to list them for the rest of us. Here are a few to get you started:

http://www.cplusplus.com/

http://cprogramming.com/

A good book never hurts either. The ones I’ve used in my classes are:

C++ How to Program by Deitel & Deitel

Concise Prelude to Programming: Concepts and Design by Stewart Venit

C++ Basics by Todd Knowlton

Back to top.

Troubleshooting

Compiling the code:

If you cannot compile the code, Then you have not saved it yet. Make sure to click “Save As” from the “File” menu and save it to C:\CPP with the name “Hello.cpp.”

Errors while Compiling:

If you are having errors compiling the code from above, make sure when you copied it into your “Hello.cpp” file, that everything printed right. Sometimes, the font doesn’t translate well for the webpage to the editor. Certain symbols don’t print correctly, such as the parenthesis and the semicolons.

To make sure that the code is right, attached to this page (at the bottom) is the original code used for this article. This code is tested and proven to compile. If you have any problems with this code, please leave a comment on this article.

Finding The Program File:

If you cannot find the program file in your CPP folder, then you must have saved it to a different folder. Click “Save As” from the “File” menu. this automatically opens the “Save As” dialog box into the last location you saved. Find out what directory you are in and go there to find your program file.

Back to top.

Copyright © 2007-2010 The Playground All rights reserved.
Desk Mess Mirrored v1.4.6 theme from BuyNowShop.com.