Hello,
I have a program that decodes a substitution cypher one character at a time and then prints it on the screen. I need to save this text into a output file on disk. I am having hard time figuring out how to get the decoded text from the screen into a text file. I posted my code below, does anyone have an idea how to do this?

Thanks for any help.

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

int letterCounter[25] = {0};
void counter(char ch);
int funcOffset();

using namespace std;

int main() {
    string fileLocation;
    ifstream textFile;
    ofstream outcode;
    char outFile;
    char character;
    int characterDecodedInt;
    int index;
    char characterDecodedChar;
    string finalMessage;
    int offset;
    string quit;
    
    //Ask the user to give the location of the file, and open it.
    cout << "Input the location of the file you want to decode: ";
    getline(cin, fileLocation);
    textFile.open(fileLocation.c_str());
    
    //Check if the file opened successfully, and prompt them to go again if it didn't.
    while (!(textFile.is_open())) {
        cout << "Invalid file location. Please try again: ";
        getline(cin, fileLocation);
        textFile.open(fileLocation.c_str());
    }
    
    //Adds up the total number of times each letter appeared in the file.
    while (textFile.peek() != EOF) {
        textFile.get(character);
        counter(character);
    }

    /*Finds out which letter appeared most, which should be a coded E, and then
    determines the offset based on this.*/
    offset = funcOffset();
    cout << "Current ASCII offset is: " << offset << endl;
    
    //Resets the cursor to the beginning of the file
    textFile.clear();
    textFile.seekg(0);
    
    //Rereads and decodes the text file, one letter at a time.
    while (textFile.peek() != EOF) {
        textFile.get(character);
        character = (static_cast<int>(character));
            if ( character < 65 || (character > 90 && character < 97) || character > 122){
              characterDecodedChar = static_cast<char>(character);
              } 
            if (character > 96 && character < 123){
               characterDecodedInt = ((static_cast<int>(character) + offset));
               if (characterDecodedInt > 96 && characterDecodedInt < 123)
                  characterDecodedChar = static_cast<char>((characterDecodedInt)); 
                     else if (characterDecodedInt > 122)
                     characterDecodedChar = static_cast<char>((characterDecodedInt - 26));
                        else if (characterDecodedInt < 97)
                           characterDecodedChar = static_cast<char>((characterDecodedInt + 26));
           }
        if (character > 64 && character < 91){
           characterDecodedInt = static_cast<int>((static_cast<int>(character) + offset));
           if (characterDecodedInt > 64 && characterDecodedInt < 91)
              characterDecodedChar = static_cast<char>((characterDecodedInt));
                  else if(characterDecodedInt > 90)
                      characterDecodedChar = static_cast<char>((characterDecodedInt - 26));
                          else if (characterDecodedInt < 65)
                              characterDecodedChar = static_cast<char>((characterDecodedInt + 26));
            }
            cout << characterDecodedChar;
            }
    
              
    cout << "\n\nEnd of message.";

    cout << endl << endl;
    cout << "Press enter to quit.";
    getline(cin, quit);
    return 0;
    
}

/*The purpose of the following function is to count up the number of times each
letter appears in the text file.*/
void counter(char ch) {
    switch(ch) {
        case 'a':
        case 'A':
            letterCounter[0]++;
            break;
        case 'b':
        case 'B':
            letterCounter[1]++;
            break;
        case 'c':
        case 'C':
            letterCounter[2]++;
            break;
        case 'd':
        case 'D':
            letterCounter[3]++;
            break;
        case 'e':
        case 'E':
            letterCounter[4]++;
            break;
        case 'f':
        case 'F':
            letterCounter[5]++;
            break;
        case 'g':
        case 'G':
            letterCounter[6]++;
            break;
        case 'h':
        case 'H':
            letterCounter[7]++;
            break;
        case 'i':
        case 'I':
            letterCounter[8]++;
            break;
        case 'j':
        case 'J':
            letterCounter[9]++;
            break;
        case 'k':
        case 'K':
            letterCounter[10]++;
            break;
        case 'l':
        case 'L':
            letterCounter[11]++;
            break;
        case 'm':
        case 'M':
            letterCounter[12]++;
            break;
        case 'n':
        case 'N':
            letterCounter[13]++;
            break;
        case 'o':
        case 'O':
            letterCounter[14]++;
            break;
        case 'p':
        case 'P':
            letterCounter[15]++;
            break;
        case 'q':
        case 'Q':
            letterCounter[16]++;
            break;
        case 'r':
        case 'R':
            letterCounter[17]++;
            break;
        case 's':
        case 'S':
            letterCounter[18]++;
            break;
        case 't':
        case 'T':
            letterCounter[19]++;
            break;
        case 'u':
        case 'U':
            letterCounter[20]++;
            break;
        case 'v':
        case 'V':
            letterCounter[21]++;
            break;
        case 'w':
        case 'W':
            letterCounter[22]++;
            break;
        case 'x':
        case 'X':
            letterCounter[23]++;
            break;
        case 'y':
        case 'Y':
            letterCounter[24]++;
            break;
        case 'z':
        case 'Z':
            letterCounter[25]++;
            break;
        default:
            break;
    }
}

/*The purpose of this function is to determine which letter appeared most often,
identify it as E, and then determine how much the offset should be when decoding*/
int funcOffset() {
    int i;
    int highestNum = letterCounter[0];
    int highestNumIndex = 0;
    char e;
    
    for (i = 0; i < 26; i++) {

        if (letterCounter[i] > highestNum) {
            highestNum = letterCounter[i];
            highestNumIndex = i;
        }
    }
    switch (highestNumIndex) {
        case 0:
            e = 'a';
            break;
        case 1:
            e = 'b';
            break;
        case 2:
            e = 'c';
            break;
        case 3:
            e = 'd';
            break;
        case 4:
            e = 'e';
            break;
        case 5:
            e = 'f';
            break;
        case 6:
            e = 'g';
            break;
        case 7:
            e = 'h';
            break;
        case 8:
            e = 'i';
            break;
        case 9:
            e = 'j';
            break;
        case 10:
            e = 'k';
            break;
        case 11:
            e = 'l';
            break;
        case 12:
            e = 'm';
            break;
        case 13:
            e = 'n';
            break;
        case 14:
            e = 'o';
            break;
        case 15:
            e = 'p';
            break;
        case 16:
            e = 'q';
            break;
        case 17:
            e = 'r';
            break;
        case 18:
            e = 's';
            break;
        case 19:
            e = 't';
            break;
        case 20:
            e = 'u';
            break;
        case 21:
            e = 'v';
            break;
        case 22:
            e = 'w';
            break;
        case 23:
            e = 'x';
            break;
        case 24:
            e = 'y';
            break;
        case 25:
            e = 'z';
            break;
    }
    //This will find the difference in ASCII code between 'e' and our mystery
    //letter, determining what the "shift" is.
    return(static_cast<int>('e') - static_cast<int>(e));
}

Recommended Answers

All 4 Replies

line 77: why don't you just same the characters in a std::string object so that you can easily write it to a file??

function counter() -- its too lengthy and too complicated. The following will work if ch is always an alphabetical letter 'A'-'Z' or 'a'-'z'.

void counter(char ch) {
    char c = tolower(ch);
    lettercounter[c-'a']++;
}

line 77: why don't you just same the characters in a std::string object so that you can easily write it to a file??

how do i go about doing this? I have not encountered that code before, but the way you explain it it seems to be what i need.

line 77: why don't you just same the characters in a std::string object so that you can easily write it to a file??

I get what your saying now, but everytime i try to use a for loop to insert the characters one at at time the program crashes.

I am not sure what i am doing wrong?

I get what your saying now, but everytime i try to use a for loop to insert the characters one at at time the program crashes.

I am not sure what i am doing wrong?

All you have to do is this:

int main()
{
   std::string saveString;
   ..,.
   ...
// line 77
   saveString += characterDecodedChar;
}
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.