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

For this project, you must complete the provided partial C++ program by implemen

ID: 3782297 • Letter: F

Question

For this project, you must complete the provided partial C++ program by implementing several
support functions that allow one to encrypt or decrypt a telegram using a Caesar Cipher.

We are required to create code for the following functions:

void OpenInputFile(string filename, ifstream& inFile, bool& status);

void LoadOneLine(ifstream& inFile, Line telegram[], int n);

void ProcessOneLine(Line telegram[], int n);

--------------------------------------------------------

main.cpp

#include
#include
#include

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 ";
    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()


/***************************************************/

#include "project01.cpp"

-------------------------------------------------------------------------------------------------------------------------------

# 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

----------------------------------------------

# 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

---------------------------------------------

# 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 can compile my code. It reads the # line in the .txt file, but it doesn't read anything else.

// Attempts to open .txt file
   void OpenInputFile(string filename, ifstream& inFile, bool& status)
   {
       inFile.open(filename.c_str()); // Opens the file
       if(inFile.is_open()) // Checks to see if file was opened
       {
           status = true; // Opening the file was successful
       }
       else
       {
           status = false; // File did not open
       }
   }

   // Attempts to load a single line from the .txt file
   void LoadOneLine(ifstream& inFile, Line telegram[], int n)
   {
       char mode;       
       int shift;
       inFile >> telegram[n].mode; // Reads the mode (e or d)
       cin.ignore(',');
       inFile >> telegram[n].shift; // Reads the shift amount
       cin.ignore(',');
       inFile >> telegram[n].ptext;
       }

   // Encrypts or decrypts the .txt file depending on the shift
   // and the mode for the particular line
   void ProcessOneLine(Line telegram[], int n)
   {
       //Line line = telegram[n];
       if (telegram[n].mode == 'e') // Check to see if line is an encrypted line
       {
           for (int i = 0; i < telegram[n].length; i++)
           {
               int tmp = ((telegram[n].ptext[i] - 'a') + telegram[n].shift) % 26;
               telegram[n].ctext[i] = (char) tmp;
           }
       }
       else // Line is a decryption line if line.mode is not an 'e'
       {
           telegram[n].shift *= -1;
           for (int i = 0; i < telegram[n].length; i++)
           {
               int tmp = ((telegram[n].ptext[i] - 'a') + telegram[n].shift) % 26;
               telegram[n].ctext[i] = (char) tmp;
           }
       }
   }

Explanation / Answer

// C++ code
#include <iostream>
#include <fstream>
#include <string>
#include <string.h>
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);
void encrypt(Line &telegram, char *encrypt, char *in); //separated function to recurse in process
void decrypt(Line &telegram, char *encrypt, char *in); //to recurse in processoneline
void decode(char encrypt[26],int shifts, char *cc); //decoding of string and incorporation of shift
void LoadOneLine(ifstream& inFile, Line telegram[], int n);
void ProcessOneLine(Line telegram[], int n);
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()
void OpenInputFile(string filename, ifstream& inFile, bool& status) // opening input file
{
//cout << " Enter the name of the input file: "; //asking for any of the p01 input files
//cin >> filename;
//cout << filename << endl;
inFile.open(filename.c_str()); //opening input file
if (inFile.fail()) //error coding of false for the failed input file or an empty input file
{
status = false; //sets from the boolean key into a fail state
}
else if (inFile.good()) //coding for when the input file has been successfully opened
{
status = true; //sets from the boolean key to continue prog.
}
}
void LoadOneLine(ifstream& inFile, Line telegram[], int n) //Read in the characters that will...
{
inFile >> telegram[n].mode; //(reaches struct line to InFile) to determine mode
if (n <= MAXLINES)
{
if (telegram[n].mode == 'e') //if encryption is selected
{
inFile.ignore(1,','); //ignores first comma
inFile >> telegram[n].shift; //extracts shift
inFile.ignore(1,','); //ignores second comma
inFile.getline(telegram[n].ptext,100); //stores into ptext
telegram[n].length = strlen(telegram[n].ptext); //equals length to get the str length of ptext
}
  
else if (telegram[n].mode == 'd') //if decryption is selected
{
inFile.ignore(1,','); //ignores first comma
inFile >> telegram[n].shift; //extracts shift
inFile.ignore(1,','); //ignores second comma
inFile.getline(telegram[n].ctext,100); //stores into ctext
telegram[n].length = strlen(telegram[n].ctext); //equals length to get the str length of ctext
}
}
  
if (n == (MAXMSG - 1)) //if the count loop for maxmsg is 63
{
inFile.ignore(200,' '); //it will ignore almost indefinitely until it reaches a newline character
}
}
void ProcessOneLine(Line telegram[], int num) //changed int n to num to be able to use enumerated letters w/out shadowing the n variable
{
enum letter{a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z}; //enum values
//declare the required variables
char cc[26]; //number of the letters
char encrypting[27];
//convert each enum value to a character
for (int i=0;i<26;i++)
{
cc[i]=(a+i)+'a'; //loops until all 26 characters are designated
}

int shift = telegram[num].shift; //changes the shift into an easier variable
  
/*if(shift < 0)
{
for (int i=26;i>0;i--)
{
cc[i]=(a+i)+'a';
}
} */ //attempt to fix the fault in my code due to negative shift
decode(encrypting, shift, cc); //recursion into the decoding function
encrypting[26]='';
if (telegram[num].mode == 'e') //if mode is e for encryption
{
encrypt(telegram[num], encrypting, cc); //recursion into the encryption void function
}
else if(telegram[num].mode == 'd') //if mode is d for decryption
{
decrypt(telegram[num], encrypting, cc); //recursion into the decryption void function
}
}
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;
}
void decode(char encrypt[26], int shift, char *cc)   
{

int k=0;

for(int i=0; i<26;i++) //decoding and encrypting technique
{ //which takes the shifted values of cipher
if((i+(shift))<26) //text into a character array encrypt
{
encrypt[i]=cc[i+(shift)];
}
  
else
{
encrypt[i]=cc[k];
k++;
}
}
}

void encrypt (Line &telegram, char *encrypt, char *cc)
{
//begins loop that checks for the plain text and assigns th
int n=0;
//encrypt logic
for(int i=0;i<telegram.length;i++) //n being the count of the letters
{
n=0;
while(n<=26) //loop that checks for the plain text and assigns the decrypted message into cipher text
{
if (telegram.ptext[i]==' ')
{
telegram.ctext[i]=' ';
}
if (telegram.ptext[i]==cc[n])
{
telegram.ctext[i]=encrypt[n];
}
n++;
}
  
}
telegram.ctext[telegram.length]='';
}

void decrypt (Line &telegram, char *encrypt, char *cc)
{
int n=0;
int k=0;
n=0;
//decrypt logic
for(int i=0; i<telegram.length;i++) //n being the amount of the letters
{
n=0;
while(n<=26) // loop that checks for the cipher text and assigns the encrypted text to plain text
{
if(telegram.ctext[i]==' ')
telegram.ptext[i]=' ';
if(telegram.ctext[i]==encrypt[n])
{
telegram.ptext[i]=cc[n];
}
n++;
}
  
}
telegram.ptext[telegram.length]='';
}


/*
# p01input2.txt -- Test decryption with various shifts
******************************************************************
line: 1
mode: d
shift: 0
0 1 2   
01234567890123456789012345
ptext: abcdefghijklmnopqrstuvwxyz
ctext: abcdefghijklmnopqrstuvwxyz
******************************************************************
******************************************************************
line: 2
mode: d
shift: 5
0 1 2   
01234567890123456789012345
ptext: abcdefghijklmnopqrstuvwxyz
ctext: fghijklmnopqrstuvwxyzabcde
******************************************************************
******************************************************************
line: 3
mode: d
shift: 26
0 1 2 3 4 5   
0123456789012345678901234567890123456789012345678901234567
ptext: a quick movement of the enemy will jeopardize six gunboats
ctext: a quick movement of the enemy will jeopardize six gunboats
******************************************************************
******************************************************************
line: 4
mode: d
shift: 4
0 1 2 3 4 5   
0123456789012345678901234567890123456789012345678901234567
ptext: a quick movement of the enemy will jeopardize six gunboats
ctext: e uymgo qsziqirx sj xli iriqc ampp nistevhmdi wmb kyrfsexw
******************************************************************
******************************************************************
line: 5
mode: d
shift: -1
0 1 2 3 4
0123456789012345678901234567890123456789012
ptext: the quick brown fox jumps over the lazy dog
ctext: sgd pthbj aqnvm enw itlor nudq sgd kzyx cnf
******************************************************************
******************************************************************
line: 6
mode: d
shift: -3
0 1 2 3 4
0123456789012345678901234567890123456789012
ptext: the quick brown fox jumps over the lazy dog
ctext: qeb nrfzh yoltk clu grjmp lsbo qeb ixwv ald
******************************************************************
*/

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