Lab06, 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 "actual" reading assignment is Chapter 6, about structs and classes. However, the following sections may also be useful as "reference" material.

Part 0: Copying files

Throughout this lab, you'l be using files from the lab06 subdirectory. You might want to just copy all the files from that subdirectory into your current directory now. Here's a command to do it:

cp -r ~pconrad/public_html/cisc181h/04S/labs/lab06 .

This will copy the entire lab06 subdirectory, along with all its contents, into the current directory. You'll then have to cd into lab06 to see all the files. The -r stands for recursive.

 

Part 1: Working with C++ classes

Complete exercise 6.12 from your Deitel/Deitel textbook. Put the class definition in a file Rectangle.h, the implementation of the member functions in a file Rectangle.cpp.

Also, write a main program (or two, or three) that tests the class and shows that it works properly. Do as many runs (and write as many mains) as you need to test all the functions of the class (including all error checking).

Remember when you compile to include both the .cpp file for the class and the .cpp file for the main on your command line. You should do a #include on the .h file inside both .cpp files (both the main, and Rectangle.cpp).

When you are done, make a script lab06p1.txt in which you cat all three files, compile them, and show your test runs.

Part 2: Using ifstream to open an input file

If you know right from the start that you are going to be reading from an input file, rather than making the user specify the name of the file on the command line with the shell redirect (<), you can open the file directly inside the program and read directly from the file. This way, you can still use cin to read other kinds of input. This part of the lab will show you an example of a program that does exactly that.
  1. Copy file readUsersFromFile.cpp into your directory, and compile it. Look at the source code, especially the lines containing "ifstream" and "userInputFile". Here are some explanations of this code:
  2. Copy the "usernames.txt" file from the directory where you did lab05 last week, into your current directory. Now run the "readUsersFromFile" program. Note that the program's output goes to the screen, and consists of listing the usernames from the file, along with a count of how many usernames there were.
  3. Now, modify the program so that it produces an HTML file as output on the standard output, similar to the one that was produced by the makeHTML.cpp file in lab05.
  4. When you are finished, use Unix redirection to send the output to a file called "usernames2.html".
  5. To see if the HTML code created is legal HTML code, copy the file to a directory "lab06" under a directory called ~/public_html/cisc181/files/lab06. Copy your usernames.txt file there as well. Then use a web browser to examine the output. (Remember to do the step that makes the files readable via the web.)
  6. Script this program as (lab06p2.txt) (cat, compile, run). Be sure to cat both the input and output files as well.
  7. Put the usernames.txt and usernames2.html file on the web in that spot where they're supposed to be (under files/lab06) and make them readable.

Part 3: Reading from an input file with multiple items per line

  1. Now, copy the files readFootballSched.cpp and udel.txt into your account, and take a look at them. The file udel.txt contains a header line with the letters "udel" on it, then one additional line for each football game the team played during the Fall 2003 season.

    The "udel" on the first line of udel.txtis the "domain name" of the University of Delaware (as in "www.udel.edu"). Each school is identified in the schedule by its domain name (this is a way of uniquely identifying schools so that we don't have confusion between "U. Mass" and "Massachusetts" for example. It also has the advantage that there are no embedded spaces in it, which makes it easier to read with the >> operator.

    The program readFootballSched.cpp reads the header line from the file udel.txt and then outputs it, and then it reads all the lines in the file. Notice how the program reads the "first" field on the line, and then checks for "cin.eof()" before reading all the other fields on that line. This is a typical way of reading data from a file.

    The program computes the win/loss/tie record for the team, and outputs it at the end. Compile and run the program, and see if you can figure out how it all works.

  2. Now, create your own file foo.txt where you replace "foo" with the domain name of the college or university you chose for this semester (for example, citadel.txt, or uri.txt). (A good source of information on football results is www.ncaa.org; you can also check your school's own web site.).
  3. Change the program to read from your file, instead of udel.txt, and run it again.

    Note: When you change the program, be sure to also change the comment at the top of the program. You should LEAVE my header comment EXACTLY as it is, but add your OWN header comment underneath that says: "Modified by (your name here), on (date here)", and then another comment that says "how" you modified the program. This is how software is modified in the real world: the original comment always REMAINS and anyone who modifies the software adds his/her own comments under the original header comment.

  4. Now, make one additional change to the program: in addition to computing the win/loss/tie record, compute "separately" the win/loss/tie record for home games, away games, and neutral field games.
    All Games:  win: xx lose: xx  tie: xx
    Home Games: win: xx lose: xx  tie: xx 
    Away Games: win: xx lose: xx  tie: xx 
    Neut Games: win: xx lose: xx  tie: xx
    
  5. Make a script lab06p3.txt in which you cat your modified "readFootballSched" program and your "foo.txt" file, compile the program, and run the program.

Part 4: Publishing your football schedule data file

Now copy your foo.txt file (the one containing the football schedule for your college, where "foo" is the domain name of your college) into the same directory you created earlier, namely:
  • Now, create a directory under your public_html called:
    ~/public_html/cisc181/files/lab06/

    Important: Do NOT put anything else in that directory except the files that you are specfically told to put there! In particular, do NOT put any C++ code in that directory! You will lose points if you do!

  • Now, do the "chmod" command again that makes that file readable.
  • Check the following URL (substituting your own userid) to make sure that each of the files is readable (replace "foo" with your college's domain name):
    http://copland.udel.edu/~userid/cisc181/files/lab06/foo.txt
  • Then check this URL to make sure there are no C++ files hanging out there. (You should be doing your "work" in a directory that is NOT under your public_html directory! If you need help figuring that out, ask your TA for assistance)
    http://copland.udel.edu/~userid/cisc181/files/lab06
  • There is nothing to script for this step: your TA will check the URLs above to give you credit or not. Note that he/she will start grading the day after the assignment is due, so if you are late getting these files up, you might lose the points. The only way to get them back is to see your TA during lab or office hours.

    Part 5: Writing to an output file with ofstream

    In the same way that you can read from an input file in a program with ifstream, there is also a constructor for an "ofstream" object that allows you to write to an output file directly from inside your program.

    Copy the file ofstreamDemo.cc to your account, compile it and run it. For Part5 of this lab, do the following steps. The result will be a script file lab06p5.txt, which you will submit.

    1. Copy the ofstreamDemo.cc file into your directory.
    2. Compile it, but don't run it yet.
    3. Before you run it, do an ls command, and note that there is no file called outfile.txt in your directory.
    4. Now run the program. Note the output you get on your screen. Then do another ls command and note that there should now be a file called outfile.txt in your directory. Use cat outfile.txt to list the output of this file.
    5. Now edit the ofStreamDemo.cc file to personalize the message that is going to the file. Make it print your name to the file as part of the message, along with any other pithy comments you want to pass along to your TA and/or instructor.
    6. Use the rm command to delete output.txt from your directory. Then, make a script file output.txt in which you
      1. cat and then compile your new (personalized) version of ofStreamDemo.cc,
      2. use ls to show that there is no output.txt file in your directory,
      3. run the program,
      4. redo the ls command to show that output.txt was created,
      5. and cat the output of your new personalized output.txt.

    Grading

    • Part 1: lab06p1.txt
      1. 50 points for correctness/style, of Rectangle.h
      2. 50 points for correctness/style, Rectangle.cpp (or Rectangle.cc)
      3. 75 points for your main programs and testing.
    • Part 2: lab06p2.txt:
      1. cat/compile/run modified version of program: 20 pts for doing it, 30 points for program correctness/style.
      2. cat input/output files for that step: 20 pts
      3. .html and .txt files on the web: 20 pts
    • Part 3: lab06p3.txt:
      1. creating the foo.txt file 30 pts
      2. cat/compile/run of modified version of program: 30 pts for doing it, 30 points for correctness/style.
      3. cat input/output files for that step: 20 pts
    • Part 4: 25 points (15 for directory and file being readable, 10 points for no other files hanging out in that directory.
    • Part 5: lab06p5.txt, 25 points for following instructions.
    • Total points: 425

    Phillip T Conrad
    Last modified: Thu Apr 8 22:12:05 EDT 2004