CSCI 261 - Programming ConceptsFall 2018 - Lab 7B - Translate the MoosageQuick Links: Canvas | Mines | Piazza | zyBooks | 
|
| | Home | Contact | Syllabus | Assignments | Schedule | Resources | | |
| 
 This lab is due by Thursday, November 15, 2018, 11:59 PM. ConceptsThe focus of this assignment is on one
	concept: how to read data from an "input file stream" or ifstream
	object. 
Working with Data
	Today's class discussed how data is often treated as "streams" of
	information that can be read a piece at a time. The files we will read
	in CSCI 261 are simple text files; for lab today, the simple text file
	contains a series of characters. Remember that whenever you work with a
	file stream as input, we call them
	 
ifstream
	objects.
	There will always be four things you will do whenever working with an
	 
ifstream
	. Open the file, check for an error, read its data, and close the file.
	The typical pattern for this is as follows:
	#include <iostream> 
#include <fstream> using namespace std; int main() { int age; ifstream myCatsAges("filename"); // declare ifstream object and open the file // check for an error if ( myCatsAges.fail() ) { cerr << "Error opening input file"; exit(1); } // read the data and do something with it while ( !myCatsAges.eof() ) { myCatsAges >> age; cout << "One cat is " << age << " years old.\n"; } myCatsAges.close(); // close the file return 0; } 
	Remember, once you have an
	 
ifstream
	object (like
	myCatsAges
	shown above) you use it in a manner similar to using
	cin
	.
Instructions
	The cows have been kidnapped by aliens! The only clue to their
	whereabouts is a strange "ciphered" message, stored in the file secretMessage.txt.
	Fortunately, our in-house cryptanalysis
	expert, D. Cipher, has discovered the key:
 
	"The key isn't very advanced, mmmkay? To decipher the message," she
	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 then writes a deciphered version to a file
	called
	 
decipheredMessage.txt
	. For each character in the file, your program should implement the
	following replacement algorithm:
 
	To see if your implementation works, you should be able to open the
	file
	 
decipheredMessage.txt
	and see the information about the missing cows.
CLion SetupWhere to place the input file
	Any input file needs to be placed at the project level, which should be
	the same directory as your
	 
main.cpp
	file. You should see both your input file AND main.cpp file in the
	same directory.After you run your program, you will likely get an error saying the requested input file cannot be found. This is the same problem we initially had with our output files. CLion is looking in the wrong place. We need to tell CLion which folder to use. To do so, we need to tell CLion where the files live. This can be found from going to Run > Edit Configurations. In the pop up window that appears, we want to set the "Working Directory" to be the path to the folder that our main.cpp file is located in. Hit Apply and OK, then rerun the program. You will now see the file created next to main.cpp. HintsReading 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:
	while ( secretMessage.get(c) ) { 
// ...mmkay } 
	This example assumes your
	 
ifstream
	is called
	secretMessage
	and you have a
	char
	variable declared that is 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. A
	switch
	statement could also be used.
	Casting to
	
 | |
| 
 Last Updated: 10/26/18 16:30 
 |