CSCI 261 - Programming Concepts - Summer 2019

Lab 7A - Translate the Moosage

This lab is due by Tuesday, June 11, 2019, 11:59 PM.
As with all labs you may, and are encouraged, to pair program a solution to this lab.


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



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.

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.


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:

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 Set7. Detailed instructions for doing this are posted in Assignment 7.


This lab is due by Tuesday, June 11, 2019, 11:59 PM.
As with all labs you may, and are encouraged, to pair program a solution to this lab.