Now before you give me a knee-jerk "do your OWN homework we-won't-help-you-cheat" reaction, please read this all the way through.
My homework assignment is to write a Caeser Cipher (from my understanding its very common for professors to give this assignment) and I have that done (with some help from a coworker who took a C++ class a couple terms ago). The problem is that I do not understand how I am supposed to make the program write the output to a seperate .txt file. I can get it to read the .txt file that gives the input (otherwise I wouldn't know that the program actually works). The code that I have is below, and I was wondering if I could get some help, how do I get this program to write the output to a text file?
Again, I am -NOT- asking for someone to do this for me, just for help.
#include <iostream>
#include <string>
#include <cctype>
#include <fstream>
using namespace std;
string CaesarShift(string text, int shift);
int main()
{
int maxEs = 0;
int currentEs = 0;
string maxString;
string currentString;
string cipher;
char ch;
ifstream fin("HiddenMessage.txt");
while( fin.get(ch) )
{
cipher += ch;
}
fin.close();
for(int i=0; i < 26; i++)
{
currentEs =0;
currentString =caesarShift(cipher, i);
for(unsigned int x=0; x <currentString.size(); x++)
{
if(currentString[x] == 'e' || currentString[x] == 'E')
{
currentEs++;
}
}
if(currentEs > maxEs)
{
maxEs =currentEs;
maxString= currentString;
}
}
cout << maxString << endl;
system("pause");
return 0;
}
string caesarShift(string text, int shift)
{
shift = shift % 26;
char ch = 0;
char chs = 0;
for(unsigned int i=0; i < text.size();i++)
{
ch = text[i];
if( isalpha(ch) )
{
chs = ch -shift;
if( (islower(ch) && chs < 'a' )
||
( isupper(ch) && chs < 'A' ) )
{
chs += 26;
}
text[i] =chs;
}
}
return text;
}