CSC103 Lab 1 - 15 Points



Name____________________________________________



Partner(s)________________________________________





Part 1: 5 Points, Due During Lab Period



You are going to type, run, and print the MyNameIs program listed in Step 3.

First, Borland C++ has to be started.



Step 1 - Start Borland C++

Start Windows 3.1.

The screen should be similar to Figure 1.

Double click the left mouse button on the Borland C++ 4.5 group icon.

The screen should be similar to Figure 2.

Double left click on the Borland C++ icon in the group of icons.

The screen should look like Figure 3.



Step 2 - Make a new Edit window

In order to type a new program, an Edit window has to be created.

Create an Edit window by selecting File|New.

Now make the window full sized by selecting Windows|Tile horizontal.

The screen should look like Figure 4.



Step 3 - Typing a program

Before typing a program, it is a good idea to save it under its new name.

Please do the following:

Select File|Save As.

Left click on the Drives box.

Left click on b: in the drop down menu.

Left click on the File Name box, type in MyNameIs, and strike Enter.



What is the new title of the Edit window?



______________________________________________________________





Now type in the MyNameIs program on the next page.

Typing tips are in the Editing section of the Borland C++ Reference.

The editor will automatically create the bold characters.

To indent, strike the Tab key. The editor will automatically indent the correct distance.



/*

Program: MyNameIs

Programmer: <put your name here>

*/



#include <cstring.h>

#include <iostream.h>



int main(void)

{

string Name;



cout << "Enter your name: " << endl;

cin >> Name;

cout << "Hello my name is: " << Name << endl;

return(0);

}



After typing the program, save it by selecting File|Save.





Step 4 - Compile and Run

Compile and run MyNameIs by selecting Debug|Run or by striking Ctrl+F9.

If there are errors, refer to the Compiling and Running section of the Borland C++ Reference.



Even a correct compile and run will generate one warning message. Ignore the warning.

(The system will automatically ignore the warning.)



The output written by a program is displayed in the Output window. The window title is the name of the executable program. In our case, the title is C:\TEMP\MYNAMEIS.EXE.

After a program ends, the Output window is still displayed and must be closed before proceeding.



After MyNameIs has been compiled and run, close the Output window.



For practice, compile and run MyNameIs several times. Try entering different names.



After a program ends, the title of the Output window is changed slightly.

What is the title of the Output window after MyNameIs ends?



____________________________________________________________________



Step 5 - Print

Print the MyNameIs program by doing the following:

Select File|Print.

Left click on OK in the Print Options Screen.



Step 6 - Debugging

This step illustrates how to correct a compile error. The Compiling and Running section in the Borland C++ Reference explains how error messages are displayed.



In the statement cin >> Name; change N to n so that the statement becomes :

cin >> name;



Compile and run MyNameIs.



What happened?_____________________________________________________



___________________________________________________________________



Why?______________________________________________________________



___________________________________________________________________



What correction did you make?__________________________________________



___________________________________________________________________





After Steps 1 through 6 are complete, call the instructor to check your work.

After your work has been checked, close the Edit window by double left clicking on the Control Menu Box or by striking Ctrl+F4.





Part 2 - 10 Points, due at the start of lab on Sept. 15



You are going to write a new program Cost by changing MyNameIs. The Cost program will calculate the total cost after asking for the cost per pound and number of pounds purchased.



Step 1 - Load MyNameIs

Load the MyNameIs program by doing the following:

Select File|Open.

Left click on the Drive box.

Left click on b: in the drop down menu.

Left click on mynameis.cpp.

Left click on OK.



Step 2 - Save MyNameIs as Cost

Instead of modifying MyNameIs, save it under the new name Cost. Use the File|Save As method from Part 1, Step 3. This will keep the MyNameIs program, but allow you to modify it as the new program Cost.





Step 3 - Typing Cost Program

Edit the Cost program so that it looks like the program below.

Be meticulous and careful.

/*

Program: Cost

Programmer: <put your name here>

*/



#include <cstring.h>

#include <iostream.h>

#include <iomanip.h>



int main(void)

{

string Name;

int Pounds;

double CostPerPound;

double TotalCost;



cout << "Enter your name: " << endl;

cin >> Name;

cout << Name << ", enter cost per pound" << endl;

cin >> CostPerPound;

cout << Name << ", enter number of pounds" << endl;

cin >> Pounds;

TotalCost = Pounds * CostPerPound;

cout << setiosflags(ios::fixed); // Set fixed format

cout << setprecision(2); // Set two decimal places

cout << Name <<", the total cost is: " << TotalCost;

return(0);

}



After typing in the changes, save the program.





Step 4 - Compile and Run

Compile and run Cost for each of the following values.



Cost per pound = 1.23

Pounds = 7

Did you get 8.61? ____________________________________________



Cost per pound = .35

Pounds = 117

Did you get 40.95? ____________________________________________





Cost per pound = 1.259

Pounds = 3

Did you get 3.78? _____________________________________________



What is the correct answer to 3 decimal places?______________________

Change setprecision(2) to setprecision(3) and repeat the test.



The result should be correct. Why?__________________________________



_____________________________________________________________





Change setprecision(3) back to setprecision(2).

Compile and run Cost for cost per pound = .16 and pounds = 51.7

Did you get 8.16? _____________________________________________



What is the correct answer?______________________________________



Why did Cost calculate the wrong answer?__________________________



____________________________________________________________





Change the statement double TotalCost; to int TotalCost;

Compile and run Cost for cost per pound = .20 and pounds = 53



Did you get 10?_______________________________________________



What is the correct answer?______________________________________



Why did Cost calculate the wrong answer?__________________________



____________________________________________________________



Turn in the lab sheet, a disk containing Cost, and a listing of Cost.