This assignment does not require the use of classes. Classes will be covered in project 3.
The original version of this assignment involved developing a program that edits a file of NCAA football teams. However, in order to make the assignment more interesting and personal, I am going to ask each of you to choose his/her own "topic" for the assignment.
You will be writing a menu-driven program that manipulates a file of data related to a given topic. You'll be able to add, delete and lookup items in that list, as well as read the list from a file, and write the list to a file.
In this part of the project, your job is to understand the big picture of what you are trying to accomplish, choose a topic, and post some general information about that topic on the web. I have put together sample scripts of a completed project involving the topic "NCAA teams." Your job is to review some sample output so that you know what you are aiming to accomplish.
The program provides the user with a menu of options for maniuplating the file. The menu allows the user to
You will develop the program as a menu driven program, and you are strongly encouraged to develop it in seven stages. (In fact, when I wrote the program, I wrote it in exactly the stages that I am recommending to you. It took me 3.5 hours to write it from start to finish, so I strongly encourage you not to wait until the day before to start, but rather, to start now, and try to do one stage per day until you are finished. If you keep pace with me, you can do each stage in about 30 minutes; more likely, it may take you a couple of hours for each stage.
So that you can get an idea of what the program is supposed to do, I have put an example script file for each stage in the following directory on strauss:
~pconrad/public_html/cisc181/05S/work/proj/proj2/sampleScriptswhich can be found on the following web link:
http://copland.udel.edu/~pconrad/cisc181/05F/work/proj/proj2/sampleScripts
There is also some sample code in the directory:
~pconrad/public_html/cisc181/05S/work/proj/proj2/structExamplewhich can be found on the following web link:
http://copland.udel.edu/~pconrad/cisc181/05F/work/proj/proj2/structExample
To finish part 0 of the project, you are going to describe a table of data related to the "topic" that you have chosen. Rather than give a detailed description of what I'm looking for, it is probably more effective to just show you several examples first.
Here are a few example tables for various topics. What you need in each case is at least two "fields" (i.e. two columns) that contain character data, and at least two that contain numeric data of some kind. It is best if you can come up with one "real number" and one "integer", but it isn't always possible (for example, I had trouble coming up with a "decimal" value for Supreme Court Justices.) So, if you come up with two integer fields, that's acceptable.
Pizzas with Side Salads (as in the inputExamples programs)
| pizzaTopping | numPizzas | priceOfPizzaInDollars | saladDressing |
|---|---|---|---|
| cheese | 2 | 4.99 | ranch |
| mushroom | 1 | 5.99 | italian |
| green peppers | 3 | 5.99 | blue cheese |
| onions | 1 | 4.99 | thousand island |
Hiking Trails
| nameOfTrail | park | length | state | timesHiked |
|---|---|---|---|---|
| South Kaibab | Grand Canyon NP | 6.3 | AZ | 1 |
| Hidden Pond | Brandywine Creek SP | 2 | DE | 4 |
| Bright Angel | Grand Canyon NP | 9.3 | AZ | 1 |
| Kelly's Run | Holtwood Recreation Area | 4 | PA | 1 |
Supreme Court Justices
| Name | Appointed by | Year Appointed | Year Born |
|---|---|---|---|
| John Robert, Jr. | Bush (George W.) | 2005 | 1955 |
| Stephen G. Breyer | Clinton | 1994 | 1938 |
| Ruth Bader Ginsburg | Clinton | 1993 | 1933 |
| Clarence Thomas | Bush (George H.W.) | 1991 | 1948 |
| David H. Souter | Bush (George H.W.) | 1990 | 1939 |
| Anthony Kennedy | Reagan | 1988 | 1936 |
| Antonin Scalia | Reagan | 1986 | 1936 |
| Sandra Day O'Connor | Reagan | 1981 | 1930 |
| John Paul Stevens | Ford | 1975 | 1920 |
Once you've come up with the "columns" for your table, and at least three sample "rows" for your table, you are ready to do the part that you are going to submit for credit. You should do the following commands.
(Note: If you are working on a PC, leave off the & on the emacs proj2data.txt command; include it only if you are working on a Sun Ray or other system with X Windows installed, such as a Linux system, and certain Macs and PCs with special software installed.)
mkdir -p ~/public_html/cisc181/proj2
cd ~/public_html/cisc181/proj2
emacs proj2data.txt &
Inside the file proj2data.txt, you want to document the columns in your table. Here are a couple of examples. The way I've formatted things here isn't the "only" way or the "right" way—this is a file written in English, not computer code, and it will be read by human beings, not a computer program. So format things however you like, as long as the way you write it is clear.
When you are finished writing up your proj2data.txt file, do the following sequence of commands to make it readable on the web:
chmod a+x ~
chmod a+rx ~/public_html
chmod -R a+rx ~/public_html/cisc181
Then, you should be able to see the file at the link below (if you substitute your username for "yourusername"). You should be able to click on it and open it in a web browser:
http://copland.udel.edu/~yourusername/cisc181/proj2
Your TA will grade it based on what is there after you've submitted the other part of your project.
P. Conrad, cisc181 Section 22, proj2data.txt Topic: Hiking Trails column 1: hiking trail name type of data: character maximum length: 20 can contain blanks: yes column 2: location type of data: character maximum length: 30 can contain blanks: yes column 3: lengthOfTrail type of data: float minimum value: 0 maximum value: 9999.9 units: miles decimal places to show: 1 calculation: show length of trail in km as well. decision: if trail is < 5 miles print short, otherwise print long. column 4: state type of data: character maximum length: 2 can contain blanks: no comments: I'll either avoid trails that are in multiple states, or I'll divide up multi-state trails (such as the Appalachian Trail) into segments, one per state. column 5: timesHiked type of data: int minimum value: 0 maximum value: 9999 |
P. Conrad, cisc181 Section 22, proj2data.txt Topic: Supreme Court Justices column 1: justiceName type of data: character maximum length: 30 can contain blanks: yes column 2: appointedBy type of data: character maximum length: 30 can contain blanks: yes column 3: yearAppointed type of data: int minimum value: 1776 maximum value: 9999 units: years column 4: yearBorn type of data: int minimum value: 0 maximum value: 9999 units: years |
P. Conrad, cisc181 Section 22, proj2data.txt
Topic: Digital Cameras
column 1: brand
type of data: character
maximum length: 20
can contain blanks: yes
column 2: model
type of data: character
maximum length: 20
can contain blanks: yes
column 3: megapixels
type of data: float
minimum value: 0
maximum value: 99.9
units: megapixels
decimal places to show: 1
column 4: price
type of data: double
minimum value: 0
maximum value: 9999.99
units: dollars
notes: print dollars and cents separately, separated by .
|
Consult the sample scripts for advice on what to test. You are encouraged to test each stage of the program as you develop, rather than trying to develop the whole thing at once.
At each stage, you should have a working complete program that you could script and turn in for partial credit. However, in the end you should only turn in one program.
It is best to read over the entire assignment before starting to code part 1, so you have some idea of where we are going. But don't let the size or complexity overwhelm you; working step by step, one step per day, you'll have no trouble finishing on time if you start NOW. With that said, let's begin.
The remainder of this project is written as if you were doing the NCAA teams as your topic. It is part of your job to make the necessary intepretation of the directions to apply them to your topic. Some things to note in particular:
Main Menu:
a: add team to list
l: list teams
q: quit program
s: summarize list
The main is therefore very short, and mainly consists of a loop that
reads options from the user until the user selects the 'q' option. You
can use a switch statement. The default case should print an error message
indicating that the option is not supported. Print the menu of options
each time through the loop.
Each of these options will call a function that will perform some task. (Even the 'q' option might perform some task before finally quitting.) For example, the 'a' option will call a function called something like:
void addTeamToList(Team **teamListHeadPtr, Team **teamListTailPtr);This function will
NOTE: the description of the format of the that appeared here has been moved to step 5 where it really belongs. You don't really need that description until you get to step 5.
The linked list will consist of struct Team nodes, which
each node contains the following fields.
domainName (domain name from main website for this college or university) schoolName (name of the college or university) city state foundingYear next (pointer to the next field)You should declare variables in your main program to point to the head and tail of a list of teams, such as the following:
Team *teamListHead; Team *teamListTail;These variables should be local variables to your main program not global variables (a serious deduction (at least 10%) will be made if you use global variables instead.)
You will pass these variables (the ones for head and tail of the team list) into the function readTeamFile. You should pass these variables by reference; that is, you will pass the address of these variables,
as follows:
...
switch (menuOption)
{
case 'a':
addTeamToList(&teamListHead, &teamListTail);
break;
...
Inside the function, you should dereference these pointers when
assigning values to them. For example, when assigning a value to
teamListHead, you should use code such as the following. Note that the
name of the variables has "ptr" on the end of it to remind you that
inside the function, these variables are not the head and tail of the list,
but rather, they point to the head and tail of the list.
void addTeamToList (Team **teamListHeadPtr, Team **teamListTailPtr)
{
...
Team *p;
p = new Team; // allocate a new node
// set fields inside p based on values read from user, then do...
if ((*teamListHeadPtr) == NULL) // if first node in list
(*teamListHeadPtr) = p; // set the head of the list
else
(*teamListTailPtr)->next = p; // link new node to end of the list
(*teamListTailPtr) = p; // update the tail pointer
...
}
The other options should do the following:
Partial Credit: If this step is as far as you get, it is worth up to 40% partial credit (you start with a grade of 40%, and additional deductions may be taken from there). If you stop here, script this program. Be sure you test all the options that are present at this stage (see the script file p2s1.txt for ideas.
Your script file should also be called p2s1.txt if you stop at this stage.
If you are continuing on with the remaining parts, you do NOT need to submit a script or C++ program for this step.
Add one additional option to the program.
The "f" option "finds" a team in the list. Prompt the user for a domain name, then search the list for all teams that match that domain name. Note that it is possible for the list to contain duplicate entries. See the script file p2s2.txt for example output.
Start by making a copy of your p2s1.cc or p2s1.cc program with, for example:
cp
p2s1.cc p2s2.cc
Then add the necessary code to implement the new option.
Partial Credit: If this step is as far as you get, it is worth up to 50% partial credit (you start with a grade of 50%, and additional deductions may be taken from there). If you stop here, script this program. Be sure you test all the options that are present at this stage (see the script file p2s2.txt for ideas.
Your script file should also be called p2s2.txt if you stop at this stage.
If you are continuing on with the remaining parts, you do NOT need to submit a script or C++ program for this step.
Add one additional option to the program:
The "d" option deletes a team from the list. It will prompt the user to enter a domainmame. It will then search the list for the first node that matches that domain name, and delete that node from the list.
From here on out the drill is the same for each additional step (except the last one)... for each step, you add one additional option to the program, and get a bit more credit. At each step, copy your file with a command such as:
cp p2s2.cc p2s3.cc
Then add the necessary code to implement the new options. (This is the last step where I'll spell out these specific instructions
Partial Credit: If this step is as far as you get, it is worth up to 60% partial credit (you start with a grade of 60%, and additional deductions may be taken from there). If you stop here, script this program. Be sure you test all the options that are present at this stage (see the script file p2s3.txt for ideas.)
(From here on out, you'll be expected to just follow the pattern; I'll just say "this step is worth x% partial credit, and the script file should be called p2sn.txt, where n is the step number.
If you are continuing on with the remaining parts, you do NOT need to submit a script or C++ program for this step. Only submit the script for the step at which you finish.
Suggestion: You should read the values into a character array (using
cin.getline()) and before assigning the new value, check if the string
entered is blank (using strcmp()). Compare against the empty string "". Ask in lecture if you are not sure how to do this.
Use a character array even for the integer
foundingYear, and then use atoi() to do the conversion. If you have a field with a decimal point represented as a double (use double, not float), then use atof() instead of atoi(). Ask in lecture if you are not sure what to do.
See previous stages 1-3 for guidance on how to name your program, script file, etc. and what to turn in if you stop at this step.
In this step, you will add an option to allow the user to read teams into the linked list from a file.
The file will have the same format as the files in the directory proj2, which contains several files teams1.txt, teams2.txt, etc., all of which contain fields separated by ":". For your project, you may use comma separated data, or : separated data; it is your choice.
For hints on how to use strtok to read colon or comma separated data, follow the example code from the strtokExample subdirectory, which reads a file separated by commas. To use colons insetad, simply change "," in the second parameter of strtok to ":"
~pconrad/public_html/cisc181/05F/work/proj/proj2/strtokExample
http://copland.udel.edu/~pconrad/cisc181/05F/work/proj/proj2/strtokExample
After you've figured out how to handle the strtok part, add the "r" option to your program. This option prompts the user to enter a file name.
It then opens that file as an ifstream; if the file can't be opened, just print an error message and return from the function to the main menu (don't exit the program.)
If the file is opened successfully, read all records from the file (which contains colon separated values, similar to the files teams.txt, and udel.txt; these can be found in the proj/proj2 directory cited above). For each line you read from the file, allocate a new Team node. Read each line from the file into a single character array, then use strtok() to break up the fields and store each one in its proper place in the new node. Then, add that new node into the linked list.
You'll need to create a data file or two. I suggest you create at least three data files:
Name your data files after your theme, e.g.
Some sample data files for the NCAA team project (in colon separated format) are in:
http://copland.udel.edu/~pconrad/cisc181/05F/work/proj/proj2/sampleData/
See previous stages 1-3 for guidance on how to name your program, script file, etc. and what to turn in if you stop at this step.
It then opens that file as an ofstream; if the file can't be opened, just print an error message and return from the function to the main menu (don't exit the program.)
If the file is opened successfully, write all the records from the linked list into an output file, separated by colons, in the same format as the input files teams.txt, and udel.txt; these can be found in the proj/proj2 directory cited above).
The idea is that the output file you create should be able to be read back into the program as an input file with the r option on the same run, or a later run. You should test this in your script.
See previous stages 1-3 for guidance on how to name your program, script file, etc. and what to turn in if you stop at this step.
When the user types quit, print a message warning the user if there are any unsaved changes. If there are NOT unsaved changes, the message should not be printed. If there are unsaved changes, ask the user if they really want to quit without saving. If the answer is yes, go ahead and quit. If the answer is no, return to the main menu.
Hint: Add a variable in your main program that keeps track of whether or not the list has been modified since the last time it was written to a file. Start the variable as false. Every time the user invokes an option that modifies the list, set the variable to true. Every time the user writes the file out to the disk, set the variable to false. Then pass this variable into the quit option.
For this stage, be sure your script tests all the options involved in the program. Also, if you reach this stage, name your program and your script file proj2.cc and proj2.txt.
You do not need to create a Makefile or a tar file to submit this assignment; all your code will be in one C++ source file.
cat any data files you read or create, and to do an ls command before and after you create. | Step | Filename | Menu options | comments | points | cumulative |
|---|---|---|---|---|---|
| 0 | proj2data.txt | N/A | available on website, at least two char fields, at least two numeric, doesn't duplicate another's topic) |
10 | 10 |
| final script |
p2sn.txt | N/A | scripting per instructions | 20 | 40 |
| all | N/A | N/A | following directions | ||
| 1 | p1s1.cc | a(dd), l(ist), q(uit), s(ummarize) | style and correctness | 10 | |
| 2 | p2s2.cc | f(ind) | style and correctness | 10 | 50 |
| 3 | p2s3.cc | d(elete) | style and correctness | 10 | 60 |
| 4 | p2s4.cc | u(pdate) | style and correctness | 10 | 70 |
| 5 | p2s5.cc | r(ead from file) | style and correctness s | 10 | 80 |
| 6 | p2s6.cc | w(rite to file) | style and correctness | 10 | 90 |
| 7 | p2s7.cc | smarter quit | style and correctness | 10 | 100 |