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

Hey guys, I need help with programming hw, I only need help on 2 void functions

ID: 3782254 • Letter: H

Question

Hey guys, I need help with programming hw, I only need help on 2 void functions since I've done a great bit of work already.

I've got everything and I only need help on the void functions LoadOneLine (read in data from input file) and ProcessOneLine (preform encrypt or decrypt with given shift)

Here is the 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
i s 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.
Blank message lines (no message text as in p01input3.txt) must not crash your program.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#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)
};

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

void PrintOneLine(Line telegram[], int count);
//Prints the output that has been defined within the processoneline function

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;
    }
    else if (inFile.good())                      //coding for when the input file has                                been successfully opened
    {
        status = true;
    }
}

void LoadOneLine(ifstream& inFile, Line telegram[], int n)      //Read in the characters that will...
{
   char c;

    inFile >> telegram[MAXLINES].mode;           //(reaches struct line to InFile) to                                        determine mode
    cin.ignore(',');                       //to bypass the comma
  
    inFile >> telegram[MAXLINES].shift;           //(reaches struct line to InFile) to                                        determine shift
    cin.ignore(',');                       //to bypass the second comma

                       //do-while loop that will continue to pull characters
                       //until it reaches the newline character
   while(c != ' ');
{
   inFile.get(c);
}      

if (telegram[MAXLINES].mode = 'e')           //if the mode was e signaling encryption
{
   inFile >> telegram[MAXLINES].ptext[MAXMSG];   //it saves the data into ptext for encrytion
}                           //later in the processoneline void function

if (telegram[MAXLINES].mode = 'd')           //if the mode was d signaling decryption
{
   inFile >> telegram[MAXLINES].ctext[MAXMSG];   //saves data into ctext for decryption
}                           //later in the processoneline void function


}

void ProcessOneLine(Line telegram[], int n)
{
   for (telegram[MAXLINES].ptext[MAXMSG]; n++;)
{
   n %= telegram[MAXLINES].shift;
   int ch;
}
}

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;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

And just in case you needed reference here are 2 input files that will be put through the program

# 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

and another:

# 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

That's all, I'd appreciate any quick help! Thank you!

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
******************************************************************
*/