Hi everyone
I have been trying to create a simple encryption program it will read the txt from a .txt and then if plaintext encrypt it or if encrypted decrypt it to plaintext using the caesar's cipher I just can't get my head around as I am only new to c++ programming
Any help or ideas would be great
Here's what I have so far:
Code:
#include <iostream>//for input and output
#include <fstream>// for working with files
#define isupper(a) ((a)>= 'A' && (a)<= 'Z')
#define islower(a) ((a)>= 'a' && (a)<= 'z')
using namespace std;
ifstream inFile;
ofstream outFile;
char PlainText[100], CipherText[100] ;
int main()
{
{
outFile.open("test.txt");
(!outFile.eof() << PlainText[100]);
for ( int i = 0 ; PlainText[i] ; i++ )
{
if ( isupper(PlainText[i]) )
CipherText[i] = 25 - (PlainText[i] - 'A') + 'A' ;
else if ( islower(PlainText[i]) )
CipherText[i] = 25 - (PlainText[i] - 'a') + 'a' ;
else CipherText[i] = PlainText[i] ;
}
outFile << CipherText << endl;
}
inFile.close();
system("pause");
return 0;
}
Thanks in advance
Last edited by hitman_38; October 27th, 2008 at 10:40 PM.
You should use code tags to make it easier to follow your code. See Odiee's link for info on how to do that.
From a casual glance at your code, it appears that there are several problems. inFile is never opened nor read from. PlainText and CipherText are never initialized. This line:
Code:
(!outFile.eof() << PlainText[100]);
appears to be missing something; perhaps a while or if. It may be that it is misplaced and should be a few lines up (as indicated by the superfluous opening '{' ).
The loop for your actual encryption/decryption looks sound.
Ok I see the mistakes that are there but I not sure how to fix them. I'm still learn. I added some comments if that will help.
I know the encryption/decryption part works fine with cin & cout's, but I'm just confused about reading & writing to a text file instead of the cin & cout's?
Code:
#include <iostream>//for input and output
#include <fstream>// for working with files
#define isupper(a) ((a)>= 'A' && (a)<= 'Z')
#define islower(a) ((a)>= 'a' && (a)<= 'z')
using namespace std;
ifstream inFile;//file handle for data writing
ofstream outFile;//file handle for data reading
char PlainText[100], CipherText[100] ;
int main()
{
cout << "A program that encrypt/decrypt's text files" << endl << endl;
cout << "Now encrypting/decrypting ..." << endl << endl;
{
//open file
outFile.open("test.txt");
if (!outFile.eof() << PlainText[100]);
for ( int i = 0 ; PlainText[i] ; i++ )
{
if ( isupper(PlainText[i]) )//run encrypt/decrypt
CipherText[i] = 25 - (PlainText[i] - 'A') + 'A' ;
else if ( islower(PlainText[i]) )
CipherText[i] = 25 - (PlainText[i] - 'a') + 'a' ;
else CipherText[i] = PlainText[i] ;
}
//write to file
outFile << CipherText << endl;
}
//close file
outFile.close();
system("pause");
return 0;
}
You're still not using inFile at all. Is it supposed to be the input text? Immediately after you open outFile, your sending your PlainText buffer to it thereby overwriting anything in the file. Are you supposed to be reading PlainText from outFile? Try:
Code:
outFile >> PlainText;
but I suspect you really want to read it from inFile.
I'm not sure where or how to use the infile. It is meant to open the file with outfile read it to the Plaintext buffer then it goes through the encryption/decryption changing to CipherText then write to the file.
I tried
Code:
outFile >> PlainText;
But it gives me an error
expected '(' before "outfile"
am I suppose to use this instead of
Code:
(!outFile.eof() << PlainText[100]);
Last edited by hitman_38; October 29th, 2008 at 10:42 PM.
Sounds like you need to learn how to do simple file reading & writing--forget about encryption for now. First try to write a simple program that reads text from one file then writes that text to another file. If you have problems with that, post it here. Since it is a simpler problem of basic file read/write, you should be able to post it using code tags as opposed to an attachment. More people will bother to help if they don't have to download something.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.