need this function definition for larger program in C++: void ProcessOneLine(Lin
ID: 667498 • Letter: N
Question
need this function definition for larger program in C++:
void ProcessOneLine(Line telegram[], int n)
{
enum alpha{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};
alpha standard;
standard=a;
n=0;
while(n<=telegram[].length)
{
if(telegram[].mode == 'e')
{
// STUCK, NEED CODE HERE!
}
if(telegram[].mode == 'd')
{
//STUCK, NEED CODE HERE!
}
}
structure Line telegram[] contains the following:
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)
};
Basically, the previous function takes one line of code in the format
e,-5,message ' ' ' ' ' '
and stores e into telegram.mode, the integer into telegram[].shift, the length of the message (<=64 characters) into telegram.length[], and each character of the message into the array ctext or ptext.
In the function, I cannot figure out how to take the alphabetic character stored into ptext or ctext and feed it into the enum function, then shift it forward or backward by telegram[].shift, then put it back into the ptext or ctext.
Explanation / Answer
#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 message 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.
// 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()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.