hey guy's I'm relatively new to C++ and I've been asked to make a Caesar Cipher. I'm supposed to take an encrypted .txt file and output it's decryption. I've sort of got an idea of my flow as shown below but I'm confused as to how to transform capitals in the encrypted .txt file into lower case so I can process them normally. I know I have to use "tolower", does that mean I have to include the header for it as well?

Here's my code so far, am I on the right track? I'm not seeing any errors right now but I haven't debugged yet.

/*
Assignment 06- The Caesar Cipher
Author- ******
Sources:

References:

void characterCount(char ch, int list[]);
void calcShift(int& shift, int list[]);
void writeOutput(ifstream &in, ofstream &out, int shift);

*/

#include <cstring>
#include <string>
#include <iomanip>
#include <fstream>
#include <iostream>

using namespace std;

const int CAPS_START = 65;
const int LOWER_START = 97;

int caps[26];
int lowerCase[26];



int main() {

	int maxIndex;
	char ch;
	int intChar;
	
	//open the file.
	//streaming..
	fstream testFile;
	ifstream inFile; 
	ofstream outFile;
	
		//Open input file
	inFile.open("C:/Users/Tommy/Documents/Visual Studio 2010/Projects/a06.cpp/encrypter.txt");
	
		if(!inFile.is_open()) 
		{//error trigger
			cout << "Error opening input file. Closing program.";
			//system("PAUSE");
			system("PAUSE");
			return(0);
		}
	
	outFile.open("encrypter.txt");
		if(!outFile.is_open()) 
		{																							//error trigger
			cout << "Error opening output file. Closing Program.";
			//system("PAUSE");
			system("PAUSE");
			return 0;
		}

	//read char by char to the end of the file.
	//decide if the char is upper or lower.
	intChar = static_cast<int>(ch); //Convert to int.

	//after counting all the letters, find the max.
	maxIndex = 0;
	for(int i = 1; i < 26; i++) 
	{
		if(caps[maxIndex] > caps[i])
		{
			maxIndex = caps[i];
		}
	
	}
		system("PAUSE");
	return 0;
}

A few notes:

  • Yes, you would need the <cctype> header for tolower() . I could add that you'd want to also have the <cstdlib> header for the calls to system() .
  • Are the system() calls actually needed? If I am not mistaken, Visual Studio 2010 automatically pauses console programs at the end.
  • As things now are, you are opening the same file for both input and output. This means that you'll be overwriting the cleartext with the ciphertext. Given that you neither read from nor write to the file, this won't matter right now, but surely you intend to use different files for this purpose?
  • You have the path to the input file hard-coded to a specific location on your drive, which will surely present a problem when you hand the assignment in. A better solution to both this problem and the previous one would be to prompt the user for the names of the files, or else get them at the command line.
  • You have three function prototypes in your comments, but the neither the prototypes nor the functions themselves are in the code. I assume that this was part of the assignment description, which you might want to re-read.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.