University of Pittsburgh
Summer 2001
CS401:  Introduction to Computer Science
Lab 0:  Using Borland C++ V5.02
Read the entire lab.  You should print this out.
INTRODUCTION

Writing a good computer program is not an easy thing to do (heck, using a computer program is often not an easy thing to do, much less writing one).  Thus, learning how to program is a daunting task.  A great amount of effort and time has been spent on writing programs that make it easier to write more programs.  In fact, this has been a general trend since computers first existed!

As you've already learned, a source code file (containing the text of a program as written by the programmer) must undergo some transformation before it can be executed on a computer.  We could, in theory, do this translation by hand - by going line-by-line through the source code and creating machine code (all binary) that implements our program.  Luckily, we have programs available that will do this for us, and these programs are known as a compilers.  Software that combines a compiler with other program development tools (editor, linker, debugger, loader, etc.) are called integrated development environments (IDE).  Borland C++ is such an environment, and is the topic of this lab.

Just a note:  sometimes you will hear people refer to an IDE as a compiler.  For example, someone could ask "what compiler are you guys using?" with the answer "Borland C++".  Even though Borland C++ is an IDE, it is ok to refer to it as a compiler.  The implicit meaning is that you are talking about the compiler module within the IDE, but no one ever worries about such details in casual conversations.  Now that I think about it, us being in Pittsburgh and all, you might hear someone say "What compiler are yinz guys usin'?", to which you might hear, "Ahh... Borland, en' at'".

Also, even if you don't intend to use Borland C++ in the future, it is important to become comfortable and proficient with an IDE.  Even though every IDE is different, they all share a similar feel.  For example, all provide an editor, debugger, linker, loader, etc. 
 

OBJECTIVES

The primary goal of this lab is to help you get you up and running within the Borland integraded development environment.  By the end of this lab, you should be able to:
  • create a Borland project (your program is part of a project)
  • use the editor
  • compile a program
  • fix syntax errors
  • run a program in a separate window via the command-prompt
When you do these labs, don't just go through the motions.  The skills you learn are hands-on and practical, so pay attention to what you are doing, and try to remember what buttons do what, and what menu selections do what, and so on.  The better you learn it now, the less you will have to re-learn it later. 
SAVING YOUR WORK

You should not have a problem finishing this within the alotted lab time, however if you don't, you'll need to have a floppy disk so that you can save your work and finish later (perhaps on a different computer).  To save your work (don't do this now, only if you do not finish your work in the lab), close your project (directions are in the next section), then copy your source code file to your disk (only the .cpp file, not the other project files, they are too large).  To be a good citizen, go back to the c:\users directory and delete the files there, just so the next person has a clean directory to use.

When you return, you'll need to start a new project with the same name, but copy your source code file to the c:\users directory before you do this.

IMPORTANT INFO FOR EARLY STARTERS:  If you are attempting to do this lab ahead of time (before your lab meets), make sure you have a floppy disk to save your code as described above.  You will need to bring this disk to the lab so you can recover the work you did.
 

WHAT TO DO

Borland C++ 5.02 uses a lot of auxiliary storage for each project. In this lab, and in the labs to follow, you will use the directory c:\users to temporarily store your work. Before you leave, be sure to copy all of your .cpp files from c:\users to your floppy disk, then erase your work from the c:\users so you don't interfere with other students' work. 

These instructions were designed for Windows NT and the computers in room MIB 111. If you are working in a CIS Lab, replace all references to the directory c:\users with c:\temp. These instructions may vary from one lab to another, and programming software is occasionally modified; please ask your TA for assistance if something doesn't quite work right and you've given a decent effort to figure it out on your own.

No matter how much programming experience you have, learning a new environment can be time-consuming and frustrating. The Borland IDE is a large, complicated system. It might take time for you to get used to it. 

Creating a new Project

The IDE manages a program as a project. Starting a new project requires several steps:
  1. Select the Start button in the lower-left corner of your screen, then Programs, then Borland_C++_5.02 (Programming_Languages in the CIS labs), then Borland_C++. This will start the IDE.  It is a good idea to maximize the window by clicking on the small box in the upper right hand corner of the window.
  2. Under the Options menu, click on Project. Look at the Output_Directories box. Make sure that the lines Intermediate and Final are empty. Confirm any changes and close the window with the OK button. 
  3. Under the File menu, choose New, then Project. A screen will appear. Then...
    1. Clear the box Project_Path_and_Name, then type the name of your project file with an .ide extension. Since your project will be stored in the c:\users and will be called lab0, you should type c:\users\XXXlab0.ide, where the "XXX" is your initials. Do not hit the enter key.
    2. Choose the Target type to be Application
    3. Choose the Platform to be Win32
    4. Choose the Target_Model to be Console
    5. Choose Static
    6. Click on the Advanced button, and make sure that only .cpp is selected (.rc and .def must not be checked). Click OK
    7. Click on OK in the New_Target window to finalize your selections. 
    8. At this point, a project window should appear on your screen.
  4. Double click on the button called XXXlab0.cpp[cpp]. A window for your program will appear on the screen. 
  5. Type the following program into the editor window.  Feel free to change the string to whatever you like (in the cout line).
  6. /* lab0.cpp
       <your name>
       CS401 - lab 0
    */
    #include <iostream>
    #include <string>
    using namespace std;

    int main() {
      cout << "Let's go BLUES!" << endl;
      return 0;
    }

  7. Save this file by going to the File menu and selecting Save
You may be wondering what all those selections mean (especially from step 3).  In a nutshell, there are many different kinds of programs you can write with Borland, and there are tons of options on those different kinds of programs.  The selections you just made are necessary to set up a simple, one file project that runs in a small text window (the "console" runs in a small text window). 

Running the program

There are two ways to run the program:  from within the IDE, or from a command-prompt.  We'll do both.

To run it from within the IDE, pull down the Debug menu, and select Run.  Watch carefully!  You will see it go through the compilation, link, and load phases very rapidly.  The output window will then appear and disapeer almost instantly.  The reason it goes away so quickly is due to the fact that the program doesn't do much.  All it has to do is print a message.

NOTE:  You will see several messages pop up in the Message window at the bottom of the screen.  The lines beginning with a yell exclamation point are known as compiler warnings which do not prevent your program from running, they are only there to alert you of things to be aware of.  For this project, you should at least glance over them, but nothing else.

If you get a syntax error you probably had a typo somewhere, just double-check that you typed the program in correctly.  If you get a more serious error then something may be wrong with the compiler's configuration.  Check with your TA.

To get a better look at the output, we need to use a command prompt, which runs independently of Borland.

  1. From the Project menu choose Make_All. This compiles and links your program.  Click OK.
  2. Minimize your IDE window by clicking on the underscore in the top-right corner of your screen.
  3. From the Start button on the lower-left of your screen, choose Programs, then Command_Prompt. This will start a command prompt window (you may need to search further if you can't find it here - it may be under Accessories).
  4. Change to the directory containing your project with the command cd c:\users.
  5. Take a look at the contents of the directory by typing dir.  This will list all the files in the directory so you can see what Borland created for this project.  You should note the .cpp file and the .exe files which are the source and executable files, respectively.  NOTE:  If you see a ton of files, some rude person did not clean up the directory for you.  You can then try the command dir XXX* instead.  This will list all files that begin with your initials (substitute your initials for XXX)..
  6. Run your program with the command XXXlab0.exe, where, again, "XXX" are your initials.
  7. You should see your message be displayed on a line, then another command prompt.  Run it as many times as you like!  Go Nuts!!
  8. To get out of the command window, just type exit
  9. Click on the Borland C++ button on the taskbar (bottom of your screen) to bring the IDE back.
The window didn't disapeer this time because the command prompt and loader run in the same window.  Thus, you manually loaded your program into memory and executed it, when the execution ended, control came back to the command prompt.  Earlier, inside Borland, control returns to the IDE, which has no use for the command window. 

Closing the project

Close your project by selecting Close_Project from the Project menu.  You should see the empty IDE again, just as when you started Borland initially. 

A more involved example

Now we'll do something slightly more realistic.  Use the steps above to create a new project, but this time call the project finance.ide. You will probably find that many of the selections you made earlier are still there, but you should verify them nonetheless.

When it comes time to enter code, use the following program instead.  NOTE:  You probably will not understand much of this (you've seen some of it in the reading, though).  Don't worry, you will just type it in and run it, no need (yet!) to understand all the details.
 
 
/* finance.cpp
   <your name>
   CS401 - lab 0

   This program computes the return on an investment.

   Input: Amount of investment, years of investment, rate of 
     interest and rate of inflation
   Processing: Each year the interest is accumulated and inflation
     reduces the value of money
   Output: A table showing the impact of inflation and interest 
     on the investment
*/
#include <iostream>
#include <string>
using namespace std;

int main() {
   int term;
   float money, moneyin, rateofreturn, inflat;

   cout << "Enter amount of investment in dollars :";
   cin >> money;

   cout << "Enter the number of years that you plan to invest :";
   cin >> term;

   cout << "Enter the rate of interest as a decimal fraction"
        << " (0.08 rather than 8%)" << endl 
        << "  that you expect to receive: ";
   cin >> rateofreturn;

   cout << "Enter the rate of inflation as a decimal fraction"
        << " (0.04 rather than 4%)" << endl
        << "  that you expect over the years of the investment :";
   cin >> inflat;

   moneyin = money;

   for (int year = 1; year <= term; year++) {
      money = money * (1 + rateofreturn);
      money = money / (1 + inflat);
      cout << "After " << year << " year(s) the value is " 
           << money << endl;
   }
}
// end finance.cpp

Make sure you have saved your work!  Run the program a few times (you'll need to use a command-prompt window) and watch it work.  Again, if you have any errors, you'll need to resolve them before you start your worksheet (this project is used in the worksheet). 

Breaking an executing program

It is often necessary to stop ("break") a program while it is running after you've seen something go wrong, or you've decided to stop before the program officially completes.  To see how to do this, run the finance program in a command window, answer the first two questions, then hold down the control key (Ctrl) and then press the "C" key.  You will often see this in print as: Ctrl-C.  You'll see the program stop and you'll be sent back to the command prompt.  If you did the same thing in a Borland execution window, you'd be sent back to the IDE.
LOOK IT UP / FIGURE IT OUT

Two important skills a good Computer Scientist / programmer needs is first, the ability to find answers that are buried in mounds of documentation, and second, to be able to explore an environment and figure out how to do something based on intuition alone.  Each lab will have a section like this with something in this category. Sometimes we learn the best information on accident, while looking for something else.  Therefore, do not ask for help here!

You've noticed by now that in the editor window the program has syntax highlighting.  This is a nice feature that helps us to see the program more clearly (e.g., reserved words are in boldface). 

First, figure out how to turn off syntax highlighting.  Take a look at the source code and notice how different (and plain) it looks.  Turn syntax highlighting back on.

Next figure out how to customize syntax highlighting.  See if you can make strings be light gray (rather than blue).  Take a look again at the text of your program, then go back to the customization screen and return it to blue.

You may go about this by either just surfing through the menus, looking through the online help, or both.