Introduction to Using Visual C++

It is a requirement of every programming course to have as its first program one which prints "Hello World" on the screen. So, to abide by this tradition that is what we will do today.

Visual C++ must be launched from the start menu. It is not available on the network. You should launch Microsoft Visual C++ 5.0 which is in the folder of the same name.

(Click on pictures for a closeup)

When it launches you will be taken to a version of the help screen. There is extensive help for this program, but not all of it is online yet.

To create a new project go to "File" and "New." You will be presented with this dialog. Enter hello for the project name, click on Win32 Console Application for type and change the location to your H drive. Then click on OK.

A project is like a folder, and by itself doesn’t contain any files. You must add a new file by going to "File" and "New" again. This time the "Files" tab should be clicked and you should choose to add a C++ source file. Call this file "main.cpp" and click OK.
You should then type in the program shown here on the right. After typing it, go to the "Build" menu and select "Build hello.exe." Errors will appear at the bottom of the screen. In this case, there were none.

Before we understand what each line of code does, let’s take a look at the output. To do this chose "Execute Hello.exe" from the "Build Menu." You should see a DOS window that says "Hello World!"

Let’s return to the program. Here is a brief description of what each line does.

#include <iostream.h> This line tells the compiler to include information on input and output (io) to the screen
int main(void) This declares a function called "main" which is by default the first function that is executed. All code must be included in functions. The "int" before main indicates that the function will return an integer value. The "void" inside the parentheses shows that there are no parameters.
{} The curly braces mark the beginning and end of the function.
cout<<"Hello World!<<endl; The "cout" (pronounced cee-out) command sends output to the screen. This must be followed by the insertion operator (<<). "Hello World" is the text to be output, and endl adds a line feed. Note the semicolon at the end of the line. All commands must end with a semicolon.
//This is the output part This is a comment and is ignored by the compiler. It is a good idea (and mandatory in this class) to make liberal use of comments.
return 0; Since main is a function that returns an integer, we must make sure it does so. This is easily done by just returning the value 0.


If you have time, you can change the program to the one on the bottom of page 31.

void Hello(void)
    cout<<"Hello World"<<endl;
int main (void)
    return 0;

Which version requires less code to print the same thing twice?

(Note that the voids inside the parentheses in the functions are optional.)

Back to Home Page House3.wmf (25540 bytes)