Search code examples
c++ifstreamofstream

Why does my program only change some of the letters


So, I am trying to make a ROT13decoder, and this is what I have so far. Only some of the letters change though and I am not sure why. I'm very new to programming. I am just trying to figure out how to read in files, and write to files. So far that part works, but yeah it doesn't change all the letters in the original file, just some of them. I would really appreciate any feedback.

#include <iostream>
#include <fstream>
//the letters in the secretMessage file are "Lbh unir gb fgnl va funcr. Zl tenaqzbgure, fur fgnegrq jnyxvat svir zvyrf n qnl jura fur jnf 60. Fur’f 97 gbqnl naq jr qba’g xabj jurer gur uryy fur vf
//
//
//and this is what it outputs to the decodedMessage file "Lbh haie gb fgal ia fhace. Ml geaadmbghee, fhe fgaeged jalkiag fiie milef a dal jhea fhe jaf 60. Fhe’f 97 gbdal aad je dba’g kabj jheee ghe hell fhe if.

using namespace std;


int main(){
    ofstream fout;
    ifstream fin;
    fin.open("secretMessage.txt");
    fout.open("decodedMessage.txt");


    char c = 0;



    while (!fin.eof()){
        c = fin.get();
        if (c == 'a')c = 'n';
        if (c == 'b')c = 'o';
        if (c == 'c')c = 'p';
        if (c == 'd')c = 'q';
        if (c == 'e')c = 'r';
        if (c == 'f')c = 's';
        if (c == 'g')c = 't';
        if (c == 'h')c = 'u';
        if (c == 'i')c = 'v';
        if (c == 'j')c = 'w';
        if (c == 'k')c = 'x';
        if (c == 'l')c = 'y';
        if (c == 'm')c = 'z';
        if (c == 'n')c = 'a';
        if (c == 'o')c = 'b';
        if (c == 'p')c = 'c';
        if (c == 'q')c = 'd';
        if (c == 'r')c = 'e';
        if (c == 's')c = 'f';
        if (c == 't')c = 'g';
        if (c == 'u')c = 'h';
        if (c == 'v')c = 'i';
        if (c == 'w')c = 'j';
        if (c == 'x')c = 'k';
        if (c == 'y')c = 'l';
        if (c == 'z')c = 'm';


        if (c == 'A')c = 'N';
        if (c == 'B')c = 'O';
        if (c == 'C')c = 'P';
        if (c == 'D')c = 'Q';
        if (c == 'E')c = 'R';
        if (c == 'F')c = 'S';
        if (c == 'G')c = 'T';
        if (c == 'H')c = 'U';
        if (c == 'I')c = 'V';
        if (c == 'J')c = 'W';
        if (c == 'K')c = 'X';
        if (c == 'L')c = 'Y';
        if (c == 'M')c = 'Z';
        if (c == 'N')c = 'A';
        if (c == 'O')c = 'B';
        if (c == 'P')c = 'C';
        if (c == 'Q')c = 'D';
        if (c == 'R')c = 'E';
        if (c == 'S')c = 'F';
        if (c == 'T')c = 'G';
        if (c == 'U')c = 'H';
        if (c == 'V')c = 'I';
        if (c == 'W')c = 'J';
        if (c == 'X')c = 'K';
        if (c == 'Y')c = 'L';
        if (c == 'Z')c = 'M';
        cout << c;
        if (!fin.eof())fout << c;
    }


    fin.close();
    fout.close();



    return 0;
}

Solution

  • Most letters in your example will be flipped twice. You either need to add a whole lot of 'else' commands or use a switch statement.

    if (c == 'a')c = 'n';
    else if (c == 'b')c = 'o';
    

    There is a better, mathematical method; but I'll leave that as an exercise to the reader.