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.
- Section 5.12 in Deitel/Deitel (C-style strings)
- Section 12.1 through 12.4 in Deitel/Deitel (Stream I/O with C-style
strings, using
cout and cin with C style strings, get, getline,
ignore, etc.)
- Section 14.1 through 14.6 in Deitel/Deitel (Sequential-access file
processing, that is,
ifstream and ofstream).
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/cisc181/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 Lab Exercise 2 from your lab manual pp.239-243. You'll find the three source files you need (date.h, datem.cpp, and date.cpp) in the lab06 subdirectory you copied in Part 0.
You do NOT need to
complete the "follow-up questions and activities", though it won't hurt
you to look them over.
When you are done with all the instructions on pp. 239-243 (except the "follow-up questions and activities"), make a script lab06p1.txt where you cat your modified date.h, datem.cpp and date.cpp files, compile them, and run them.
NOTE ADDED 4/9/2004: The file datem.cpp already has some basic error
checking, as illustrated by the following example from the Date::setDay(int d) member function:
day = (d <= monthDays() && d >= 1) ? d : 1;
However, as I may have mentioned in lecture, I'm not particularly fond of this
form of error checking. I'd like you to replace with with something more
of the form:
if (d <= 0 || d > monthDays())
{
cerr << "Illegal value for d passed into Date::setDay(): " << d << endl;
exit(-1);
}
Please make that change everywhere it is needed in the datem.cpp file. That is worth the "50 points" for fixing the "error checking on initializer values".
Part 2a: Reading from a file until cin.eof()
- Copy the program
readUsernames.cpp from the lab06
subdirectory of the labs page on the course web site.
- Open up the file with a text editor, and notice the structure of the code.
What the program is doing is reading a series of usernames, and for each username
that it reads in, it writes out a URL (Uniform Resource Locator, that is,
a web address) that you can use to access that user's collegeDat.txt file.
However, rather than reading until we hit a "sentinel" value, or
until we've read a specific number of usernames, we use the cin.eof() function.
This function returns "true" when we have hit the "end of file"
on cin.
When running an interactive program (one where a human user is interacting
with the program, providing input and reading output), the user can signal
the end of file by typing "CTRL/D". So, essentially, this program
reads a line of input, and then repeateadly outputs URLs until the user puts
in CTRL/D.
- Compile and run this program, placing your output in the file readUsernames1
(rather than a.out). If you are not sure how to do that, consult your Anderson
book, the chapters on compiling C++ programs!).
- Try "pconrad" as the userid. Then, try your own userid, and those
of some of the classmates sitting near you in lab, or any other usernames
that you remember of CISC181 students. (Oh, you aren't working on this in
lab? Then make it a point to learn the usernames of some of your classmates
next time you come to lecture or lab!) When you've typed in a few userids,
hit CTRL/D to stop the program.
- Make sure your collegeDat.txt file can be read via the URL given by
the output of the program. If it can't, then now is your chance to
fix it up! If it doesn't work, you already lost points on an earlier
lab, and might lose points on a future lab as well!
- You'll script this program in part 2b, so you are done with
Part 2a.
Part 2: Using redirection ( < ) to read standard input (cin) from a file
- Now, copy the file "usernames.txt" into your directory from the
lab06 subdirectory.
- Add your own username to this file, and also, add at least four additional
usernames of classmates of yours.
- Run the readUsernames program again, with the command line:
./readUsernames < usernames.txt
This will cause the program to take its standard input (cin) from usernames.txt
instead of from the keyboard. Notice that the output still contains all the
prompts for the input, which is a bit messy.
- Now, copy the program
makeHTML.cpp from the lab06 subdirectory.
Edit it, and look over the source code. In this program, I've taken out all
the prompts for input, and replaced all the output with "cout <<"
statements that will produce HTML code. The result is that when you run the
program with
./makeHTML < usernames.txt
you will get as output something that is suitable to be the HTML code for
a simple web page. You can store this into an HTML file via:
./makeHTML < usernames.txt > usernames.html
The "> usernames.html" part of the command line
is a shell feature that says "take the standard output (the cout stuff)
and put it into a file called usernames.html". Use "cat usernames.html"
to look at the contents of the usernames.html file.
- Now take your file "usernames.html", and copy it to a new subdirectory
under your public_html directory, called:
~userid/public_html/cisc181/files/lab06/usernames.html
and then browse it via http://copland.udel.edu/~userid/cisc181/files/lab06/usernames.html.
(Replace "userid" with your username (e.g. pconrad). You'll need
to do the "chmod"command, and use the proper "cp" command
before your file will be available on the web. You will also need to use an
"mkdir"command. . Check your old labs or your Anderson book for
information on how to do all that. Then, browse the page you created, and
check that the links work. If your own link doesn't work, then fix it! (This
is a chance for a do over of lab00.)
- Make a script lab06p2.txt in which you show the following steps:
- Compile readUsernames.
- Run readUsernames, inputting 3 usernames interactively, then typing
CTRL/D.
- cat your usernames.txt file to show that you added a few extra names
to it.
- Run readUsernames, redirecting the standard input from usernames.txt
(as shown above).
- Compile and run makeHTML, redirecting the standard input from usernames.txt,
and redirecting the output to usernames.html. (Note: you may have to delete
the old copy of usernames.html to make this work properly.). Cat the
usernames.html file afterwards to show that its contents.
- Now, create a directory under your public_html called:
~/public_html/cisc181/files/lab06/
and copy the "usernames.txt"
and "usernames.html" files from part 2 of this lab into that same directory.
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 that makes that file readable. If you don't remember it, check your notes, and check previous labs (and learn it this time!) Then, check the following URL (substituting your own userid) to make sure that each of the files is readable:
http://copland.udel.edu/~userid/cisc181/files/lab06
http://copland.udel.edu/~userid/cisc181/files/lab06/usernames.txt
http://copland.udel.edu/~userid/cisc181/files/lab06/usernames.html
Part 3: 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.
- 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:
-
"ifstream" is the name of a class for an "input file stream" object.
- "userInputFile" is an object of type "ifstream";
it can be used in place of "cin" as a stream to read data from.
- When we declare "userInputFile" as an object of type "ifstream", we
invoke the constructor for the ifstream class, passing in the string
"usernames.txt".
- Invoking this constructor sets up the object "userInputFile" to refer
to the file "usernames.txt".
- When you run this program, the code
will look for a file named "usernames.txt" in your current directory
on strauss. Operations such as
userInputFile >> x
will
look in the file "usernames.txt" for input instead of looking for that input
from the keyboard.
- Run the 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.
- 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 the previous step.
- When you are finished, use
Unix redirection to send the output to a file called "usernames2.html". To
see if the HTML code created is legal HTML code, copy the file to the same directory
as in the previous step, and use a web browser to examine the output.
- Script this program as (lab06p3.txt) (cat, compile, run). Be sure to cat both the
input and output files as well.
- 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 4: Reading from an input file with multiple items per line
- 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.
- 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.).
- 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.
- 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
- Make a script lab06p4.txt in which you cat your modified "readFootballSched" program and your "foo.txt" file, compile the program, and run the program.
Part 5: 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 they 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.
Grading
- Part 1:
- 50 points for correctness/style: error checking on initializer values
- 50 points for correctness/style: member function "nextDay"
- 65 points for main, testing, scripting, etc.
- Part 2: lab06p2.txt:
- cat/compile readUsernames interactively: 10 points.
- run readUsernames with input from user: 10 pts
- cat the usernames.txt file: 10 pts
- run readUsernames with redirection from file: 10 pts
- cat/compile/run makeHTML: 30 pts
- cat the .html and .txt files: 20 pts
- .html and .txt files on the web: 20 pts
- Part 3: lab06p3.txt:
- cat/compile/run modified version of program: 20 pts for
doing it, 30 points for program correctness/style.
- cat input/output files for that step: 20 pts
- .html and .txt files on the web: 20 pts
- Part 4: lab06p4.txt:
- creating the foo.txt file 30 pts
- cat/compile/run of modified version of program: 30 pts
for doing it, 30 points for correctness/style.
- cat input/output files for that step: 20 pts
- Part 5: 25 points (15 for directory and file being readable, 10 points for no other files hanging out in that directory.)
- Total points: 500
Phillip T Conrad
Last modified: Tue Apr 13 23:31:11 EDT 2004