#include<iostream>
#include<fstream>
#include<string>
#include<vector>
usingnamespace std;
vector<char> read_file(string file_name);
string decode(vector<char> encrypted, int key);
void display(string decrypted, int key);
int main ()
{
int ekey = 0;
vector<char> encrypted = read_file("intercepted.txt");
string decoder = 0;
// Insert appropriate function calls to decrypt the message and display the results
return 0;
}
vector<char> read_file(string file_name)
{
ifstream input_file;
input_file.open(file_name.c_str());
vector<char> encrypted;
char x;
while(input_file >> x)
{
encrypted.push_back(x);
cout << x;
}
input_file.close();
return encrypted;
}
string decode(vector<char> encrypted, int key)
{
for(int i = 0; i < encrypted.size(); i++)
{
}
return 0;
}
void display(string decrypted, int key){
cout << "Key: " << key << " Text: " << decrypted << endl;
}
I know the string decode function is incorrect, which is the main part of the program. I have no idea how to start this function or what I must call in the main function to make it work correctly. The functions arguments are correct, they were given to me.
Please help me start this function or give me an idea as to what I need to do to decode this!!
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
usingnamespace std;
//Function Declarations
void get_input(string& message, int& key);
vector<char> encrypt(string message, int key);
void display(vector<char> encrypted);
void save_file(vector<char> encrypted);
//Function calls to other functions to encrypt message
int main ()
{
string message;
int key;
get_input(message, key);
vector<char> encrypted = encrypt(message, key);
display(encrypted);
save_file(encrypted);
return 0;
}
//Asks the user for input to encrypt the user-entered input
void get_input(string& message, int& key)
{
//User enters the message they want to be encrypted
cout << "Please enter the message to be encrypted on a single line: " << endl;
//String function that includes the whitespaces on a single line
getline(cin, message);
//User enters a number used to encrypt their message
cout << "Please enter a key between 1 and 100 for use in encrypting your message: ";
cin >> key;
}
//This function encrypts the message and returns the encrypted value
vector<char> encrypt(string message, int key)
{
//Creates new vector
vector<char> encrypted;
for (int i = 0; i < message.length(); i++){
//Using ASCII, pushes the character back or farther back depending on the characters
//in the message, and the number entered.
if (int(message[i]) + key > 126){
encrypted.push_back(char(32 + ((int(message[i]) + key) - 127)));
}else{
encrypted.push_back(char(int(message[i]) + key));
}
}
cout << endl;
//Returns the encrypted value above
return encrypted;
}
//Displays the encrypted value from function vector<char>encrypt
void display(vector<char> encrypted){
cout << "After encrypting your message reads:" << endl;
//Displays the encrypted message one character at a time until it reaches null or
//i becomes > than encrypted.size
for (int i = 0; i < encrypted.size(); i++){
cout << encrypted[i];
}
cout << endl;
}
//Outputs the encryption to a .txt file
void save_file(vector<char> encrypted){
ofstream output_file;
output_file.open("encrypted.txt");
//Displays the encrypted message one character at a time until it reaches null or
//i becomes > than encrypted.size
for (int i = 0; i < encrypted.size(); i++){
output_file << encrypted[i];
}
output_file << endl;
output_file.close();
}