1.11M Members

Yes, another Caesar Cipher Problem

 
0
 

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;
}
 
1
 

We can't run it without the input file. The most obvious first debugging technique would be to figure out what the value of TempChar is. Do that by printing it out, but typecast it to an int to make sure it prints. Then look up the value in the Ascii table and go back to your file and see if it's in there and not covered.

Add this line at line 79.

cout << (int) TempChar << endl;
 
0
 

Thanks Vernon, for some reason, the else error was keeping it back, you line showed that all the ASCII numbers were being accessed just not printed so an easy comment out got it to flow through.

If your interested here's the encrypted file:

Kyv Nyvvc fw Kzdv kliej, reu Rxvj tfdv reu grjj, cvrmzex dvdfizvj kyrk svtfdv cvxveu. Cvxveu wruvj kf dpky, reu vmve dpky zj cfex wfixfkkve nyve kyv Rxv kyrk xrmv zk sziky tfdvj rxrze. Ze fev Rxv, trccvu kyv Kyziu Rxv sp jfdv,
re Rxv pvk kf tfdv, re Rxv cfex grjk, r nzeu ifjv rsfmv kyv xivrk dflekrzeflj zjcreu fw Kivdrcbzex. Kyv nzeu nrj
efk kyv svxzeezex. Kyviv riv evzkyvi svxzeezexj efi veuzexj kf kyv Nyvvc fw Kzdv. Slk zk nrj r svxzeezex.

Question Answered as of 2 Years Ago by VernonDozier
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: