Could somebody please help me with this C++ program? And here is the linked func
ID: 3834848 • Letter: C
Question
Could somebody please help me with this C++ program?
And here is the linked function
string translate(string s) {
string output = "";
for (int i = 0; i < s.length(); i++)
{
char c = s[i]; if ((c >= 'a' && c <= 'm') || (c >= 'A' && c <= 'M'))
c += 13;
else if ((c >= 'n' && c <= 'z') || (c >= 'N' && c <= 'Z'))
c -= 13; output += c;
}
return output;
}
create a program that encrypts and decrypts text files, using a simple ROT13 algorithm (a function that translates a string using the algorithm is linked in the assignment). Your program should: A. Provide a menu of options (ENCRYPT, DECRYPT, QUIT) B. Loop until a user selects QUIT C. If ENCRYPT is selected, ask the user for 1. a text file to be encrypted and 2. the name of a file to contain the output. Open the input file and use the provided function to translate a line of the file at a time, writing the translated line to the output file. Remember that getline can be used to get one line of input with spaces at a time, for example, the code below will loop until no more lines can be read string line; while (getline (infile, line)) D. If DECRYPT is selected, reverse the process, asking the user for 1. a text file to be decrypted and 2. the name of a file to contain the output. You will have to figure out how to decrypt the input. (HINT: this is quite easy, think of how many letters there are in the alphabetExplanation / Answer
Here is the code for you:
#include <iostream>
#include <fstream>
using namespace std;
string translate(string s) {
string output = "";
for (int i = 0; i < s.length(); i++)
{
char c = s[i]; if ((c >= 'a' && c <= 'm') || (c >= 'A' && c <= 'M'))
c += 13;
else if ((c >= 'n' && c <= 'z') || (c >= 'N' && c <= 'Z'))
c -= 13; output += c;
}
return output;
}
int main()
{
string fileName, line;
int choice;
ifstream fin;
ofstream fout;
while(true)
{
cout << "1. ENCRYPT. 2. DECRYPT. 3. QUIT." << endl;
cin >> choice;
switch(choice)
{
case 1:
cout << "Enter the name of the text file to encrypt: ";
cin >> fileName;
fin.open(fileName);
cout << "Enter the name of the text file to save the output: ";
cin >> fileName;
fout.open(fileName);
while(getline(fin, line))
fout << translate(line) << endl;
break;
case 2:
cout << "Enter the name of the text file to decrypt: ";
cin >> fileName;
fin.open(fileName);
cout << "Enter the name of the text file to save the output: ";
cin >> fileName;
fout.open(fileName);
while(getline(fin, line))
fout << translate(line) << endl;
break;
case 3: return 0;
}
}
}