Main

Visual Basic

C++

JavaScript

Flash

Games

Miscellaneous

Tech

About

Expand All - Collapse All

The Basics

Writing your first C++ program shouldn't be too hard, especially if you have programmed in other languages before. Begin by firing up your C++ compiler. I would recomend either a Visual C++ compiler, CodeWarrior, or Borland. Type in the following code. (excluding the line numbers, of course)

1. #include <iostream.h>
2.
3. int main()
4. {
5. cout << "Hello, World!";
6. return 0;
7. }

Output: Hello, World!
Let's go over this program step by step. On line 1, we see #include <iostream.h>. The #include statement is a preprocessor command, and essentially adds the code in "iosteam.h" and places it in the output file. So it's like actually typing out the contents of "iostream.h" right there in your code. "iostream.h" is a basic IO file which allows you to input and output, among other things. Next we see int main() followed by a bracket on lines 3 and 4. This is the opening of the main() funtion. When the program is first executed, the program jumps to main() and runs. main() closes on line 7 with another bracket. After that is cout << "Hello, World!"; on line 5. The cout << command is the output statement. Whatever is in the quotes is printed. You can also put a variable in place of the quotes, and it will print your variable. Of course, you can also do a combonation of them both Finally, we see return 0;. This shows that it is the end of main and and to return the integer 0, which means that the program ran successfully. Be sure to always have this in your programs! When you begin writing other functions, you'll use the return statement to return values from your funtion.

Input and Output
To make a C++ program, you need both input and output. You've already seen the cout << statement, which is for output. This is where you can write text, variables or both to the console. But aside from output, there is input. Input is where the computer waits until the user types something in the console and presses enter. To get input, you must first declare a variable. We will go over this in the next lesson. Once your variable is defined, you can use the cin >> statement. Here's an example of how you can use it:

1. #include <iostream.h>
2.
3. int main()
4. {
5. char name[20];
6.
7. cout << "Please enter your name: ";
8. cin >> name;
9. cout << ". Hello, " << name;
10. return 0;
11. }

Output: Please enter your name: SqueakMac. Hello, SqueakMac

Data Types and Variables
C++ has many different kinds of variable types (or data types). There are strings, characters, integers, doubles, floats, boolean, and more. The most common you will use is an integer. The decleration of it is "int". So to declare an integer, you would write int <varname>. For example...

int myInt = 5;
string myString = "Hello!";
Here's a list of some common data types you might use in your program:

Type Declaration Values
Byte byte -255 to 255
Short short -32,768 to 32,768
Integer int about -2 bil to 2 bil
Long long about -9,223,372 tril to 9,223,372 tril
Character char A single character
String string A string (uses string.h)
Boolean bool 'true' or 'false'

A common feature you may use regarding variables is an array. An array is essentially multiple values for one variable. You access an array by using the [ and ] brackets. You can either manually define the number of values for the variable, or let another variable control that. To declare a variable you would write:

type var [size];
Here are some example declarations

int myArray [3] = {2,4,8};
int myArray2 [] = {16,32,64,128,256};
int myArray3 [myInt];
As you can see, you could either define the number of elements or not. You can also define the list of values when you first define the array. Below is how to access certain array values.

myArray[0] = 2;
myArray[1] = 4;
myArray[2] = 8;

Flow Control: If Statements
If statements control data flow in C++, as well all other programming languages. To proccess the input from the user and determine what to do, you would need an If statement. To use one, you simply type 'if (expression)' and brackets, placing the code you want between. You can also add the else statement, in which you would add after the closing bracket. Here's an example:

1. #include <iostream.h>
2.
3. int main()
4. {
5. int num;
6.
7. cout << "Enter a number 1-5: ";
8. cin >> num;
9. if (num > 0 && num < 6)
10. {
11. cout << ". Good choice!";
12. }
13. else
14. {
15. cout << ". Not valid!";
16. }
17. return 0;
18. }

Output: Enter a number 1-5: 4. Good Choice!


 

© 2004 by Lam Ri Hui. All rights reserved.

1