CPS08: Lab6 Characters, Codes, and Streams 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 (more on this later). If you can't log into mentor, log into an acpub machine and do your work there. ***** Logging in to mentor 1. From an xterm window type 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 passwd. You'll be prompted for your old password and your new password. After changing your password you should run the command ~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. 2. Change into your cps08 directory and create a lab6 subdirectory. Then change into this and copy files by typing what's below (don't forget the trailing dot). cp ~ola/cps08/lab6/* . If you type ls you should see the following files: Makefile and decode.cc. 3. You need to give your machine permission for mentor to connect to it. To do this type in an xterm on the DEC station NOT in a mentor xterm the following: xhost +mentor.cs.duke.edu. Do NOT put a space after the + sign. You should then be able to type 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 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. ********** Acpub Alternative Change into your cps08 directory using the cd command and create another directory called lab6 using the mkdir command. Change into the lab6 directory. If you did this correctly, when you type pwd you should see a long path name that ends with /cps08/lab6. You need to copy some files to do this lab. The 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): This command will copy all the files in the directory ~ola/cps08/lab6 into your cps08/lab6/ directory. If you type ls you should see the following files: Makefile and decode.cc. ***************Lab Use the emacs commands C-x C-f to find/load the file decode.cc. Then compile this command using C-x c and type make decode in the minibuffer. In it's current version, the file just echoes its input which it reads and writes one character at a time. You should run it, and type some characters to see what happens. To signify that you're done, type the control-D, the character used to signify end-of-file. You can also use I/O redirection to read from a file rather than from the standard input stream. Type decode < decode.cc and you should see the file decode.cc being echoed. You can type decode < ~ola/misc/data/poe.txt to see the program echo Edgar Allen Poe's The Cask of Amontillado. You'll know change the program so that it encodes its input using what's called a rotation-13 (or rot-13) cipher method. In this method, the characters 'a'-'m' are replaced with 'n'-'z' respectively and 'n'-'z' are replaced by 'a'-'m'. Code Actual a n b o c p ... ... l y m z n a o b ... ... y l z m The same is done for upper-case characters. With this coding scheme, the same program can be used to decode and encode. This isn't a very secure cipher, but it can be useful. To do this coding, you'll rely on the ASCII coding of letters so that char('a' + 13) yields the character 'n' and char('z'-13) yields the character 'm'. You'll add 13 to the characters in the first half of the alphabet, and subtract 13 from the characters in the second half. To help, you'll want to make use of several functions from the library of functions in . These are shown below. function prototype returns int isalpha(int c) returns true when c is alphabetic (upper or lower case) int islower(int c) returns true when c is a lowercase letter int isupper(int c) returns true when c is an uppercase letter int tolower(int c) returns lowercase equivalent of c if isupper(c) then returns c unchanged int toupper(int c) returns uppercase equivalent of c if islower(c) then returns c unchanged Using these, modify the function Decode so that it adds 13 to alphabetic characters in the first half of the alphabet, subtracts 13 from alphabetic characters in the second half, and leaves other characters unchanged. You'll need to use statements like ch = ch + 13; or cout << char(ch + 13); where ch is a char variable. Make sure that you only add/subtract 13 from alphabetic characters (which you can test using the isalpha function). To test the program, you can use I/O redirection decode < decode.cc > code.out This runs the program using the file decode.cc as input, and stores the results in the file code.out. You can examine this file using emacs or by typing more code.out. Then you can run the program again using code.out as input. If you did things properly, the file uncode.out and decode.cc should be the same. To see if they are, you can use the UNIX command diff. diff uncode.out decode.cc If the files are the same, nothing will be printed as a result of running the diff command. If the files are different, then lots of things will be printed. Reading from User-specified file If you get the coding part working, you should then modify main so that the user is prompted for the name of a text file. A variable of type ifstream should be created and the open member function used to bind the iftream variable to the file specified by the user (this was part of last week's lab). ifstream infile; String name; cout << "enter file name: "; cin >> name; infile.open(name); if (infile.good()) { Decode(infile); } else { cout << "could not open file " << name << endl; } Then run your program and test it using a file whose name is entered by the user instead of using I/O redirection. *Printing the program Practice printing. To print the program follow the steps below. Create a lab6 subdirectory in your cps08 directory on your acpub account. Change into this directory and then type ftp mentor.cs.duke.edu (you'll be prompted for your password). From the ftp prompt ftp> you should change into your cps08/lab6 directory using the cd command: cd cps08/lab6. You can type ls to see what's there. Now you want to get the modified wordcheck.cc program. To do this type get wordcheck.cc. Check to see if you got the file in another xterm (you can't check with ftp active). If you retrieved the file, you can type quit to get out of ftp. You can also practice ftp'ing from mentor to the acpub machine. The same protocol is followed, but from mentor you would type ftp teer5.acpub.duke.edu for example, and you'd use put instead of get. Print the file wordcheck.cc by typing enscript -2rG -Pteerlp1 wordcheck.cc. If this doesn't work ask a TA. (You can substitute another printer for teerlp1). The -r option may cause an error message, but it still works. To turn in programs: When your modified programs word, 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. submit08 lab6 decode.cc README 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.