CS 150 - Intro to Programming - Fall 2001
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.
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.
- Launch Code Warrior. Code Warrior will start and open the IDE window.
- Create a new project.
- Pull down the File menu to New...
- 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.
- 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.
- 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.
- You should save your files on your machine's hard drive and then copy them to your floppy at the
end of each lab.
- When you are done specifying the project, click OK.
- 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.
- 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.
- 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.
- Run the program contained in the project, as described below.
- 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".
- 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.
- Quit Code Warrior.
- From the Project menu - choose: Remove Object Code
- push the All Targets button - this removes temp files from the hard drive.
- 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.
- Start Code Warrior if necessary. From the File menu - choose: Open or Open recent. Locate and open your project
(yourLastNameLab1.mcp).
- Open the Sources folder in the project window by clicking on the "+" symbol.
- Open the file named "hello.cpp" from inside the project window (double click the file icon or File->Open).
- 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.
- 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)
- 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.
- 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.
- Save the file (select Save from the File menu).
- 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.
- Verify the correctness of the output. If you discover an error in the display, correct the program, save it
and run it again.
- Follow the steps at the end of Part 1 to quit your program.
- 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.
- 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.
- 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.
- 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.
- 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 (yourLastName
Lab1Part3.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;
}
- Save the file (File->Save).
- 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.
- SINGLE-CLICK (don't double click) on the Source folder icon to select it. Then pull down the Project menu and
choose Add Files...
- Use the file navigation dialog box and select (SINGLE-click) the file yourLastNameLab1Part3.cpp. Then
click Add, to add it to the project.
- Verify that all targets are selected and then click OK.
- 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 .
- 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.
- Select Run from the Project menu.
- If error messages appear, return to the source file window. Correct any typing errors you made, save the file
and run the program again.
- Once the program runs, Input integers for the area. Press Enter to continue.
- Repeat the Run/Continue sequence to try out other inputs.
- 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.
- 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.
- From the Project menu - choose: Remove Object Code; push the All Targets button.
- From the File menu - choose: Exit.
- Locate the folder you created in Step 1 (yourLastNameLab1) and open it.
- 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)
- 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.
On Your Own:
Due: At the Beginning of your next lab section.
The next exercise is designed to familiarize you with the procedure for completing and submitting programming
projects. For each programming assignment you will be required to turn in an electronic version of your code and
a paper copy. The electronic version is necessary so your program can be tested and the paper copy is necessary
so I can provide you with feedback.
Design, implement and test any C++ program. Your program must print at least one line of text.
- Recall that the basic steps are:
- create a new CodeWarrior project folder and project file
- create and edit a source file
- run the program, correct errors in source file, run again and so on.
- Be sure to document your source file - use the exact indenting and spacing described in the style
guidelines.
- Turn in an electronic version to the lab1HomeWork folder on the server.
- Turn in a hard copy.
What to Turn In:
electronic version files:
- yourLastNameLab1HomeWork.cpp
paper version:
- yourLastNameLab1HomeWork.cpp