Although, I could easily state quite a few people will be bored with these, I couldn't find someone else with my problem. The code below runs, though doesn't decrypt. It gets to the first "Error: Unidentified Character" and ends. Any help great appreciated.
#include <iostream>
#include <string>
#include <array>
#include <iomanip>
#include <fstream>
#include <cstdlib>
//Defines the namespace
using namespace std;
int iteration = 0;
int size = 375;
//char *textReadIn;
string userHold = "q";
//Ask the user to input the shift number.
//loads the specified file.
char inputFileName[50] = "task_1.txt";
//opening pointer
ifstream inputFile;
//decryption function which takes the inputted data
void decryption(char* textToDecrypt)
{
string shiftTEXT(textToDecrypt);
string shiftTEXTOut;
//clears data from location if any.
shiftTEXTOut.clear();
//if any of these words are not in the text execute code
if ((shiftTEXT.find("or") !=string::npos)||(shiftTEXT.find("and") !=string::npos)||(shiftTEXT.find("the") !=string::npos)||(shiftTEXT.find("with") !=string::npos)||(shiftTEXT.find("have") !=string::npos)||
(shiftTEXT.find("if") !=string::npos)||(shiftTEXT.find("is") !=string::npos)||(shiftTEXT.find("of") !=string::npos)||
(shiftTEXT.find("as") !=string::npos)||(shiftTEXT.find("in") !=string::npos)||(shiftTEXT.find("been") !=string::npos))
{
char TempChar;
int i = 0;
string ADD;
//for loop around the array. size-3 avoids the header file
for (i = 0; i < size-3; i++)
{
TempChar = shiftTEXT.at(i);
//Ignores undeclared characters.
if((TempChar != ' ') && (TempChar != '.') && (TempChar != ','))// && (TempChar !=10))
{
//Uppercase boundaries in ASCII
if ((TempChar >= 65) && (TempChar <= 90))
{
//if statement to stop the program leaving the Uppercase boundaries.
TempChar -=17;
if (TempChar <65)
{
TempChar = 91 - (65 - TempChar);
}
ADD = TempChar;
shiftTEXTOut.append(ADD);
}
//Lowercase ASCII Boundaries
else if((TempChar >= 97) && (TempChar <= 122))
{
//if statement to stop the program leaving the Lowercase boundaries
TempChar -= 17;
if(TempChar < 97)
{
TempChar = 123 - (97 - TempChar);
}
ADD = TempChar;
shiftTEXTOut.append(ADD);
}
//If not the if or else if statement do below, handles any characters that has not been recognised.
else
{
//Prints a line to screen to inform user.
cout << "Error: Unidentified Character" << "\n";
cin >> userHold;
}
}
//handles the undeclared characters
else if ((TempChar == ' ')|| (TempChar == '.')|| (TempChar == ','))
{
//checks for any new lines, 10 = ASCII for NL.
if (TempChar != 10)
{
ADD = TempChar;
shiftTEXTOut.append(ADD);
}
}
else
{
cout << "Error: Unidentified Character" << "\n";
}
}
//prints the decrypted text out.
cout << shiftTEXTOut << "\n";
}
}
void outPut()
{
long start = 1;
long end = 400;
char *textReadIn;
inputFile.open(inputFileName);
if(!inputFile.is_open())
{
//prints line informing user of error.
cout << "Error: Unable to open the Defined File: ";
cin >> userHold;
//waits for user to press space
//closes program
exit(1);
}
//reads file
start = inputFile.tellg();
inputFile.seekg (0, ios::end);
end = inputFile.tellg();
//obtain the size
size = (int) (end-start);
//sets a size of memory for the file
textReadIn = new char [size];
inputFile.seekg(0, ios::beg);
inputFile.read(textReadIn, size);
//send the loaded file to the decryption method
decryption(textReadIn);
//close the file
inputFile.close();
//delete the pointer.
delete[] textReadIn;
}
int main()
{
outPut();
cin >> userHold;
}