This tutorial shows you how to create a C++ program that uses standard console (character) input/output only. This kind of program doesn't use any Windows-specific routines.
This tutorial takes about 10 minutes.
You create a program by doing these tasks:
Launch the CodeWarrior IDE.
Click Start in the task bar (i.e., the Windows95 start button in
the lower left corner).
Choose Programs,
then choose Metrowerks CodeWarrior,
then choose CodeWarrior IDE.
The main window of the CodeWarrior IDE ("Integrated Development Environment") application appears.
Introduction
First you'll create a new project.
A project contains a list of the files, such as source code files and libraries, needed to create a program. A project also contains settings that determine how the program is created.
Choose New Project from the File menu (at the upper left corner of the upper options bar).
A dialog box appears, titled "New Project."
From the "Project Stationery" list, navigate the hierarchy of
project types
to go to Win32x86
(click on the blue triangle to the left of the character string)
then to C, C++.
Then click the C++ Console Application item to select it (it becomes highlighted).
The stationery project you've chosen contains libraries and settings that allow you to create a C++ program that uses ANSI-standard console input/output routines.
Make sure the Create Folder checkbox (lower left box) is selected.
If the Create Folder checkbox is not selected, click it.
You create a new folder (i.e., subdirectory) to make a place to keep all the files associated with your project. A folder is a convenient way to group your project, your source code, and the program created from your project.
Click OK to continue.
The New Project dialog box closes and a Name new project as dialog box appears.
In the Name new project as dialog box, use the navigation controls to choose a location for the new project. This is done by clicking on the downarrow at the right side of the "Save in" field (at the top of the dialog box). Other options will appear, and you can scroll to your choice, and click on your choice which will highlight it and insert it in the "Save in" field.
A good place for your project on your own machine is the Desktop, but 3 1/2 Disk A if you use a lab machine.
NOTE: Don't put the new project in the CodeWarrior folder or in one of its sub-folders. You may lose your project when you update CodeWarrior software.
Without pressing Return (or Enter), move the cursor to the File name field near the bottom of the window, click on it, and type a name for the project in that field.
Name the project "Newhello CPP" (without the quote marks). In the future you will enter a name for any new project at this point, with the extension "CPP".
Click Save to create a new project file and close the dialog box.
In a moment, a new window will appear, titled "Newhello CPP.mcp".
This is the project you've just created.
The CodeWarrior IDE uses the ".mcp" file name extension for CodeWarrior projects, which may contain several files of code.
Introduction
Now you'll use the IDE's text editor to create source code for your program.
C++ is a case-sensitive language. Remember to type the source code exactly as it appears in this tutorial.
The IDE editor has features to help you enter source code.
- Press the Tab key to indent a line. The editor will automatically indent ("auto-indent") the next line to the same level as the previous line for you.
- Press the Backspace key to remove any spaces or tabs that the editor has auto-indented.
Choose New from the File menu (at the upper left corner of the upper options bar).
A new untitled window appears.
Type the comment below. Press Return at the end of the line.
// Newhello.cpp
Note: Syntax Highlighting
Did you notice that the color of the comment changes as you type it in? This is syntax highlighting in action. Syntax highlighting uses color to emphasize keywords and other items, making it easier for you to view, enter, and edit your source code.
Type the source code below into the editor window. Press Return at the end of each line.
#include <iostream.h>
int main()
{
cout << "Hello from CodeWarrior!\n";
cin.get(); // Wait for the Enter key to close
// the output window
return 0;
}
Choose Save from the File menu.
A dialog box appears, titled "Save document as."
In this "Save document as" dialog box, use the navigation controls to go your chosen storage location (e.g., 3 1/2 Floppy A:), then open the Newhello CPP folder by double clicking on the name.
This is the folder you created for your new project.
Move the cursor to the File name field, click on the field, and erase "untitled." Then, enter the name of the source code file as follows:
Without pressing Enter or Return, enter "Newhello.cpp" (without the quotes) in the File name field.
Click Save to save the file and close the file dialog box. (If a warning box appears about a duplicate file name, either change te file name you have chosen, or answer yes to overwrite the existing file.)
The title of the editor window changes to Newhello.cpp.
Introduction
Now you'll add the source code to the project.
To add a source code window to a project, you'll do these steps:
If you have replaced an existing file of the same name, you may skip all of part 3.
- Add the source code window to the project.
- Make sure the item you just added is in the correct group in the project window.
- Remove any place holders that are already in the project.
Choose Newhello.cpp in the Window menu to make it the active window (if the upper bar is not dark blue). (The "Newhello.cpp" window can also be made the "active" window usually by clicking somewhere in the window.)
Choose Add Window from the Project menu.
If a dialog box appears, titled "Add Files", make sure all the checkboxes in the Targets section of the dialog box are selected.
Click OK when you're done.
This action adds the active window the project.
Make sure the group named Source is expanded.
If it isn't, click its little blue arrow (to the left of the character string) so that it points down to reveal the item(s) already in the Source group.
Drag and drop the item named Newhello.cpp onto the entry named Source.
Newhello.cpp should appear, indented, immediately below Source.
Note: About "Place Holders"
Now you'll remove the place holder file from the project.
A place holder is a file that helps you organize your project. The IDE adds place holders to your project and your project folder when you create a new project.
A place holder's name and its position in the project window tell you what kind of files you should replace it with.
Because it is a file like any other file in your project, you may change a placeholder's contents, add related files to the same group as the placeholder, or even remove the placeholder from the project.
To select the place holder (e.g., hello.cpp or main.cpp), click its name in the project window.
Choose Remove Selected Items from the Project menu to remove the place holder from the project window.
The Source group should now contain only one item: Newhello.cpp.
The other groups in the project include standard libraries necessary for the program to run. These libraries include support functions for displaying characters on the screen and other tasks. In a typical project you would include these or similar libraries. Project stationery, such as the C++ Console App stationery you selected in Part 1, usually includes the typical libraries required for a particular kind of project.
Introduction
Now you're ready to compile, link, and run your program.
NOTE: If this editing window is not closed when a program is first entered, you are likely to get compiler errors that report certain libraries are missing. After the first compilation, the window can be reopened to enter additions or corrections, and then usually left open.
Choose Run from the Project menu.
The Run command compiles all the new and modified files in your project, links the compiled files into an application, then runs the application.
If a window appears titled "Errors & Warnings", you may have entered the source code incorrectly, or added the wrong files to the project.
If the Errors & Warnings window did not appear, then the IDE successfully created an application from your project. It's now ready to run!
Congratulations! An output window should have appeared with "Hello from CodeWarrior!" written in it. The window will remain open until you press the Enter key.
You've created a standard C++ program that uses console input/output.
To quit your new application, press the Enter key.
In this tutorial you created a project, typed in source code, added it and other files to the project, then compiled, linked and ran the project.
Now you're ready to create your own standard C or C++ programs using CodeWarrior.
To learn more about the CodeWarrior IDE, choose an item from the Help menu or read CodeWarrior's on-line documentation.
To learn more about creating other kinds of software with CodeWarrior, follow one of the other tutorials. (Select "Win95 Start," then "Programs," then "Metrowerks CodeWarrior," then "CodeWarrior Reference Web Site.")