\"C ++ In this assignment, you are to write a ROT13 (for \"rotate 13\") decoder
ID: 3549601 • Letter: #
Question
"C ++ In this assignment, you are to write a ROT13 (for "rotate 13") decoder to translate a secret message from its encoded state to something readable. In a ROT13 code, 'a' is shifted 13 letters to 'n', 'B' to 'O', 'c' to 'p', 'D' to 'Q', ..., 'N' to 'A', 'o' to 'b', 'P' to 'C', 'q' to 'd', etc. Only letters are affected; punctuation, spaces, and all other characters remain the same. Your program should read the secret message out of a file called "secretMessage.txt" and should write the decoded message into a file called "decodedMessage.txt" Please take care to name the files exactly as shown in this description, paying particular care to capitalization."
okay so i am in a beginner C++ class, CS1400. We have been asked to write this decoder program. I can get a good chunk of it written but i have no idea what to write in my if else statements. Some of the stuff i have written in the program is from the teachers notes so i know that i probably have stuff i dont need. Here is what i have so far. I need the program to write to the file but i also would like it to print to my screen. Please dont use advanced techniques im just learning all this stuff. Thanks for the help!
#include <iostream>
#include <cmath>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ifstream inputFile;
ofstream outputFile;
char ch;
inputFile.open("secretMessage.txt");
outputFile.open("decodedMessage.txt");
ch=inputFile.get();
while (!inputFile.eof())
{
if ((ch>='A') && (ch<='Z'))
{
ch+=13;
}
else if ((ch>='a') && (ch<='z'))
{
ch+=13
}
ch=inputFile.get();
}
inputFile.close();
outputFile.close();
cout<<endl<<endl;
return 0;
}
Explanation / Answer
#include <iostream>
#include <cmath>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ifstream inputFile;
ofstream outputFile;
char ch;
inputFile.open("secretMessage.txt");
outputFile.open("decodedMessage.txt");
ch=inputFile.get();
while (!inputFile.eof())
{
if ((ch>='A') && (ch<='Z'))
{
// since you already shifted to 13 letters.. now move back 13 letters
// if we get negative value we have to add 26 to compensate
// because we have total 26 letters.
int error = (ch-'A'-13)<0?(ch-'A'-13+26):(ch-'A'-13);
ch = error%26+'A';
}
else if ((ch>='a') && (ch<='z'))
{
// since you already shifted to 13 letters.. now move back 13 letters.
int error = (ch-'a'-13)<0?(ch-'a'-13+26):(ch-'a'-13);
ch = error%26+'a';
}
outputFile << ch;
ch=inputFile.get();
}
inputFile.close();
outputFile.close();
cout<<endl<<endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.