CS 112b
Spring 2004
Ileana Streinu

Lab 1

Introduction to C++

This lab will be spread over the week on Tu and Th, during the lecture and lab periods.

Read the whole lab before starting working on it, so that you know what you are required to do by the end of the lab.


Today you learn the basic routine for writing C++ programs, compiling them, executing and documenting the execution. You will do this for every homework and for many labs throughout the semester.
Make sure you know how to carry through each of these steps, otherwise seek help from me.

Getting Started

This section will help you connect to the Linux server.
    In Windows
  1. Login to the Novell Tree using your student Novell account.
  2. Launch an ssh window. You do this the way you learned in CSC111, or ask me if you do not know how. Connect to beowulf.csc.smith.edu.
  3. When the login prompt to beowulf shows up, enter your 112b-xx account and password.
  4. Change the password to something you remember easily and write it down. I will not be able to help you if you forgt your password.
  5. Check your email: you should have received an email from the class account 112b. If not, please email me at 112b "at" cs.smith.edu to let me know that you should be added to the class mailing list.

    In Linux
  1. Turn off all programs and reboot your computer into Linux mode.
  2. Log on to the system using your 112b class account.
  3. Then use the shell windows to check your email using pine.

If this works, you will have the option of logging on and doing your work under any of the two environments (Windows or Linux). I strongly recommend that you get familiar with working directly under Linux.

Create a directory for each lab and homework

From now on, you will do your work for each lab or homework in a separate directory (or else things will get VERY complicated pretty soon). To get started, create NOW a directory named Lab1 in your class account. In the future, as well as when doing your homework, do this automatically (i.e. create a new directory named Labx or Hwx, where x is the number of the lab or homework, and work in it). Then change your working directory to Lab1.

You do this by using the Unix commands:

mkdir Lab1
cd Lab1

Your first C++ program

Enter your first C++ program with emacs.
EXERCISE 1: Once your program works, modify it so that it displays also the number of apples left over. For example, if you have 10 apples and 3 students, each student should get 3 apples, and there will be one left over.
Name your source file ex1.cpp.
Skip the second exercise for the time being, and do the Create a typescript and submit part. I want to make sure everybody knows how to create a typescript and that submit works. Then come back and finish the second exercise.

C++: Reading strings of characters

To read a string of characters, use cin.get(string,length,delimiter), where length is the maximum number of characters that can fit in the string, not including the '\0' terminator, and delimiter is the character that marks the end of the input.

Here's an example that reads a string and prints it back.

	char name[20];

	cout << "enter your name: ";
The second call to cin.get() reads only one character, the end-of-line marker that the first call to cin.get() stopped on, but didn't remove from the input stream.
EXERCISE 2: Write a program that asks the user for her first name, her middle initial, and then her last name, in three different input operations, and then outputs all three of them on the same line, as in this example:
First name?     Marge
Middle Initial? F
Last name?      Philips

Hello Marge F. Philips!
Use two strings and a character to store the three pieces of information. You will have to be careful when you get the middle initial and see if you need to get the end of line (eoln) marker after it or not. You may have to try different options and see which one is the solution.

Creating a typescript and submitting

When you are done with these two exercises, use script to save a copy of the execution.

Wrap Up

Check that you have submitted your work. A first step is to look into the directory where all the files have been copied to. You can do this from your class account with the command:

ls ~112b/handin/Lab1/112b-xx/

where 112b-xx is your hermite account. You will not be able to look inside the directory to see what files are in, but at least you can check that the directory is there.

The ONLY WAY to make sure that you have submitted all the requested files is to have make a list and check each item after you submitted it successfully. If you got an error after submit, your file may not have been submitted, so you'll have to do it again until you got no error.

You can always resubmit (by the deadline). The submit program will ask you if you want to overwrite the old file, so you have a chance to change your mind. As a general strategy from now on, I recommend that you submit the first version that runs satisfactorily (to make sure you do the submission in time), then if you improve it later, resubmit.

Optional: Pipe in input data to your program from a data file

If you are done and want to learn one more trick, then do this part.