Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

<Caesar Cipher C++ Hmwk Assignment> Complete the provided partial C++ program by

ID: 3663356 • Letter: #

Question

<Caesar Cipher C++ Hmwk Assignment>

Complete the provided partial C++ program by implementing several support functions that allow one to encrypt or decrypt a telegram using a Caesar Cipher. The Caesar Cipher algorithm is described in detail at the end of this document along with hints for implementing the cipher algorithm. You must implement each of these C++ support functions using the interface described by the provided function prototypes

The provided file main.cpp contains the function main() to provide a common way for everyone to test their code. The function main() will scan a sample input file and invoke the functions required to process the telegram stored in the input file. There will be two files.

<Description>

Each telegram is input from one of the provided sample input files. The first line of each input file starts with a # symbol and is a comment line describing the tests performed by that input file. The provided function main() inputs and echo prints the comment line to stdout. Each subsequent line of text within the input file represents a message line of the telegram. Within a message line there are three pieces of information separated by commas. • The first character is always the mode designation (e for encryption or d for decryption). • The second piece of information is an integer (positive, zero, or negative) which represents the amount of alphabet shift for the Caesar Cipher. • The message text begins after the second comma and continues until the end of the line. If the mode designation for a message line is e then the program assumes that the message text is plaintext that must be encrypted later on. If the mode designation is d then the program assumes that the message text is ciphertext that must be decrypted later. Any spaces within the message text are left as spaces whether in encryption or decryption mode. The function main() uses a loop and a series of function calls to input each message line, storing the mode designation, shift, and message text into an element of a one dimensional array of Line structures. Each Line structure contains multiple fields for storing mode, shift, ptext (plaintext), ctext (ciphertext), and the message text length (number of characters within the message text). The mode is used to decide where (ptext or ctext) to store the incoming message text. See the structure declaration within main.cpp for additional details. A telegram consists of up to MAXLINES message lines to be processed (the constant MAXLINES is predefined for you), and each message line may contain message text with up to MAXMSG characters. Any extra message lines must be ignored since there is not enough storage space reserved within the telegram array for more than MAXLINES message lines. Similarly, message text exceeding a length of MAXMSG characters must be truncated to a length of MAXMSG characters due to array size limitations.Only lower case characters are used within the message lines.

<compilation instr>

This project consists of two C++ files – one named main.cpp and one named project01.cpp written by you. If you examine the structure of main.cpp, you will see an extra #include directive near the end of the file – this statement will cause the preprocessor to import the function definitions you have written and saved in the project01.cpp file. So, for this assignment, all you must do to compile the program.

On project01.cpp, you may only use the following include files:

#include <iostream>

#include <fstring>

#include <string>

which have already been integrated into the provided file named main.cpp

<Caesar Cipher>

With a Caesar Cipher, an integer key value is used to specify the amount of alphabet shift between the plaintext and ciphertext alphabets. To encrypt a plaintext word, substitute each plaintext letter from the word with the corresponding letter from the ciphertext alphabet.

For example, assume that the shift value is 2. Then...

• Any letter 'a' appearing in the plaintext (unencrypted) message would be replaced by a 'c' in the ciphertext (encrypted) message.

• Any letter 'b' appearing in the plaintext (unencrypted) message would be replaced by a 'd' in the ciphertext (encrypted) message.

• Any letter 'c' appearing in the plaintext (unencrypted) message would be replaced by a 'e' in the ciphertext (encrypted) message.

• Etc. • Any letter 'x' appearing in the plaintext (unencrypted) message would be replaced by a 'z' in the ciphertext (encrypted) message.

• Any letter 'y' appearing in the plaintext (unencrypted) message would be replaced by a 'a' in the ciphertext (encrypted) message. (The alphabet wraps around)

• Any letter 'z' appearing in the plaintext (unencrypted) message would be replaced by a 'b' in the ciphertext (encrypted) message. (The alphabet wraps around)

Thus, to compute the ciphertext equivalent character of any plaintext character, the shift is ADDED to the relative position of the plaintext character

Using a shift of 2, the plaintext word hello would become the ciphertext word jgnnq

Decryption works in the opposite direction – from ciphertext alphabet back to plaintext alphabet by SUBTRACTING the shift quantity.

Just don't forget to WRAP once you get to the end of the lowercase letters.

HINT: Behind the scenes, characters are represented internally by integers (ASCII codes). Adjacent characters in the alphabet differ by 1.

'B' – 'A' ==> 66 – 65 = 1

'C' – 'A' ==> 67 – 65 = 2

HINT: The modulus operator % and type casting can be very useful on this assignment.

this is the main.cpp file(do not change anything in main)

====================================================================

//
// main.cpp --Project01
//

//

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

// Global constants
const int MAXMSG = 64;           // Maximum message length
const int MAXLINES = 6;           // Maximum number of non-comment lines in telegram


// Structure declaration
struct Line               // Represents one line input from file
{
char       mode; // Stores mode: e = encrypt, d = decrypt
int       shift;           // Stores amount of alphabet shift
int       length;           // Number of chars in message (<= MAXMSG)
char       ptext[MAXMSG];       // Stores unencrypted message (plaintext)
char       ctext[MAXMSG];       // Stores encrypted message (ciphertext)
};

/******** Start **********/

// Function prototypes for functions you must implement in the file project01.cpp
void OpenInputFile(string filename, ifstream& inFile, bool& status);
// OpenInputFile(...) attempts to open the given file for input. If the file
// is opened successfully, status should be set to true. Otherwise, status
// should be set to false.

void LoadOneLine(ifstream& inFile, Line telegram[], int n);
// LoadOneLine(...) attempts to load a single line of the telegram from the
// input file stream assuming that the stream has been opened successfully.
// n is the index within the telegram array where the new line should be stored.

void ProcessOneLine(Line telegram[], int n);
// ProcessOneLine(...) encrypts or decrypts the line at index n within telegram
// based upon the mode and shift values stored for that particular line.

/******** End **********/


// Function prototypes for provided functions provided
void PrintOneLine(Line telegram[], int count);

int main(int argc, char* argv[]) // Use command line arguments
{
ifstream   inFile; // Input file stream variable
bool       status = false;       // Stores file status: true = opened successfully
string comment; // Stores line of text from input file
Line telegram[MAXLINES];   // Stores up to MAXLINES messages input from file
  
// Verify command line arguments present
if (argc != 2)
{  
// Program usage error
cout << "Usage: ./project01 <inputfilenamehere> ";
return 1;
}
else
{
// Convert command line argument into c++ string
string filename(argv[1]);      

// Attempt to open file for input
OpenInputFile(filename, inFile, status);

// Verify file status
if (status) // If file opened successfully, process telegram
{
// Input and echo print comment
getline(inFile, comment);
cout << endl << comment << endl;;

// Loop to process up to MAXLINES messages from input file
int count = 0;

// Attempt to input first line of telegram array
LoadOneLine(inFile, telegram, count);

while ( inFile )
{
// Perform encryption/decryption operation
ProcessOneLine(telegram, count);

// Output processed line of input
PrintOneLine(telegram, count);

// Count processed message
count++;

// If a total of MAXLINES have been processed, then exit loop
if (count == MAXLINES)
break;

// Attempt to input next line into telegram array
LoadOneLine(inFile, telegram, count);
} // End WHILE

cout << endl;
}
else // ...else unable to open file
{
cout << "Error: unable to open file '" << filename << "' for input." << endl;
cout << "Terminating program now..." << endl;
}
  
return 0;
}

} // End main()

//
// Provided Function Definitions Below
//
// Reminder:
// All function definitions that you write must
// be placed into the file named project01.cpp
//

void PrintOneLine(Line telegram[], int count)
{
cout << "******************************************************************" << endl;
cout << " line: " << count+1 << endl;
cout << " mode: " << telegram[count].mode << endl;

cout << "shift: " << telegram[count].shift << endl;

cout << " ";
for(int k = 0; k < telegram[count].length; k++)
{
if (k % 10 == 0)
cout << k/10;
else
cout << ' ';
}
cout << endl;

cout << " ";
for(int k = 0; k < telegram[count].length; k++)
{
cout << k%10;
}
cout << endl;


cout << "ptext: ";
for(int k = 0; k < telegram[count].length; k++)
cout << telegram[count].ptext[k];
cout << endl;

cout << "ctext: ";
for(int k = 0; k < telegram[count].length; k++)
cout << telegram[count].ctext[k];
cout << endl;
cout << "******************************************************************" << endl;
} // End PrintOneLine()


/***************************************************/
/* The following include directive automatically */
/* adds the function definitions you have placed */
/* into project01.cpp to this file so that the */
/* compiler will see a complete single file program */
/***************************************************/

#include "project01.cpp"

================================================================

this file (below) is project01.cpp(Implementation file)

====================================

#include <iostream>
#include <fstream>
#include <string>

// I need this file

=======================================================

this is the first of three input files

input 1

===========================================================

# p01input1.txt -- Test encrypt with various shifts
e,0,abcdefghijklmnopqrstuvwxyz
e,5,abcdefghijklmnopqrstuvwxyz
e,26,a quick movement of the enemy will jeopardize six gunboats
e,4,a quick movement of the enemy will jeopardize six gunboats
e,-1,the quick brown fox jumps over the lazy dog
e,-3,the quick brown fox jumps over the lazy dog

================================================

input2

================================================

# p01input2.txt -- Test decryption with various shifts
d,0,abcdefghijklmnopqrstuvwxyz
d,5,fghijklmnopqrstuvwxyzabcde
d,26,a quick movement of the enemy will jeopardize six gunboats
d,4,e uymgo qsziqirx sj xli iriqc ampp nistevhmdi wmb kyrfsexw
d,-1,sgd pthbj aqnvm enw itlor nudq sgd kzyx cnf
d,-3,qeb nrfzh yoltk clu grjmp lsbo qeb ixwv ald

==================================================

input3

=================================================

# p01input3.txt -- Test error handling for too many message lines
e,1,line one
e,2,line two
e,3,line three
e,4,line four
e,5,line five
e,6,line six
e,7,line seven
e,8,line eight
e,0,
e,0,this very long sentence tests the ability of your code to process a long phrase
d,0,

====================================================================

I need the project01.cpp or the implemenation file compiled correctly.

Explanation / Answer

void OpenInputFile(string filename,ifstream& inFile,bool& status){
   inFile.open(filename.c_str());
   if (inFile.is_open())
       status = true;
   else
       status = false;
}

void LoadOneLine(ifstream& inFile, Line telegram[], int n){
   char mode;
   int shift;
   string s;
   inFile >> mode >> shift >> s;
   Line line();
   line.mode = mode;
   line.shift = shift;
   line.ptext = s.c_str();
   line.length = s.length();
   telegram[n] = line;
}

void ProcessOneLine(Line telegram[], int n){
   Line line = telegram[n];
   if (line.mode == 'e'){
       for (int i = 0; i < line.length; i++){
           int temp = ((line.ptext[i] - 'a') + line.shift) % 26;
           line.ctext[i] = (char) temp;
       }
   }
   else{
       line.shift *= -1;
       for (int i = 0; i < line.length; i++){
           int temp = ((line.ptext[i] - 'a') + line.shift) % 26;
           line.ctext[i] = (char) temp;
       }  
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote