\documentstyle{article} \input{/u/ola/lib/wide} \input{/u/ola/lib/para} \begin{document} \begin{center} \Large \bf CPS08: CD reading/Strings/Streams \end{center} You should work on this assignment on mentor. It is on the acpub system as well, but you should get used to working on mentor during lab. It would be prudent, however, to back things up by ftp-ing files to your acpub account. If you can't log into mentor, get help NOW! \subsection*{Logging in to mentor} \begin{enumerate} \item From an xterm window type {\tt telnet mentor.cs.duke.edu} to connect to the machine mentor. You'll be prompted for your login and password so you should supply these. If you haven't changed your password from the default given you, change it now. To do this, type {\tt passwd}. You'll be prompted for your old password and your new password. After changing your password you should run the command \verb!~ola/setup! (just type that). This will set up your files so that you can work more easily. You only need to run the set program once. \item Change into your {\tt cps08} directory and create a {\tt lab7} subdirectory. Then change into this and copy files by typing what's below (don't forget the trailing dot). \begin{verbatim} cp ~ola/cps08/lab7/* . \end{verbatim} If you type {\tt ls} you should see the following files: {\tt Makefile}, {\tt cd.dat} and {\tt cdstuff.cc}. \item You need to give your machine permission for mentor to connect to it. To do this type {\em in an xterm on the DEC station} NOT in a mentor xterm the following: \begin{verbatim} xhost +mentor.cs.duke.edu. \end{verbatim} Do NOT put a space after the + sign. \item You should then be able to type {\tt emacs \&} to get an emacs window. You want to use the ampersand \& which leaves you with control of your xterm (so you can run programs from it). If this doesn't work, skip to the description of {\bf Acpub Alternative} below for completing the lab on the acpub machines. You should check with a TA since all students should have working mentor accounts. \end{enumerate} \subsection*{Acpub Alternative} Change into your {\tt cps08} directory using the {\bf cd} command and create another directory called {\bf lab7} using the {\bf mkdir} command. Change into the {\bf lab7} directory. If you did this correctly, when you type {\bf pwd} you should see a long path name that ends with {\bf /cps08/lab7.} You need to copy some files to do this lab. The {\bf cp} command is used for copying files on Unix systems. Type the following command to copy files for the lab (don't forget the trailing period, or dot): \begin{verbatim} cp ~ola/cps08/lab7/* . \end{verbatim} This command will copy all the files in the directory \verb!~ola/cps08/lab7! into your {\tt cps08/lab7/} directory. If you type {\tt ls} you should see the following files: {\tt Makefile}, {\tt cd.dat} and {\tt cdstuff.cc}. \subsection*{Lab} Use the emacs commands {\tt C-x C-f} to find/load the file {\sl cdstuff.cc}. Then compile this command using {\tt C-x c} and type {\tt make cdstuff} in the minibuffer. In the current version, you'll be prompted for the name of a group. The names of all CD's by the artist you type will be printed as well as the total price of all the CD's. When testing the program, you must type the name as it's stored in the file {\tt cd.dat} --- in particular this means that to see titles by the B-52's youll need to enter the name as ``\verb!B-52's, The!'' (no quotes) since this is how the name is stored in the file. You can check on ``Grateful Dead'', ``Prince'', ``R.E.M'', ``Tone-Loc'', and many other artists. Currently, however, you need to run the program each time the database is searched. You are to change this so that the user can query the database many times while the program is running. \subsubsection*{Re-reading the file} To do this, you'll need to put the call to {\tt Lookup} inside a loop. Prompt the user for a group name, and use some special name to indicate that no search will be done by the program (the program will stop). You might type \verb!"END"!, for example, to indicate that the program is finished. In any case, you need to prompt the user. The prompt should indicate what sentinel string is used to indicate that the program is over. You'll also need to clear the stream since all the entries in the stream are processed each time that {\tt Lookup} is called. In particular, none of the calls of {\tt GetLine} will succeed after the file is read completely the first time, because they failed when the file was exhaustively read the first time. To fix this, you'll need to use two stream member functions to clear the stream and reset it to the beginning. \begin{verbatim} input.clear(); // clear any failed flags input.seekg(0); // reset to beginning of stream \end{verbatim} You should call BOTH of these to reset the stream to a ``good'' state so that all of the database can be read. The best place to do this is either before the call to {\tt Lookup} or as the first statements inside {\tt Lookup}. The function {\tt seekg} is called that because it ``seeks to get'' a new location. \subsubsection*{Comparing Strings} You should also write a function with the prototype below. \begin{verbatim} String Tolower(String s); // postcondition: returns lowercase equivalent of s \end{verbatim} that returns a string that is equal to s, except that all uppercase letters have been converted to lowercase. For example, the statement \begin{verbatim} cout << Tolower("HaPpy Days!") << endl; \end{verbatim} should print ``{\tt happy days!}'' since uppercase letters are converted to lowercase (other letters are unchanged). To do this you'll need to loop over all the characters of s, and you'll need to use the function {\tt tolower} in \verb!!. For example: \begin{verbatim} String result = ""; // empty string to start for(...) { result += char(tolower(s[k])); } return result; \end{verbatim} is part of the code in {\tt Tolower(String)}. The cast to a char isn't necessary, but it's best to do this with {\tt tolower} since it returns an int. Then you should use {\tt Tolower} in the function {\tt Lookup} \begin{verbatim} if (Tolower(group) == Tolower(name)) \end{verbatim} As a check that everything works, find the total value of all works by \verb!"various artists"! (it's a lot). \subsection*{Heart} If you have more time in lab, you should start working with the heart monitor program so taht you can ask questions of the TAs. \subsection*{Submission} {\bf To turn in programs}: When your modified programs works, you can submit it, along with a README describing how long it took you and what your impressions of the lab are. Submit works on the DEC stations and on mentor. \begin{center} \tt submit08 lab7 cdstuff.cc README \end{center} The README file should include your name, how long you worked on the program, and anyone you received significant help from. You should also include any comments you have about the lab. You should receive a message telling you that the programs were submitted correctly. You should remove (the command is rm) the files {\tt cd.dat} and {\tt cdstuff}. DON't remove the .cc file. \end{document}