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

Make a std::ofstream object (instead of an std::ifstream object, as you would for reading in). Then use it just as you would std::cout (don't forget to close the file when you're done)

Okay so where would I put the ofstream? I was told by a classmate to use

ofstream outFile;

outFile.open("outfile.txt");

outFile >> Whatever you want to put in the file...

outFile.flush();

outFile.close();

But how would I integrate that into the rest of the problem? Would I put that near line 46, or would it go somewhere near the beginning?

Is it maxString that contains the final output? If so, you just need to open the file just before you want to write to it (around line 41 in your case, I think), write to it, then close it after you're done. You don't need to worry about calling fflush() , the file stream will be flushed before it's closed. Also, you want to use << to write to the file, not >> as you have in your previous post :)

P.S. when you open a file (for any reason) it really pays to check that it opened correctly (by checking is_open() )

Edited 4 Years Ago by ravenous: n/a

Sorry for not responding sooner, I've been busy with my finals. Thanks for the help, I got it solved.

Is it maxString that contains the final output? If so, you just need to open the file just before you want to write to it (around line 41 in your case, I think), write to it, then close it after you're done. You don't need to worry about calling fflush() , the file stream will be flushed before it's closed. Also, you want to use << to write to the file, not >> as you have in your previous post :)

P.S. when you open a file (for any reason) it really pays to check that it opened correctly (by checking is_open() )

This question has already been answered. Start a new discussion instead.