i need a code as very easy way! http://www.chegg.com/homework-help/questions-and
ID: 3606841 • Letter: I
Question
i need a code as very easy way!
http://www.chegg.com/homework-help/questions-and-answers/c-please-type-code-show-run-also-upload-output-q19924771
using a similar function like a above of link. its not same question above the link, a little bit different.
Explanation / Answer
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cctype>
using namespace std;
void banner();
char select(char& choice);
void ceasar(ifstream& in_stream, ofstream& out_stream);
void caesar(ifstream& in_stream, ofstream& out_stream);
int main()
{
char choice;
char input_file[33];
char output_file[33];
ifstream fin;
ofstream fout;
banner(); // display info
select(choice); // prompt choice
if (choice == 'E' || choice == 'e') // if E then prompt for file to encrypt and output file to write to
{
cout << "Enter the name of your input file that you want to encrypt: ";
cin >> input_file;
cout << "Enter the name of the output file to write the ciphertext to: ";
cin >> output_file;
fin.open(input_file); // open files
fout.open(output_file);
if (fin.fail()) // ERROR check if file doesnt open properly
{
cout << "Input file failed to open. " << endl;
exit(1);
}
ceasar(fin, fout); // process files and functions
fout << endl;
fin.close();
fout.close(); // close files
}
if (choice == 'D' || choice == 'd') // same as above just for decryption
{
cout << "Enter the name of your input file that you want to decrypt: ";
cin >> input_file;
cout << "Enter the name of the output file to write the plaintext to: ";
cin >> output_file;
fin.open("input.txt");
fout.open("output.txt");
if (fin.fail())
{
cout << "Input file failed to open. " << endl;
exit(1);
}
caesar(fin, fout);
fout << endl;
fin.close();
fout.close();
}
return 0;
}
void banner() // personal info function
{
cout << endl
<<" +--------------------------------------------------+ "
<<" | Computer Science and Engineering | "
<<" | CSCE 1030 - Computer Science I | "
<<" +--------------------------------------------------+ "
<< endl;
return;
}
char select(char& choice) // choice func
{
cout << "Would you like to ENCRYPT OR DECRYPT a file (E or D)? ";
cin >> choice;
switch (choice)
{
case 'E':
case 'e':
//Encryption
break;
case 'D':
case 'd':
//Decryption
break;
default:
// loop back if E or D is not choosen
return select(choice);
}
}
void ceasar(ifstream& in_stream, ofstream& out_stream)
{
int key; // key should be positive int
char text; //specified by instructions to not use string array
cout << "Enter the numerical key (i.e., an integer) used to encrypt: ";
cin >> key;
in_stream.get(text); // process input file text into function
while (! in_stream.eof()) // read every character until EOF
{
if (isalpha(text)) // check for alphabet
{
if (text >= 'A' && text <= 'Z') // if upper
{
text = (((text - 'A') + key + 26) % 26) + 'A'; // function to cipher each character determined by key entered
out_stream.put(text);
}
else if (text >= 'a' && text <= 'z') // if lower
{
text = (((text - 'a') + key + 26) % 26) + 'a'; // function to cipher each character determined by key entered
out_stream.put(text);
}
}
else if (isdigit(text)) // check for digit and cipher determined by key entered
{
if (text >= '0' && text <= '9')
{
text = (((text - '0') + key + 10) % 10) + '0';
out_stream.put(text);
}
}
in_stream.get(text); // process text in file
}
return;
}
void caesar(ifstream& in_stream, ofstream& out_stream) // process same as above just for decryption
{
int key;
char text;
cout << "Enter the numerical key (i.e., an integer) used to decrypt: ";
cin >> key;
in_stream.get(text);
while (! in_stream.eof())
{
if (isalpha(text))
{
if (text >= 'A' && text <= 'Z')
{
text = (((text - 'A') - key + 26) % 26) + 'A'; // function to cipher each character determined by key entered
out_stream.put(text);
}
else if (text >= 'a' && text <= 'z')
{
text = (((text - 'a') - key + 26) % 26) + 'a'; // function to cipher each character determined by key entered
out_stream.put(text);
}
}
else if (isdigit(text)) // check for digit
{
if (text >= '0' && text <= '9')
{
text = (((text - '0') + key + 14) % 10) + '0';
out_stream.put(text);
}
}
in_stream.get(text);
}
return;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.