CS 150 - Intro to Programming - Spring 2002
Lab Exercise 1

Topics:


Code Warrior Introduction

All of your laboratory and programming project assignments will be done using the C++ programming language. We will use Metrowerks Code Warrior 6.0 for writing and developing C++ programs. Code Warrior is an Integrated Development Environment (IDE) designed for developing professional software for a variety of computer platforms. The Code Warrior development environment enables one to program in several different programming languages (C, C++, Pascal, Java), we will restrict our use to the C++ features. In this lab you will learn the basics of C++ program development using The Code Warrior IDE. When developing a C++ program, you will encounter 4 recuring tasks:

The focus of this lab is to become familiar with these steps.

Part 1 - Create a new Code Warrior project.

A complete C++ program consists of several files: source files, header files, and libraries. In Code Warrior, a project is required to coordinate these various components. Thus, to create a program we must begin by creating a project file. As you will learn, there is often more than one way of accomplishing the same task in Code Warrior. For now, you will learn a way - later as you learn and experiment more with the development environment you will find other ways.

  1. Launch Code Warrior. Code Warrior will start and open the IDE window.

  2. Create a new project.

    1. Pull down the File menu to New...

    2. At this point we need to specify which type of project we would like to create. Built in libraries to be included in the project are based on the type of project chosen. Built in libraries contain pre-existing code that typically comes as part of a standard C++ compiler. Code Warrior has predefined "project stationery" in order to help us use the correct libraries depending on the type of computer and the language we are using.

      pc: Choose Win32 C++ Stationery.
      mac: Choose Mac OS C++ Stationery.

    3. Use the Set button to create a folder which will contain your project. When you click the Set button, the SaveAs dialog box appears. Navigate to:
      pc: c:\student
      mac: Desktop->student
      Enter - yourLastNameLab1 in the "File name" text box. Then click save.

      Note: each of our labs will be completely contained in its own folder. We will use the following convention when naming our labs and the folders that contain the files for the lab.

      1. Enter an appropriate name using the following convention: yourLastNameLab1 (no spaces). For example, Robin Smith's folder for this exercise would be named something like smithLab1. You will create a folder for each Lab that you do. For example: smithLab2, smithLab3,..., smithLab14.

      2. You should save your files on your machine's hard drive and then copy them to your floppy at the end of each lab.

    4. When you are done specifying the project, click OK.

    5. The New Project dialog box appears

      pc: Expand the "Win32 Console App" box, then select C++ Console App - Click OK.
      mac:Expand the "Standard Counsole" box - then select PPC - finally select Std C++ Counsole PPC - Click ok.

      In response to the above dialog, Code Warrior creates a new folder (unless you specified otherwise) and opens a project window. The project window lists the sets of libraries required in order to run a C++ program that conforms to the ANSI (American National Standards Institute) C++ standards on both the Mac and Windows platforms.

    6. Notice the libraries included in the "Files" section of the project window - this indicates they will be used in the compilation and execution of the project.

  3. A default C++ program is automatically created by Code Warrior. To view this default program, open the Source folder (click on the square to the left of the word "Source"). The name of the program is "hello.cpp", and you can open it by double-clicking it. Notice the different colors. Comments appear in red, keywords appear in blue, and other statements appear in black. Also notice the various little buttons in the program window. Some of these will come in handy while writing larger programs. For instance, pressing in the box at the bottom (marked "Line") will ask you for a line number in your program to go to. You should investigate the features of Code Warrior yourself. Don't wait to be directed to each one. Instead spend some time examining the menu options and to find out more using the Help facilities.

  4. Run the program contained in the project, as described below.

    1. From the Project menu select Run. After compiling and linking, a window will appear containing the results of the execution of the program contained in the file "hello.cpp".

    2. Close the "run window" to return to the project (you do not need to save the output). You can run the program again by selecting Run from the Project menu again.

  5. Quit Code Warrior.

    1. From the Project menu - choose: Remove Object Code
    2. push the All Targets button - this removes temp files from the hard drive.
    3. From the File menu - choose: Exit.

Part 2 - Changing (editing) an existing program.

The Code Warrior project you created in Part 1 uses the default source code file provided with the project stationery. We will now learn how to create a new C++ program other than the default "hello.cpp". The first approach we will take is to open the project from Part 1 and modify the default program to suit our needs.

  1. Start Code Warrior if necessary. From the File menu - choose: Open or Open recent. Locate and open your project (yourLastNameLab1.mcp).

  2. Open the Sources folder in the project window by clicking on the "+" symbol.

  3. Open the file named "hello.cpp" from inside the project window (double click the file icon or File->Open).

  4. From the File menu - choose Save As... This will allow us to save the current file with a different name.
    In essence preserving the origional and editing a copy.

  5. In the save as dialog box, navigate to your project folder. Change "hello.cpp" to yourLastNameLab1Part2.cpp in the "File name:" box and push the Save button. (Note: the file hello.cpp still exists)

  6. Click in the project window and observe the name change in the Source folder. This indicates that the file yourLastNameLab1Part2.cpp will now be used as the source code file next time the project is run.

    Note: The .cpp file name extension is important. It enables the IDE to recognize the file as a C++ program. This is a common source of error - always be sure your source files have this extension.

  7. Return to the source program (yourLastNameLab1Part2.cpp) and modify it so that it prints your name, your major, and your email address (each on a separate line) on the screen. Refer to page 22 of our textbook for help in creating the necessary code.

  8. Save the file (select Save from the File menu).

  9. From the Project menu - choose: Run. If your program has any syntax errors or linking errors, they will be reported in a message window that pops up. Correct your program (if necessary), save it, and run it again.

  10. Verify the correctness of the output. If you discover an error in the display, correct the program, save it and run it again.

  11. Follow the steps at the end of Part 1 to quit your program.

  12. Examine the contents of your project folder. The file "hello.cpp" is still there although it is no longer utilized by the project. Delete the file hello.cpp.

  13. Close the project window and exit Code Warrior before going to the next step.

Part 3 - Creating a new C++ program using Code Warrior.

The Code Warrior project you created in Part 1 and Part 2 uses the default source code file provided with the project stationery. We will now learn how to create a new C++ source file and add it to our project so it becomes the default source code file.

  1. Another way to launch a Code Warrior Project is to double click on a file name with a .mcp file extension. Open the yourLastNameLab1 project folder on the hard drive, find the yourLastNameLab1.mcp file, then double-click on this file name - Code Warrior will start and open that project.

    Note: If you are working on a PC and the file name extensions are not appearing, you may wish to change the view settings. One way to do this is to open My Computer and select Options from the View menu. Next, select the View tab and make sure the box "Hide file extensions for known file types" is not checked.

  2. Create a new source file and update the project.

    In Part 2 the program was modified by making changes to the default source file (originally named "hello.cpp"). It is also possible to create new source files from scratch and to add them to the project. Source files can also be removed from the project. We will do that next.

    1. From the File Menu - choose: New. This will bring up the New dialog box - click on the File tab - then choose Text File and enter a file name (yourLastNameLab1Part3.cpp). This will give you an editor window in which to write your program. The name of this new window is yourLastNameLab1Part3.cpp.

      Type the following program in the yourLastNameLab1Part3.cpp window. Copy everything exactly as it is shown here, even the case (upper or lower) of each letter, except substitute your own information in the comment section at the top of the program.

       
        //filename: yourLastNameLab1Part3.cpp
        //author: Your Name
        //date last modified: Today's date
         
        //This program computes the area of a circle after obtaining the
        //radius from the user.
               
        #include <iostream>
        
        using namespace std;
              
        int main()
          {
          int radius = 0;
                      
          cout << "Enter radius of the circle: ";
          cin >> radius;
                      
          double area = 3.14159 * radius * radius;
                      
          cout << endl 
               << "The area of a circle of radius " << radius
               << " inches is " << area << " sq. inches." << endl;
                           
          return 0;
          }
    2. Save the file (File->Save).

    3. Return to the project window. Now we will change the specification of the project by swapping out the file yourLastNameLab1Part2.cpp with the file yourLastNameLab1Part3.cpp. Recall that the purpose of the project is to coordinate source, header and library files. This step is just changing which files the project will attempt to coordinate.

    4. SINGLE-CLICK (don't double click) on the Source folder icon to select it. Then pull down the Project menu and choose Add Files...

    5. Use the file navigation dialog box and select (SINGLE-click) the file yourLastNameLab1Part3.cpp. Then click Add, to add it to the project.

    6. Verify that all targets are selected and then click OK.

    7. Now remove the file yourLastNameLab1Part2.cpp from the project by clicking once on yourLastNameLab1Part2.cpp in the source folder of the project window and selecting "delete" from the edit menu - click OK on the warning message .

    8. Open the Sources folder in the project window and double-click on yourLastNameLab1Part3.cpp. The file should open and the source code appear.

      Important: You will eventually have several files with similar names. By accessing the file through the project window, you ensure that you're working on the correct file.

  3. Select Run from the Project menu.

  4. If error messages appear, return to the source file window. Correct any typing errors you made, save the file and run the program again.

  5. Once the program runs, Input integers for the area. Press Enter to continue.

  6. Repeat the Run/Continue sequence to try out other inputs.

  7. Follow the steps at the end of Part 1 to remove unnecessary files, then quit Code Warrior.

Part 4 - Submitting a lab.

This part describes the procedure for turning in your work. It is important that everyone understands and adheres to a consistent procedure.

  1. At this point, we have completed our work and want to turn in the lab. Recall that a C++ program must be compiled into object or machine code before it can be executed. The object code as well as other support files are generated every time the project is run. You will be asked to delete these auxiliary files before you turn in your program to the file server, since they require lots of additional space and are not necessary after a program is compiled and linked. Space is a concern for the department server because there are so many students turning in work electronically.

    1. From the Project menu - choose: Remove Object Code; push the All Targets button.

    2. From the File menu - choose: Exit.

    3. Locate the folder you created in Step 1 (yourLastNameLab1) and open it.

    4. Delete all files except those ending in ".cpp" (the source files). You should have 2 files remaining in the folder. (yourLastNameLab1Part2. cpp and yourLastNameLab1Part3.cpp)

  2. Follow the checking out procedure outlined in the Lab Introduction and Assessment page.

If there is time remaining, resist the temptation to leave lab early. Instead experiment more with the compiler or the operating system, visit the course web pages, or start up the Code Warrior application and check out the tutorial and help documents.