I have an assignment using a version of Ceasar cipher but it shifts characters in a file based on user input. For instance, if the user enters the shift value as 1, it would change 'a' to 'b'. I've tried typing to the out file and adding the shift value to the characters but it doesnt output anything to the out1 file and I can't figure out the right way to type this so the code does what I want it to do. Any info would help, here's my code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cctype>
using namespace std;
double Percent(double&, double&);
int main()
{
//Declare user variables
int shift;
char ifilename[25], ofilename[25], ch;
ifstream in1;
ofstream out1;
double other = 0, letter, nums = 0;
//Attach files
do {
in1.clear(); //clear status flags
//Prompt user to enter name of input file and amount of shift
cout << "Please enter the name of the input file." << endl;
cout << "Filename: ";
cin >> ifilename;
//Open file name
in1.open(ifilename);
//Error message if no file
if (!in1)
cout << "That is not a valid file. Try again\n";
}
while (!in1);
do {
out1.clear(); //clear status flags
cout << "Please enter the name of the output file." << endl;
cout << "Filename: ";
cin >> ofilename;
out1.open(ofilename);
//Error message if no file
if (!out1)
cout << "That is not a valid file. Try again\n";
}
while (!out1);
//prompt user to enter shift
cout << "Please intput the shift amount: ";
cin >> shift;
cout << "Processing complete" << endl;
double count = 0;
int sum = 0, i = 0;
while (!in1.eof()) // while not end of input file
{
in1.get(ch); // read a character from input file using get()
out1 << ch; //print to output
count++; //count how many characters
i = ch;
if (i > 47 && i < 58) //count number of numbers
nums++;
else if ((i > 31 && i < 48) || (i > 57 && i < 65) || (i > 90 && i < 97) || (i > 122 && i < 127))
other++; // count number of other characters
else if ((i > 64 && i < 91) || (i > 96 && i < 123))
letter++; // count number of letters
// type to output file and shift characters
out1 << ch + shift;
}
//Tell user file input is complete and is now printing statistics
cout << "\nShifted input file Complete. Now printing statistics " << endl;
//Show statistics for file
cout << "\nStatistics for file: " << ifilename << endl;
cout << "------------------------------------------------";
//Show characters in file and stats before shift
cout << "\n\nTotal # of characters in file: " << count << endl;
cout << "Statistics before shift: " << endl;
cout << "\n\nCategory" << setw(30) << "How many in file" << setw(20) << "% of file" << endl;
cout << "----------------------------------------------------------------" << endl;
cout << "Letters" << setw(25) << letter << setw(20) << setprecision(4) << letter / count * 100 << " %" << endl;
cout << "Digits" << setw(26) << nums << setw(20) << setprecision(4) << nums / count * 100 << " %" << endl;
cout << "Other" << setw(27) << other << setw(20) << setprecision(4) << other / count * 100 << " %" << endl;
//Show user stats after shift
cout << "\nStatistics after shift: " << endl;
cout << "\n\nCategory" << setw(30) << "How many in file" << setw(20) << "% of file" << endl;
cout << "----------------------------------------------------------------" << endl;
cout << "Letters" << setw(25) << letter << setw(20) << setprecision(4) << Percent(letter, count) << " %" << endl;
cout << "Digits" << setw(26) << sum << setw(20) << setprecision(4) << nums / count * 100 << " %" << endl;
cout << "Other" << setw(27) << nums << setw(20) << setprecision(4) << other / count * 100 << " %" << endl;
//Close files
out1.close();
in1.close();
return 0;
}
double Percent(double&, double&)
{
double sum, letter, count;
sum = letter / count * 100;
return sum;
}