CSCI 261 - Programming Concepts (C++)

Fall 2017 - Lab 6B

Quick Links: Canvas | CS @ Mines | Cloud9 | Piazza | zyBooks

|   Home |  Contact |  Syllabus |  Assignments |  Schedule |  Resources   |
This lab is due by Friday, November 10, 2017 11:59 PM .

Concepts



The 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:
  • If the character read is a newline character '\n', then you should write the newline character to the file.
  • Otherwise, if the character read 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 decipheredMessage.txt and see the information about the missing cows.


Hints



Where 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.

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:

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 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:

cout << ('a' + 2);

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:

cout << char('a' + 2);

Ahhh, much better.


Lab Submission



You will submit your solution to this lab with the rest of A6. Detailed instructions for doing this are posted in Assignment 6.


This lab is due by Friday, November 10, 2017 11:59 PM .
Last Updated: 10/22/17 19:43


Valid HTML 4.01 Strict Valid CSS! Level Triple-A conformance, W3C WAI Web Content Accessibility Guidelines 2.0