i have to make a program which reads each chaacter from a file and using the caesar cipher if the character is an alpha it should be shifted by the user specified number, if it is not an alpha it should be left alone, including spaces. i am so close, everyhting works except for the program not reading the spaces and putting in spaces where they need to be in the outfile
thanks in advance!!!
here is my program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
#include<iostream>
#include<cstdlib>
#include<fstream>
using namespace std;
char encode(char&, int);
char decode(char&, int);
int main()
{
cout << "Enter E to encode a file or D to decode a file: ";
char choice;
cin >> choice;
while(choice != 'E' && choice != 'D')
{
cout << "Enter E to encode a file or D to decode a file: ";
cin >> choice;
}
if(choice == 'E')
cout << "Enter the name of the file to be encoded: ";
if(choice == 'D')
cout << "Enter the name of the file to be decoded: ";
ifstream inFile;
string inputFilename;
cin >> inputFilename;
inFile.open(inputFilename.c_str());
while(inFile.fail())
{
cout << inputFilename << " does not exist, enter new file name: ";
cin >> inputFilename;
inFile.open(inputFilename.c_str());
}
if(choice == 'E')
cout << inputFilename << " will be encoded and written to a new file.\n";
if(choice == 'D')
cout << inputFilename << " will be decoded and written to a new file.\n";
cout << "What would you like to name this file? ";
string outputFilename;
cin >> outputFilename;
ifstream checkFile;
checkFile.open(outputFilename.c_str());
if(!checkFile.fail())
{
cout << outputFilename << " already exists.\n"
<< "Do you want to overwrite the file?\n"
<< "Enter Y to overwrite, or any other character to quit: ";
char decision;
cin >> decision;
if(decision != 'Y')
return 0;
}
ofstream outFile;
outFile.open(outputFilename.c_str());
cout << "Enter the key (an integer): ";
int key;
cin >> key;
char letter;
if(choice == 'E')
{
while(inFile.good())
{
if(letter == 32)
outFile << " "; // i thought this would work but it doesnt
inFile >> letter;
encode(letter, key);
outFile << letter;
}
}
if(choice == 'D')
{
while(inFile.good())
{
if(letter == 32)
outFile << " "; // i thought this would work but it doesnt
inFile >> letter;
decode(letter, key);
outFile << letter;
}
}
return 0;
}
char encode(char& t, int k)
{
if(t >= 'A' && t <= 'Z')
t = (t - 'A' + k) % 26 + 'A';
else if (t >= 'a' && t <= 'z')
t = (t - 'a' + k) % 26 + 'a';
return t;
}
char decode(char& t, int k)
{
if(t >= 'A' && t <= 'Z')
t = (t - 'A' - k + 26) % 26 + 'A';
else if (t >= 'a' && t <= 'z')
t = (t - 'a' - k + 26) % 26 + 'a';
return t;
}
|
here is an example file:
a b c d e f g h ijklmn o p q r s t u v w x y z
A B C D E F G H I J K LMNOP Q R S T U V W X Y Z
1 2 34 5
this is what the program should do if i encode with a key of 5:
f g h i j k l m nopqrs t u v w x y z a b c d e
F G H I J K L M N O P QRSTU V W X Y Z A B C D E
1 2 34 5
here is what i get with my program:
fghijklmnopqrstuvwxyzabcdeFGHIJKLMNOPQRSTUVWXYZABCDE123455
all i have to do is figure out how to get it to read the white spaces and put white spaces in the right place and why does my program put an extra 5 in the file at the end?