CSCI 261 - Programming Concepts (C++)Fall 2016 - Lab 09BQuick Links: Blackboard | EECS | Mines | Piazza | zyBooks |
|
| Home | Contact | Syllabus | Assignments | Schedule | Resources | | |
This lab is due by November 02, 2016 08:00am.
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 . InstructionsThe 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.HintsWhere 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
a .vcxproj file in the same directory. Reading whitespace charactersIn 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 statementNote 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: 01/01/70 00:00
|