-1
void transferData(ifstream& input_file, ofstream& output_file)
{
    char ch;
    while (!(input_file.eof())) {
        input_file.get(ch);
        if ((ch >= 97 && ch <= 118) || (ch <= 65 && ch <= 86)) {
            ch += 4;
            output_file << ch;
        }
        else if ((ch >= 87 && ch <= 90) || (ch <= 119 && ch <= 122)) {
            ch -= 22;
            output_file << ch;
        } else
            output_file << ch;

    }
}

I intended for it to change each letter to the one 4 places ahead of it in the alphabet. For some reason, it changes spaces and dots

Text in the input file: "This text is to test the program."

Text in the output file: "lmw$xixx$mw$xs$xiwx$xli$tvskveq26"

PointBlank
  • 11
  • 3
  • Your boolean expressions are wrong. In the 2nd bracket – Armin Montigny Nov 28 '19 at 19:20
  • 3
    Don't use magic numbers. `97` is, I guess, supposed to be `'a'`. If that's right, don't make me guess; write it that way: `if(ch >= 'a' && ch <= 'v'` etc. – Pete Becker Nov 28 '19 at 19:36
  • `input_file.eof()` does not tell you that the file is at the end. It tells you that the previous read failed because the file was already at the end. (not quite that simple, but close enough for the moment). So change that loop to `while (input_file.get(ch))`. That will exit the loop when the attempted input fails, instead of processing `ch` after a failed attempt to read a new value. – Pete Becker Nov 28 '19 at 19:48

1 Answers1

1

Read your code more carefully.

ch <= 65 && ch <= 86
ch <= 119 && ch <= 122

That's every character under 87, and every character under 123.

I think you meant >= at the start mate.

Also, your loop condition is broken. Don't use eof() like that.

Lightness Races in Orbit
  • 358,771
  • 68
  • 593
  • 989