Hello, I am stuck on a part of my code, where I need to decode a file. I have searched and exhausted every forum I could find to see if other people have maybe had the same issue and solved it. However, I haven't found anything.
Here is what I have:
/* Marina Chernysheva
CS 161 Assignment 6
Sources: Instructor
*/
#include <iostream>
#include <string>
#include <cctype>
#include <cstring>
#include <fstream>
using namespace std;
char getMenuselect (char m);
const int ALPHA_LEN = 26;
// Prototypes
bool loadSubstitutions(char substitutions[]);
void decrypt(string fileName, char substitutions[]);
void encrypt(string fileName2, char substitutions[]);
int main()
{
ifstream input;
ifstream in;
ofstream out;
char menu;
char file[80];
string fileName;
char substitutions[ALPHA_LEN];
menu = 0;
menu = getMenuselect(menu); // Asks user what function to use
// Print your greeting and info about program.
cout << "Welcome to the Substitution Cipher Program." << endl;
if(menu == 'e'|| menu == 'E') // Encrypt file option
{
cout << "Enter file name: ";
cin >> fileName;
input.open(fileName);
//encrypt (file);
input.close();
cout << "Encrypted text follows: " << fileName << endl;
return main();
}
if (menu == 'd' || menu == 'D') // Decrypt file option
{
cout << "Enter file name: ";
cin >> fileName;
input.open(fileName);
decrypt (file, substitutions);
input.close();
cout << "Decrypted text follows: " << fileName << endl;
return main();
}
if (menu == 'q') // End program option
{
cout << "Program terminated!";
cin.ignore(2);
return 0;
}
cin.ignore(2);
return 0;
}
char getMenuselect (char m) //Menu select function
{
cout << "Select 'E' to encrypt a file or selec 'D' to decrypt a file or 'Q' to quit: ";
cin >> m;
switch (m)
{
case 'e':
case 'E': cout << "You have selected to encrypt a file: " << endl;
break;
case 'd':
case'D': cout << "You have selected to decrypt a file: " << endl;
case 'q':
case 'Q': cout << "You have selected to terminate the program." << endl;
break;
default: cout << "Invalid entry, please try again: ";
return getMenuselect(m);
}return (m);
}
void decrypt(string fileName, char substitutions[]) // funciton for decrypt
{
ifstream inFile;
int i = 0;
int letter;
for(i = 0; i < ALPHA_LEN; i++)
{
if(letter == substitutions[i])
{
cout << " " << substitutions[i] << endl;
}
}
inFile.close();
cout << "/n/n";
//create an ifstream, and use fileName to open it.
//loop through every letter in the file. maybe use while loop?
//inside loop: Find index of the letter in substitutions
// loop through the substitutions, looking for the letter.
// for (int i = 0; i < ALPHA_LEN; i ++)
// if letter = substitutions[i]
// i is what we are looking for. stop when equal, take note of i.
// Find the regular letter at index (A is 0, B is 1, etc)
// first cast the letter to an int. then do the following:
// if letter is B, letter - 'A' = 1;
// cast back to char, maybe add 'A';
// send the decrypted letter to screen and to file.
}
void encrypt(string fileName2, char substitutions[]) // function for encrypt
{
ifstream inFile;
cout << "What file would you like to encrypt?: ";
getline(cin, fileName2);
cin >> fileName2;
inFile.open(fileName2.c_str());
cout << "What would you like to name the Cipher file?: ";
string name;
getline(cin, name);
cin >> name;
cout << "Encrypted text follows: ";
cin >> substitutions;
cout << "/n/n";
}
bool loadSubstitutions(char substitutions[])
{
char letter;
ifstream inFile;
inFile.open("Substitutions.txt");
if (!inFile.is_open())
{
cout << "Error, can't open substitutions file. Exiting." << endl;
return false;
}
for (int i = 0; i < ALPHA_LEN; i++)
{
inFile.get(letter);
substitutions[i] = letter;
}
inFile.close();
return true;
}
line 115 - 126 I commented out for what I need to do. I just need some direction or link with an example to get an idea of how the loop should look like. Do I need to list the entire alphabet? Or just the letters in the file that needs to be decoded? Any help is appreciated!