CSCI240 Program 6 Functions, switch, and Character Processing solution

$25.00

Original Work ?
Category: You will Instantly receive a download link for .ZIP solution file upon Payment

Description

5/5 - (5 votes)

For years, the Radio Orphan Annie’s Secret Society has entrusted their members with a 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.

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 on your computer. 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, 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. One way to do that 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();

Processing

The text file (encoded_quotes.txt that was mentioned above) will be processed one character at a time. Each character will be decoded by calling an appropriate function and then displayed.

The basic layout for the program is as follows:

//Open the input file and make sure that it opened correctly

//Read the first character from the input file

//while there are characters in the file

   //Process the character by decoding it. A cascading if statement should
   //be used to test the "type" of character (upper case, lower case, digit,
   //punctuation, or special character) and call the appropriate decoding
   //function to change the character to its original value
  
   //Display the decoded character

   //Read the next character from the input file
//end of while loop

//Close the input file

The Encoding Process

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

    1. 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’.
    2. 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’.
    3. Any character in message that was a digit (‘0’ … ‘9’) was converted as follows:
'0'  -->  )
'1'  -->  !
'2'  -->  @
'3'  -->  #
'4'  -->  $
'5'  -->  %
'6'  -->  ^
'7'  -->  &
'8'  -->  *
'9'  -->  (
    1. 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.
, -->  '9'
" -->  '8'
! -->  '7'
; -->  '6'
? -->  '5'
' -->  '4'
( -->  '3'
) -->  '2'
. -->  '1'
- -->  '0'
    1. Two white space characters were encoded as non-standard characters:

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

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

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): isalphaisupper(), islower(), ispunct()isdigit().

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
  }

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 ch )

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 changeLower( char ch )

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 changeUpper( char ch )

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 changePunct( char ch )

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 changeDigit( char ch )

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 changeSpecial( char ch )

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

  1. 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.This will be the final reminder in the assignment write-ups.
  2. Hand in a copy of your source code using Blackboard.

Notes

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

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.

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

Extra Credit #1

For up to 5 points of extra credit, add code that will calculate and display the total number of:

  • characters
  • alphabetic characters
  • digit characters
  • punctuation characters
  • whitespace (newline and space) characters

in the DECODED message. The 5 values should be displayed in a neat table with labels that clearly indicate what value is being displayed.

Note about extra credit: the points will ONLY be awarded if the required portions of the assignment work correctly. In other words, don’t take short cuts in the rest of the program because it is assumed that 5 extra points will be awarded.

Extra Credit #2

For up to an additional 5 points of extra credit, add a cout statement at the end of main that displays the name of the movie that had all of the quotes in the decoded message.

Note about extra credit: the points will ONLY be awarded if the required portions of the assignment work correctly. In other words, don’t take short cuts in the rest of the program because it is assumed that 5 extra points will be awarded.