Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

*IN C++ LANGUAGE* Overview For years, the Radio Orphan Annie's Secret Society has entrusted their members with special Little Orphan Annie decoder pins so that

*IN C++ LANGUAGE*

Overview

For years, the Radio Orphan Annie's Secret Society has entrusted their members with special Little Orphan Annie decoder pins so that they can decode secret messages from Orphan Annie. For this program, the system of decoding messages will be modernized so that a C++ program can do the decoding rather than having to rely on a physical pin.

The information to be decoded is contained in a text file. The file will be processed one character at a time. Each character will be decoded by calling an appropriate function and then the decoded character will be displayed.

Using an Input File in a CPP program

Rather than having the user type in the encoded message, the information has been placed in a text file. This means that the program will be getting its input from the file rather than the keyboard.

The file with the input data can be downloaded and saved. It is called encoded_quotes.txt and can be found here:

http://faculty.cs.niu.edu/~byrnes/csci240/pgms/encoded_quotes.txt

Since the input for this program will be coming from a text file rather than standard input (the keyboard), the program will not use cin when it needs to read data. Instead, it will have to read the data from the file.

In order to read from a file, add two #include statements to the program. The new libraries that are needed are fstream and cstdlib.

Now, create an input file stream variable and "connect" it with the text file. The "connection" is created by opening the file:

ifstream infile; //input file stream variable //this will be used instead of cin infile.open( "encoded_quotes.txt" ); //open the file for reading 

The open statement will open a file named encoded_quotes.txt. The file must be located in the same directory as the CPP file. (For Mac users, you will probably have to put in the set of double quotes(""), find where the file has been saved on your computer, and then drag and drop the file in between the quotes. It should place the name of the file, including the complete path of where it is located on your computer, between the quotes.)

Once the file has been opened, make sure that it opened correctly. This is an important step because a file cannot be processed if it doesn't exist or open correctly. To test if the file opened correctly:

if( infile.fail() ) //if the input file failed to open { cout << "input file did not open" << endl; exit(-1) //stop execution of the program immediately } 

In order to read a character from a file, the input operator can be used in the same manner that it's used with a cin statement. So:

char ch; infile >> ch; 

Since the data is coming from a file, there has to be some way to determine when there is no more data in the file. One way to do this is to use the input file stream variable as a boolean variable:

while( infile ) //while there are characters in the file

As long as there is information in the file, the input file stream variable will remain valid (or true). Once the end of the data has been reached, the stream will become invalid (or false). Note: this test is only successful AFTER an attempt has been made to read data from the file. This means that a standard read loop pattern should be followed.

When a file is done being processed, it should be closed.

infile.close();

The Encoding Process

The original message from Orphan Annie was encoded using the following rules. Your program will simply undo/reverse this process. The original message that was encoded will be referred to as "message" below:

'0' --> ) '1' --> ! '2' --> @ '3' --> # '4' --> $ '5' --> % '6' --> ^ '7' --> & '8' --> * '9' --> ( 
, --> '9' " --> '8' ! --> '7' ; --> '6' ? --> '5' ' --> '4' ( --> '3' ) --> '2' . --> '1' - --> '0' 

a blank/space was encoded as an ASCII 22 a newline character was encoded as an ASCII 20

Any character in message that was an uppercase character was converted to lowercase and then had 1 subtracted from it. So 'B' became 'a', 'C' became 'b', ... The first uppercase letter was the exception to the rule. The 'A' was simply wrapped around to the end of the alphabet so that it became 'z'.

Any character in message that was a lowercase character was converted to uppercase and then had 1 added it. So 'a' became 'B', 'b' became 'C', ... The last lowercase letter was the exception to the rule. The 'z' was simply wrapped around to the beginning of the alphabet so that it became 'A'.

Any character in message that was a digit ('0' ... '9') was converted as follows:

Any character in message that was one of the following punctuation characters was encoded as shown below. Any punctuation that is not shown here was not altered.

Two white space characters were encoded as non-standard characters:

Note again: any character that does not fit the criteria listed above in 1 - 5 was not altered in the encoding.

Basic Program Logic

The program should first open the text file (encoded_quotes.txt that was mentioned above) and make sure that it opened correctly. If the file did not open correctly, display an informative error message and stop execution of the program.

Next, a single character should be read from the text file.

As long as there are characters in the text file, the single character should be processed/decoded by calling one of the functions that is described below based on the type of character. This should be done as part of a cascading decision statement that tests for the different type of characters (upper case, lower case, digit, punctuation, or special character). The decoded character should be displayed. And, finally, another character should be read from the text file.

Once the end of the file has been reached, the text file should be closed.

The Functions

This program will accomplish the decoding by using some pre-written library functions and ones that you design yourself.

Library Functions

The following set of functions are contained in the cctype library and can simply be called in the program (ie. they do not have to be written): isalpha, isupper(), islower(), ispunct(), isdigit(), toupper, tolower.

Each of the "is" functions take a character as an argument and return a true or false value:

isalpha() will return true if the passed in character is an alphabetic character; and false if the passed in character is not an alphabetic character

isupper() will return true if the passed in character is an uppercase character; and false if the passed in character is not an uppercase character

islower() will return true if the passed in character is a lowercase character; and false if the passed in character is not a lowercase character

ispunct() will return true if the passed in character is a punctuation character; and false if the passed in character is not a punctuation character

isdigit() will return true if the passed in character is a digit; and false if the passed in character is not a digit

The return value for the "is" functions allows for the functions to be called as the condition in an if statement:

if( isdigit(ch) ) { call appropriate decoding function } 

Each of the "to" functions take a character as an argument and return a character:

toupper() will take the passed in character and convert it to uppercase. The uppercase character will be returned from the function.

tolower() will take the passed in character and convert it to lowercase. The lowercase character will be returned from the function.

Other Functions

The functions listed below will be used to decode a character (these are the functions that MUST be written and called in the program).

bool isspecial( char charToTest )

This function will be called to test if the passed in character is a special character (ie. is the ASCII value for the character 20 or 22?). It takes a single character as its argument: the character to be tested. It returns a boolean value. If the passed in character is one of the two special characters, the function should return true. If the passed in character is not one of the two special characters, the function should return false.

char changeLowerToUpper( char charToChange )

This function will be called for any character that is a lowercase character. It takes a single character as its argument: the character to be decoded. It returns a character: the decoded character.

The function should reverse the process that was detailed in #1 of the Encoding Process above by converting the passed in character to uppercase and adding 1, so that, for example, 'd' becomes 'E'. However, there is the one exception to the rule: 'z' should become 'A'.

DO NOT have a decision statement with 26 separate tests. Take advantage of the fact that characters are stored as integer values.

char changeUpperToLower( char charToChange )

This function will be called for any character that is an uppercase character. It takes a single character as its argument: the character to be decoded. It returns a character: the decoded character.

The function should reverse the process that was detailed in #2 of the Encoding Process above by converting the passed in character to lowercase and subtracting 1, so that, for example, 'D' becomes 'c' and 'E' becomes 'd'. However, there is the one exception to the rule: 'A' should become 'z'.

DO NOT have a decision statement with 26 separate tests. Take advantage of the fact that characters are stored as integer values.

char changePunctToDigit( char charToChange )

This function will be called for any character that is a punctuation symbol (in C++, that is all type-able characters that are not letters, digits, or whitespace). It takes a single character as its argument: the character to possibly be decoded. It returns a character: the decoded character or the original passed in character.

The function should use a switch statement to check for each of the punctuation symbols that were detailed in #3 of the Encoding Process above and convert the passed in character to its original version, so that, for example, '&' becomes '7' or '@' becomes '2'. Don't forget that if the punctuation symbol is not listed above, it should be returned unchanged.

char changeDigitToPunct( char charToChange )

This function will be called for any character that is a digit character. It takes a single character as its argument: the character to be decoded. It returns a character: the decoded character.

The function should reverse the process that was detailed in #4 of the Encoding Process above by converting the passed in character to its original value, so that, for example, '6' becomes ';' and '2' becomes ')'.

char changeSpecialToSpace( char charToChange )

This function will be called for any character that is not an uppercase, lowercase, digit, or punctuation character. It takes a single character as its argument: the character to possibly be decoded. It returns a character: the decoded character or the original passed in character.

The function should reverse the process that was detailed in #5 of the Encoding Process above by only changing the passed in character if it has ASCII value 20 or 22. If the passed in character has an ASCII value of 20, then it will return a newline character. If the passed in character has ASCII value 22, then it will return a space character. Anything else will be returned unchanged.

Requirements

Each function must have a documentation box explaining:

/*************************************************************** Function: Use: Arguments: Returns: Note: ***************************************************************/ 

See the documentation standards on the course webpage for more examples or if further clarification is needed. Your program will not get full credit (even if it works correctly) if these standards are not followed. This will be the final reminder about documentation in the assignment write-ups.

As with the previous assignment and the assignments until the end of the semester, complete program documentation is required. For this assignment, that means that line documentation AND function documentation boxes are needed. In regards to line documentation, there is no need to document every single line, but logical "chunks" of related code should be preceded by a line or two that describe what the "chunk" of code does. Make sure that main() and any function that you write contains line documentation

its name

its use or function: that is, what does it do? What service does it provide to the code that calls it?

a list of its arguments briefly describing the meaning and use of each

the value returned (if any) or none

notes on any unusual features, assumptions, techniques, etc.

Make sure that the open statement contains only "encoded_quotes.txt".

Hand in a copy of the source code using Blackboard.

Notes

Then add code to handle uppercase characters and add uppercase characters to your text file. Run the program again and verify the output. Add in punctuation, digits, etc...

The one test that will be difficult to add in is the testing for the special characters. This is where you can switch to working with the original input file since all of the other characters likely work at this point. If you want something that is a little smaller to work with, try using just the 1st 50 characters from the original file. There should be enough of the special characters to test if your program can handle them properly.

When you're done testing the characters individually, make sure to run your code with original file, unchanged.

This program can be developed incrementally if you use your own data file. Start by writing the program so that it handles lowecase characters. Create a text file with the line zabcdef. Run your program using this file as input. It should produce ABCDEFG on the screen as its result.

Above, it says that isdigit() and the other "is" functions return a true or false value. That's part of the truth. If you look up the functions, you will see that they actually return an integer. As mentioned in lecture, any non-zero value is considered true; whereas 0 is considered false.

Output

For this assignment, to verify your output, just make sure that you can read the decoded message.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Logics For Databases And Information Systems

Authors: Jan Chomicki ,Gunter Saake

1st Edition

1461375827, 978-1461375821

More Books

Students also viewed these Databases questions

Question

2. What potential barriers would you encourage Samuel to avoid?

Answered: 1 week ago