So the goal is to use ROT13 and read from a file with the words "JULIUS CAESAR" or "Now is the time for all good men
and women to come
to the aid of their party."
and write it to another file but encrypted, and it needs to keep the formatting. I did read that if I use " << " or " >> " operators it will just ignore the spaces and newline "\n". So how should I write my code to make sure it keeps spaces in tact and other stuff?
Line 57 "while(getline (plainTextFile, ch)" doesn't work because I changed it from "while(plainTextFile >> ch)" and that obviously skips spaces.
output: (ignore the 0 and the test word)
input2.txt data read:
-------------------------------------
JULIUS CAESAR
-------------------------------------
0
input2.txt data converted and stored:
-------------------------------------
WHYVHFPNRFNE testWHYVHFPNRFNE
-------------------------------------
Great!
*It's probably possible to not have my code go from "char ch" to "encrypt" then to only add it to my "outputString" in my switch statement but I was using some old code, so I'm gonna leave it for now.
I just need a space such as: "WHYVHF PNRFNE". Then I just need to take that encryption and write it to another file.
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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
// Defines the input as a character, and an encrypted character
char ch, encrypt;
// Makes a flag for any input errors
bool errorFlag;
string inFileName, outFileName;
cout << "*****************************************\n";
cout << "ROT13 - A Caesar cipher program\n";
cout << "*****************************************\n";
cout << "Enter the name of a text file with alpha characters to convert: ";
cin >> inFileName;
cout << "Enter the data output file to store Caesar cipher translated text: ";
cin >> outFileName;
ifstream plainTextFile;
string inputString, outputString;
plainTextFile.open(inFileName); //(put input of cin filename here)
while(!plainTextFile.is_open())
{
cout << "Unable to open file. Enter correct filename: ";
cin >> inFileName;
plainTextFile.open(inFileName);
}
if(plainTextFile.is_open())
{
cout << "file open \n";
while(getline (plainTextFile, inputString))
{
cout << "\n";
cout << inFileName << " data read:\n";
cout << "-------------------------------------\n";
cout << inputString << "\n";
cout << "-------------------------------------\n";
}
cout << inputString.length() << "\n";
plainTextFile.close();
}
plainTextFile.open(inFileName);
cout << inFileName << " data converted and stored:\n";
cout << "-------------------------------------\n";
while(getline (plainTextFile, ch))
{
// Converts
ch = toupper(ch);
switch (ch)
{
case 'A' :
encrypt = 'N';
break;
case 'B' :
encrypt = 'O';
break;
case 'C' :
encrypt = 'P';
break;
case 'D' :
encrypt = 'Q';
break;
case 'E' :
encrypt = 'R';
break;
case 'F' :
encrypt = 'S';
break;
case 'G' :
encrypt = 'T';
break;
case 'H' :
encrypt = 'U';
break;
case 'I' :
encrypt = 'V';
break;
case 'J' :
encrypt = 'W';
break;
case 'K' :
encrypt = 'X';
break;
case 'L' :
encrypt = 'Y';
break;
case 'M' :
encrypt = 'Z';
break;
case 'N' :
encrypt = 'A';
break;
case 'O' :
encrypt = 'B';
break;
case 'P' :
encrypt = 'C';
break;
case 'Q' :
encrypt = 'D';
break;
case 'R' :
encrypt = 'E';
break;
case 'S' :
encrypt = 'F';
break;
case 'T' :
encrypt = 'G';
break;
case 'U' :
encrypt = 'H';
break;
case 'V' :
encrypt = 'I';
break;
case 'W' :
encrypt = 'J';
break;
case 'X' :
encrypt = 'K';
break;
case 'Y' :
encrypt = 'L';
break;
case 'Z' :
encrypt = 'M';
break;
case ' ' :
encrypt = ch;
break;
case '.' :
encrypt = '.';
break;
case '\n' :
encrypt = ch;
break;
default :
errorFlag = true;
cout << ch << " not found in the ROT13 coding scheme." << endl; // Default catch all other characters
}
if (!errorFlag)
{
cout << encrypt;
outputString += encrypt;
ch++;
}
}
cout << " test" << outputString << "\n";
cout << "-------------------------------------\n";
cout << "Great! ";
//cout << str.length() << "\n";
// make sure to close file plainTextFile.close();
//ofstream translatedText;
// translatedText.open (put name of output file here)
// validation if(translatedText.is_open())
// close file translatedText.close();
return 0;
}
|