Homework 12: Shift Cipher
This assignment is designed to exercise your ability to write data to a file.
Concepts
Focus on one main concept for this assignment: how to write data to an "output file stream" or ofstream
object.
Writing Data to a File
You should have learned in class about how data can be written to files. Remember that whenever you work with a file stream as output, we call them ofstream
objects.
Similar to reading files, there will always be four things you will do whenever working with an ofstream
. Open the file, check for error, write some data, and close the file. The typical pattern for this is as follows:
#include <fstream>
using namespace std;
ofstream myCatsAges("filename"); // open the file
// check for an error
if (!myCatsAges) {
cerr << "Error opening output file";
exit(1);
}
// write some data
myCatsAges << "3" << endl;
myCatsAges << "12" << endl;
myCatsAges << "21 woohoo!" << endl;
myCatsAges.close(); // close the file
Remember, once you have an ofstream
object (like myCatsAges
shown above) you use it in a manner similar to using cout
.
Instructions
The cows have been kidnapped by aliens! The only clue to their whereabouts is a strange "ciphered" message, stored in the file secret_message.txt
. Fortunately, our in-house cryptanalysis expert, D. Cipher, has discovered the key:
"The key isn't very advanced, mmmkay? To decipher the message," he says, " you should take each character and replace all ~
(tilde) characters with a space, and shift all characters up by one, mmmkay?"
Your goal for this assignment is to create a program that reads the ciphered text file and writes a deciphered version to a file called deciphered_message.txt
. For each character in the file, your program should implement the following replacement algorithm:
- If the character read is a newline character
'\n'
, then you should write the newline character to the file. - Otherwise, if the character is a
~
, you should write a space to the file. - Otherwise, write the character read "offset" by +1.
To see if your implementation works, you should be able to open the file deciphered_message.txt
and see the information about the missing cows.
Hints
Reading whitespace characters
In order to capture and replace whitespace characters, you will not use the >>
operator with the input filestream. Instead, you will use the get()
function like this:
// ...mmkay
}
This example assumes your ifstream
is called secretMessage
and you have char
variable called c
.
Selection statement
Note that one requirement is to model the logic of the deciphering algorithm using a proper if/else-if/else
construct.
Casting to char
Remember, cout
and ofstream
objects are sensitive to the datatype of the value to be printed or written to a file. Consider the following:
What is printed to the screen? The number 99. Why? Because a char
plus an int
yields an int
, and then the int is "sent" to cout
. To print the character c
to the screen, you will need to use casting, like this:
Ahhh, much better.
Messed up your original secret_message.txt
file?
Requirements and Rubric
A friendly message from The Terminator, our grading program
*bzzzt* Hel-lo. I will check for the following:
Your program must not require any user input (no use of cin
).
Your program must terminate after deciphering the text file.
Your program must correctly decipher the secret message.
This work is worth 55 points.
Requirement | Points | Notes |
---|---|---|
Place your name in the comment header in main.cpp | 2 | |
Correct datatypes used | 5 | char |
Opens input file for reading | 5 | |
Checks input file for error | 5 | |
Properly closes input file | 5 | |
Opens output file for writing | 5 | |
Checks output file for error | 5 | |
Properly closes output file | 5 | |
Successfully deciphers the secret message | 9 | |
Proper implementation of deciphering algorithm | 9 | correct selection statement |
Concepts Exercised: I/O, repetitive tasks, making decisions, declaring facts, using libraries, functions, fun
© 2011 Yong Joseph Bakos and Keith Hellman.