I'm working on an exercise in my C++ textbook that asks me to create a program to "encrypt" a given input text file and print the encrypted text to an output file. The "encryption" is fairly simplistic and just consist of moving every alphabetical character two places down the list in the alphabet. So, a would be encrypted as c, e would be encrypted as g, etc.
My approach has been to assign a declared integer object "converter" to the value "input +2" where "input" is the character read from the file. I use a switch statement to ensure that grammatical characters like period, comma, etc, are copied with no transformation.
I am having the damndest time trying to figure out how I can then print "converter" as a character rather than an integer. It's not that I don't know how to use the putchar() function - it's that I can't get this function to write the output to my output text file rather than stream it to the standard output!
Does anybody know how to manipulate this function to print to an output file rather than standard output? Is there perhaps another function that I am not aware of which I should be using? Here is my code:
/******************************************************************************
Problem 5-18
*******************************************************************************/
#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
//Declare and Initialize Objects
char input, output;
int converter;
string filename;
ifstream fin;
ofstream fout;
//Collect Name of Input File
cout << "Enter filename: \n" << endl;
cin >> filename;
//Open Input File and Check for Errors
fin.open(filename);
if(fin.fail())
{
cerr << "Error opening file " << filename << endl;
exit(1);
}
//Open Output filename
fout.open("Report.txt");
//Read Input File and Transcribe to Code
while(!fin.eof())
{
fin >> input;
switch(input)
{
case ' ':
fout << input;
break;
case '.':
fout << input;
break;
case ',':
fout << input;
break;
default:
converter = (input + 2);
fout << putchar(converter);
break;
}
}
return 0;
Your help would be greatly appreciated!