Lab05, CISC181, Spring 2004

Background

Your Deitel/Deitel text sometimes functions like a textbook, but other times, it functions more like a reference manual. For this lab,the following section(s) may be useful as "reference" material.

Part 1: Experimenting with C-strings and cin

  1. Find the file cstring.cpp in the lab05 subdirectory. Read over the code and try to understand what it is doing. Note that we are using "C-style strings" here, which are basically just arrays of characters, with \0 marking the end of the string.

  2. Compile and run this program. Try your own name as input. Does the program appear to work properly?

  3. Notice the use of the std::ws on cin to get rid of whitespace. What happens if you take out that line of code? Experiment by commenting this out and seeing what happens.

  4. Now consider the fact that in some families, a name like "Mary Beth" or "Jimmy Bob" is considered to be a single name (not a first name and a middle name).

    Now try "Ann Marie" as the first name, and "Fitzpatrick" as the last name. Does the program work the way you would want it to?

    Try the same thing with "Billy Bob" as the first name, and "Thornton" as the last name.

    Notice that the program does not appear to work properly. Fix the program by using the cin.getline function in the proper spot.

  5. When the program is fixed so that it works properly with names that have embedded spaces, add to the program code that will also ask for the name of a city and state. The city should be allowed to have embedded spaces (like "New York"), and the state should be exactly a 2 letter abbreviation (like DE or NY). Note that for a 2 letter state abbreviation, you need a C-string array of 3 characters.

  6. For this step, script your finished program that demonstrates asking for first name and last name separately and together, as well as city and state. The purpose of the program is only to show that you know how to read this kind of character data in, and print it back out again, using C strings.

Part 2: Command line parameters

  1. Copy the file cmdLine.cpp from the lab05 subdirectory.
  2. Compile and run the program with
       g++ cmdLine.cpp
       ./a.out
    
    Then try running it with
       g++ -o cmdLine cmdLine.cpp
       ./cmdLine
    
    How is the output different? Look at the output, then read through the source code, and see if you can figure out what is going on.
  3. Now try running the program with these command lines:
    ./cmdLine help
    ./cmdLine repeat 5 Hello There!
    ./cmdLine foobar
    

    In each case, look through the source code and try to understand what is happening.

  4. Now, add a new command to the program. The command "reverse" should print out the reverse of all the remaining items on the command line. For example:
    ./cmdLine reverse This is a test
    
    should produce output:
    test a is This
    
  5. When you are finished, script this, showing that the reverse command works, but that all the other commands still work as well.

Grading

  1. Part 1: 25 points
  2. Part 2: 25 points
  3. Total points: 50

Phillip T Conrad
Last modified: Wed Mar 31 08:03:36 EST 2004