Home

 

 

This document is current at:  Saturday 7th March 2009, 16:36 PT, AHD

 

C++ Lab Assignments 

 

Instructions: 

 

FOLLOW THESE INSTRUCTIONS EXACTLY OR YOUR PROGRAMS MAY NOT COMPILE.

 

[These instructions assume you are in CSCI101A and you are starting Lab 1.  If you are in a different course, or it is a different lab, make the appropriate substitutions.]

 

1.    Create a folder in C:\temp to save your lab. For example, if you are in CSCI101A and this is lab 1,

      create this folder:    

      C:\temp\csci101a\lab1 (use all lower case and no spaces)

 

2.    Start up the Borland C++ program (click on the Start button, then Programs, then select       Borland C++ from the programs list).

 

3.    From the Borland C++ menu bar, select File -> New -> Project to get to the "New Target"       window.

 

4.    In the "Project Path and Name" box type:

 

      C:\temp\csci101a\lab1\lab1.ide

 

     [Whatever you type here MUST match the path and name of the C++ program file (in this       case lab1.cpp) that you are about to create, except that you type .ide instead of .cpp]

 

5.    In the "Target Model" box select "Console"

 

6.    Click on the "Advanced" button and deselect (turn off) .rc and .def

 

7.    Click on "OK" to get back to the "New Target" window

 

8.    Click on "OK" in the "New Target" window

 

9.    In the Projects window, double click on the file name lab1.cpp to open up the code       window.

 

(Important note: if a file called lab1.cpp already existed in the C:\temp\csci101a\lab1 folder, then Borland would find it and open it for you - useful if you want to build a project around a program that already exists! If a file of that name does not already exist in that folder, an empty code window opens ready for you to type in your program.)

 

10.   Close the Projects window.

 

11.   Click in the code window to start typing in your C++ program code.

 

12.   Copy and paste the following starter program from this document to the Borland C++ code       window.

 

 

//  Start of program

 

//  Programmer:  write your name here

//  Course code: write your course code here, e.g. CSCI101A

//  Date:        write today's date here

//  File name:   lab1.cpp

//  Problem:     write the programming problem here

//               (or the text book page and exercise number)

 

 

#include <iostream.h> 

#include <conio.h>

 

void main (void)

{

    cout << "Test \n\n";

 

 

    cout << "Press any key to end this program...";

    getch();

}

 

// End of program

 

 

 

13.  Select from the Menu bar: File -> Save

 

     (Important note: you should use File -> Save every time you make a change to your program     before you compile it.)

 

[IF YOU LATER WANT TO COPY YOUR C++ FILE TO A FLOPPY DISK, YOU MUST QUIT BORLAND C++ FIRST!  THEN SIMPLY COPY AND PASTE THE FILE FROM THE C:\temp\csci101a\lab1 FOLDER.]

 

14.   Select from the Menu bar: Project -> Compile

 

     (There should be no errors or warnings. If there are any errors, the program will not       compile to executable code. You will need to find any errors in your code, save and       recompile. If there are warnings but no compilation errors, executable code will be       generated and automatically saved to a file with the same name as the .cpp file, but with the file extension .exe.  For example if lab1.cpp successfully compiles, then the       executable file lab1.exe will be generated.)

 

15.  Select from the Menu bar: Debug -> Run (or click the Run button) The lab1.exe program       will now run.

 

You are now ready to start typing in your own code in the space provided in the starter program.  Before you start, take the time to read the document in the CSCI101A\C++ Program Development folder called "How to develop a good C++ program.doc".  The marking scheme for all programming assignments is based on this document.

 

16.   Make sure you understand what is required BEFORE you start to design your program       solution.  Make sure you have a design BEFORE you start to code (type in the program).       Jot down a few notes on how you will solve the problem. If you wish, you can check out       the program requirements and your design with the instructor.


17.  If the program problem starts by reading in, for example, two integer values, write the       code for this first. Use cin to get the integers, use cout to print the values entered to    the screen to double check all is OK.

 

     Write a few lines, Save, Compile, Run, Test

 

     Now write a few more lines of code, then:

 

     Save, Compile, Run, Test

 

     Now write a few more lines of code, then:

 

     Save, Compile, Run, Test

 

     NEVER, NEVER, NEVER write more than a few lines of

      code without doing:   Save, Compile, Run, Test

 

18.   IF YOU HAVE ANY ERRORS: If you have any errors in your program, an executable version of your code will not be generated. If you have any errors in your program, the Borland IDE will open a Message window (View->Message). The Message window lists the errors and warnings (potential errors) detected by the compiler. Errors are identified with a red exclamation mark  ! and warnings are identified with a yellow exclamation mark ! in the messages window.

 

This is a typical error message:

 

! lab11.cpp(20,17):Statement missing ;

 

This error message means that the compiler detected an error at line 20 column 17 in your code. 

The type of error is "Statement missing ;" which means that the compiler has detected that a semi-colon (;) is missing in the code.  This particular type of error ("Statement missing ;") usually originates at the line before the reported line (in this case, line 19 not line 20).

 

You may have several errors and several warnings on compiling your program. Read the first error message by double clicking on the error message, i.e. the text to the right of the first red mark ! in the Messages window. This causes the Borland IDE to skip to the line in your program which contains the error.  Look for the error at the line containing the flashing cursor. Sometimes the error is on the previous line. Find the error, correct it, then:

 

      Save, Compile, Run, Test

 

     Correct errors ONE AT A TIME, starting with the first error, and after each correction:

 

     Save, Compile, Run, Test

 

19.  Devise a set of test data to test that your program is running correctly. Check that your program runs correctly with different sets of input data.

 

20.   When your program is complete, do one final

      File -> Save

      then exit Borland C++.

 

      Then, after quitting the Borland software, using Windows Explorer, locate your file in       C:\temp\CSCI101a\lab1, and drag it onto your folder in:

      \CSCI\Anne Dawson\CSCI101A\week01\lab1

 

      Open your folder in:

      \CSCI\Anne Dawson\CSCI101A\week01\lab1

      From the Explorer View menu, select Details. Make sure your file has a byte size > 0.

 

      Note: you can only save your file to this folder once. If you need to resave your file       for any reason, rename your local .cpp file and repeat the save instructions. You only       have write and list folder permissions in subdirectories of the destination folder:

      \CSCI\Anne Dawson\CSCI101A\Week01\Lab1

      This means you will not be able to open any files in this folder, but you can list the       files and inspect the file sizes.  Please note, Borland will generate extra files when       you compile a .cpp file, but you only need to copy the .cpp file to

      \CSCI\Anne Dawson\CSCI101A\week01\lab1

 

IMPORTANT Be careful how you save your files when using Borland. If you use the Save As option to save a .cpp file, make sure you're in the source code window and not the projects window when you save. If you inadvertently corrupted your C++ code by overwriting it with the contents of the project binary file, your .cpp file will contain a jumble of text and strange characters. To revert back to the last saved version of your .cpp file: Exit Borland, locate the folder of your .cpp file. Delete the .cpp file, then find a file of the same name but with the extension .bak. Rename the .bak file with so it has the extension .cpp.