This is the last lab of the semester. This lab will provide practice using vectors, structs, and modifying classes. Change into your "cps6" directory using the "cd" command and create another directory called "lab9" using the "mkdir" command. Change into the "lab9" directory. If you did this correctly, when you type "pwd" you should see a long path name that ends with "/cps6/lab9"
In order to do this lab, you need to copy some files using the following "cp" (for "copy") command (don't forget the trailing period, or dot):
cp ~dr/cps6/lab9/* .
You'll need to link to datafiles by typing the following command:
ln -s ~dr/cps6/data data
If you type "ls" to list the files now in your lab9 directory, you should see the following files: Makefile, data, smallcd.dat, cdlist.h, cdlist.cc and usedb.cc.
For each of the programming problems that follow, you should use the style rules discussed in class, which includes indentation, meaningful variable names and comments at the top of the file and for each function.
The program you will develop comes in two parts: the client program usedb.cc and the class implementation cdlist.cc. The client program maintains a data base of CD's. Users of this program can add a new CD to the collection maintained by the program, find all CD's by a given group/artist, and find all CDs with any given word in the title of the CD. For example, running the program and specifying the artist name ``Fish'' might yield the groups Hootie and the Blowfish and Country Joe and the Fish (if both are in the CD database).
You will need to add options for the user of the program (in usedb.cc) and implement member functions of the class CDlist declared in cdlist.h.
The program usedb.cc is partionally functional. Compile the program by typing: make usedb
Run this program and enter the following
> usedb [a] add [t] title search [g] group search [p] print [r] read [q] quit enter choice: r name of input file: smallcd.dat total of 22 CD's [a] add [t] title search [g] group search [p] print [r] read [q] quit enter choice: p 100001 $15.98 Najee Day By Day 100006 $15.98 Alpert, Herb Under A Spanish Moon ... 100127 $11.98 Skid Row B-Side Ourselves [a] add [t] title search [g] group search [p] print [r] read [q] quit enter choice: t title to look for: the warning, TitleSearch not implemented [a] add [t] title search [g] group search [p] print [r] read [q] quit enter choice: g Unrecognized option: g [a] add [t] title search [g] group search [p] print [r] read [q] quit enter choice: q thanks for using our CD database
It does implement some operations, you can read in from a file and print the list of CD's, but you can't do a title search, a group search or an add because the CDlist class isn't fully functional.
Before attempting part 1, take a look at the three files cdlist.h, cdlist.cc and usedb.cc.
In cdlist.h you'll see the definition of a CD,
struct CDinfo
{
int id_number; // as assigned by BMG
string title; // title of CD, e.g., Workingman's Dead
double price; // to buy CD
string group; // artist/grop, e.g., Grateful Dead
};
and you'll see the class definition of CDlist (a partial description is below). CDlist stores the CD's in a vector of type CDinfo and allows one to iterate over the CD's.
class CDlist
{
public:
CDlist();
CDlist(int size);
// for traversing/accessing list
void First(); // reset current to front of list
void Next(); // advance current to next item in list
bool IsDone(); // return true iff current past end of list
CDinfo Current(); // return current item in list
int Size(); // return # items in list
bool IsFull(); // return true iff # items = capacity
void SetSize(int size); // set capacity of list to size
void Add(const CDinfo & cd); // add cd to list (at end)
void DeleteAll(); // delete all items from list
void Print(); // print items in list
// search for artist/title matches
void ArtistSearch(const string & name, CDlist & match);
void TitleSearch(const string & name, CDlist & match);
private:
Vector<CDinfo> myList;
int myCount; // how many cd's stored
int myCapacity; // how much room there is
int myIndex; // current element of list
};
In the file cdlist.cc, modify the function CDlist::TitleSearch to search for a title. This is the only function you should modify for this part of the lab. In the file usedb.cc the function DoTitle already calls this function, so you do not need to modify anything in the file usedb.cc.
The TitleSearch function receives a name to match, and a CDlist named match that is passed by reference. This function should add all titles that have this name in their title to the CDlist match, which is initially empty. Then DoTitle will call Print to print out all the CD matches put into match.
Since you are modifying a function in the class CDlist, you can search the Vector myList, which is in the private section of the class.
You should look for partial matches as described in the header file cdlist.h and below. Searching for the title ``ell'', for example, would match 1) Hello, Dolly/Original Cast and 2) Jelly's Last Jam/Orig. Cast Recording. All the CD's for these entries would be added to the parameter match.
To facilitate matches you can use three string functions: Contains, Downcase, and substr.
The prototype for Contains is given below.
int Contains(const string & s);
// postcondition: returns 1 (true) iff string contains s as substring
It is used as follows:
string s = "daydream";
if (s.Contains("dream")) cout << "yes 1" << endl;
if (s.Contains("ayd")) cout << "yes 2" << endl;
if (s.Contains("Day")) cout << "yes 3" << endl;
The first two statements evaluate to true (1), the third call of Contains returns 0 (false) since s does not contain "Day".
The member function Downcase returns a lowercase version of a string. The statements below print "hello".
string s = "HeLLo";
cout << s.Downcase() << endl;
The prototype is void string Downcase();. Note that this function DOES NOT change the string it is applied to, but returns another string that is a lower-case version.
The function substr has prototype void substr(int pos, int len), it returns a string of len characters starting with character pos (recall that the first character has index 0).
For example:
string s = "bloodstream";
cout << s.substr(0,5) << endl;
cout << s.substr(5,6) << endl;
cout << s.substr(2,3) << endl;
would print, on three separate lines, ``blood'', ``stream'', and ``ood''. If the parameter len is too big, only as many characters as are in the string are taken. If pos is too small or too big, the empty string is returned.
Careful If a string matches a title in more than one place, only add the CD to the match CDlist once. For example, if the title to match is "day", then the title "Day by Day" would match in two places, but you don't want to list the same CD twice.
Compile and run your program. Make sure it is correct before moving on to part 2. You can test it on the small database smallcd.dat, and also on a much larger file with over 3000 CD's, data/cd.dat
Modify the program to find partial matches of artists. Searching for the artist ``ro'', for example, would match 1) Palmer, Robert and 2) Skid Row.
For this part, you'll have to make several modifications to the program.
You can run your program, put it won't match artists yet, because you'll need to also make changes in cdlist.cc.
NOTE: Even though you just modified cdlist.cc, you DO NOT type make cdlist to compile the program, because cdlist.cc does not contain the main function, but rather is a separate file containing a class that the main program usedb uses. The Makefile you are using figures out that you have made a change in the file cdlist.cc and recompiles it first, before recompiling usedb.
Implement the DoAdd function in the file usedb.cc to allow the user to type in a new CD that is added to the database. Do not add the CD if the ID number is already in the database, inform the user that the ID number is incorrect.
For this part of the assignment, you only need to modify the DoAdd function in usedb.cc. You do not need to modify cdlist.cc.
The DoAdd function currently asks the user to type in a new CD. Note that the input is read in with getline, and not cin. You cannot mix using getline and cin in the same program as they process the input stream differently.
You need to add code to create a new CD, iterate over all the current CD's (see cdlist.h for the iterator functions) to make sure the id number of the new CD is not already in the database, and if not, then add the CD to the database. If the id number is already in the database, inform the user.
You can test your program by adding your CD to the database and then searching for it.
Careful. Notice that each time you read in a list of CD's the old list is deleted. So if you add a new CD, and then use the read option, the CD you entered will be lost.
~dr/bin/submit6 lab9 README usedb.cc cdlist.cc cdlist.h
You should receive a message telling you that the programs were submitted correctly.