Writing CodeWarrior C++ Applications

Bowdoin College Department of Computer Science
September 7, 1998



CodeWarrior
is a software environment that supports Pascal, C, C++, Java, and Assembly language programming. This document describes how to use the current version of CodeWarrior, called CodeWarrior Professional, to write and run C++ application programs. For using CodeWarrior to develop programs in other languages, readers should consult other documentation.


Files and Folders Used by CodeWarrior

CodeWarrior allows large programs to be written as one or more files. When you first start a program in CodeWarrior there are three different kinds of files to keep in mind:

These files are all kept in a "project folder," which has the same name as the project file (except for the suffix ".µ"). For example, "Lab1" might be the name of your project folder for lab 1, while "Lab1.µ" is the name of the corresponding project file. The project folder is a regular Macintosh folder that holds all the required files for a particular programming project.

When you write a C++ program, you will need to first create a project and then create one or more files that contain the text of your program. These files are all kept in the project folder.


Part 1 - Find CodeWarrior and Start It Up

CodeWarrior is on the hard drive of the Macs in the Adams 208 lab and other Mac labs. Look in the CodeWarrior folder on the hard drive and double click on the CodeWarrior IDE 3.0 icon. It looks like this:

(Once you have a project defined and saved, you can just double click on that project file (with the .µ suffix) to restart CodeWarrior; this step is only necessary the first time you use CodeWarrior).

When CodeWarrior starts, the screen should have a menu bar that looks like this:

Optionally, you may open the "toolbar" by selecting Show floating toolbar under the Window/Toolbar menu. The toolbar contains shortcuts for many of the menu items, and it looks like this:

You might want to look at some of the items available on the pulldown menus, just to familiarize yourself with them.


Part 2 - Creating a New C++ Project

In the menu bar, select the New Project... option under the File menu. This creates a dialog box like this:

Now click once on the right-pointing triangle next to MacOS, then the triangle next to C/C++, and then the ANSI C++ Console PPC entry, as shown below. Now select the OK button:

At this point, you'll get a dialog box similar to the following:

Now use the folder names at the top of the window in the figure above to select your diskette (the one shown above is named "untitled"), or else the Mac Desktop, where you want your work to be saved. If you save your project on the desktop, care must be taken to move it to your Arctos account at the end of the session, so that you can retrieve it in the future.

Now name the project (the one shown above is named Lab1.µ, and the character µ is formed by typing option-M on the keyboard). Save the project by clicking the Save button. After you've done this, a window like the one below will appear:

The figure above also shows that the newly created project has three parts, called Sources, ANSI Libraries, and Mac Libraries. If you click on the triangles on the left, you will see that the C++ source program HelloWorld.cp and various libraries that have been prepackaged inside this project.

Just to make sure you understand the difference between a project folder, a project file, and a C++ program, take a little detour to look at the contents of your floppy disk (or simply the folder on your desktop). It should contain a project folder called "Lab1" and double-clicking on that folder should show the following files:


Part 3 - Create Your Own Program for This Project

You now need to create a separate file that contains the program for your project. Under the File menu, select the New option. This will open a new file with the heading untitled. Use the Save As command under the File menu to give the program file a name and store it inside the Lab1 folder on your diskette. The dialog box is illustrated below.

You must choose a program name that ends with the suffix ".cp" or ".cpp " (like "CountDown.cpp" in the picture shown above).

Now we must add the program file we just created to the project Lab1.µ, and remove the HelloWorld.cp program that is currently there as a placeholder.

To add your program file, select HelloWorld.cp in the project window, and then select the Add Files... command in the Project menu. A dialog box like the one shown below will appear, and the name of the program CountDown.cpp will appear in the list. Clicking Add and then Done will add this program file to the project.

  • Now select HelloWorld.cp and click Remove Selected Items in the Project menu to delete this sample program that came with the project originally. The resulting project window should now look like the following:
  • Typing the Program

    Go back to the program window by double clicking on the file name CountDown.cpp. You are now ready to type a program using the editor, such as the following simple C++ program. (While typing is better for the soul, you may choose to copy and paste it directly off this Web page into your empty text window if you are in a hurry.)

    #include <iostream.h>
    
    main() 
    {
       int n;
       try {
            cout << "Enter an integer:";
            cin >> n;                     // read an input integer
            for (int i=n; i>0; i--)
                cout << i << "\n";
            cout << "Welcome to CodeWarrior C++!" ;
            return 0;
       } 
       catch (...) {
            cout << "Exception occurred";
       }
    }

    If this is your first program, be very careful to copy all the punctuation, tabs, and spacing exactly as they are shown.

    As you type, notice that some program elements appear in different colors (comments appear in red and reserved words appear in blue); this is often helpful in finding and correcting syntax errors, and generally makes the program more readable.

    It is important to know that C++ is a case-sensitive language, so that capital letters are different from their lowercase counterparts. Ror example, the variable names MYNAME, MyName, and myname have distinct meanings in a program if they all appear. Reserved words in C++ (the words that appeared in blue in the program you just typed) are always written in lowercase. Comments can have any mixture of upper and lowercase characters, and begin with the characters //.

    You should periodically use the Save command under the File menu while you are typing a program. This precaution gives you added insurance against unexpected (though infrequent) system failures. When you are finished, save the complete program.


    Part 4 - Compile and Run the Program

    Once you have completed and saved your program, it needs to be compiled (checked for syntactic errors) along with the other modules in the project. This is done by selecting the Make option in the Project menu. If your program contains any syntactic errors (errors in its grammatical structure), a window will open and identify the nature and location of each error in your program.

    To run the program, select the Run command under the Project menu. (Note that a speedy way to make and run a program is to use the cloverleaf-m and cloverleaf-r commands for Make and Run, respectively.) This will open a window called ANSI C++ Console PPC.out, which is used both for typing input to the program and for displaying output from the program. Notice that this window can be resized in the usual manner (use the mouse to grab and drag the lower righthand corner).

    For this particular example, you need to type an integer into the ANSI C++ Console PPC.out window in order for the program to complete. For instance, if you type the integer 10, the window will look like the following when the program run is completed:


    Part 5 - Print the Program

    To print, make sure that the window you want to print is at the front of the screen. This will usually be the program window, but sometimes you might want to print an output window. Then go to the File menu and select Print. This will print a hard copy of your program text on the printer at the back of the lab in 208 Adams (or whatever other printer has been selected in the Chooser).

    Before you print, you might want to check the Chooser (from the apple menu) to see that the printer is set correctly to print in the lab you're working in. In many labs, it's also a good idea to make sure that the printer itself is turned on before you try to print.

    Some printers will allow you to change the print format to save some paper. Before you print, go to the File menu, and select Page Setup. The 2up layout will print your program using half as much paper (but in a smaller font). You can also use the Page Setup menu to print in landscape orientation (rotated 90 degrees), which can be helpful if your program has very long lines.


    Part 6 - Leave CodeWarrior and Turn In Your Program

    To end your CodeWarrior session, select Quit under the File menu. Be sure that the latest changes in your program are saved before quitting.

    If you've been working on the desktop, don't forget to copy your files onto your own floppy disk or into your Arctos account.

    To submit your program electronically, open the appropriate course folder for your class and drag your program file to the Drop Box folder that appears there. To find the course folder, look in the Courseware folder on the harddrive. (Opening one of the course folders causes a new icon to appear on your desktop.)