Here is the encoded message – Mrkq#6=49#Dpsolilhg#Eleoh#+DPS,#%Iru#Jrg#vr#^juhdw
ID: 3712048 • Letter: H
Question
Here is the encoded message –
Mrkq#6=49#Dpsolilhg#Eleoh#+DPS,#%Iru#Jrg#vr#^juhdwo|`#oryhg#dqg#ghduo|#sul}hg#wkh#zruog/#wkdw#Kh#^hyhq`#jdyh#Klv#^Rqh#dqg`#^d`rqo|#ehjrwwhq#Vrq/#vr#wkdw#zkrhyhu#eholhyhv#dqg#wuxvwv#lq#Klp#^dv#Vdylru`#vkdoo#qrw#shulvk/#exw#kdyh#hwhuqdo#olih1
Copy this text and save it to a text file.
Write a C++ program that does the following:
Creates a class called Paragraph. It will have one private member variable of type string.
Opens the text file and reads in the text and stores it in an instance of “Paragraph”.
Decode the message by subtracting 3 from the ASCII value for each character (For example, ‘M’ becomes ‘J’, and ‘#’ becomes a space character).
Store the decoded message in another instance of “Paragraph”.
Output the decoded message to another text file.
If this is done successfully, you will see an English, decoded message in the new text file.
I'm having touble with this one because the it does not out put the text on my cout statements, so I'm thinking my getMessage function isn't right.
My C++ code below:
#include
#include
#include
using namespace std;
class Paragraph {
string message; //private string variable
public:
void setMessage(string m);
string getMessage();
}; Paragraph pCode, pDecode;
//public member functions to access and modify private member message
void Paragraph::setMessage(string m) {
message = m;
}
string Paragraph::getMessage() {
return message;
}
int main() {
//Paragraph pCode;
//Paragraph pDecode;
string userM;
string mCode;
string mDecode;
string mNormal;
ifstream inFS;
inFS.open("myfile.txt");
if (!inFS.is_open()) { //checking for errors in opening file
cout << "Could not open myfile.txt" << endl;
}
if (inFS.is_open()) {
while (getline(inFS, userM)) {
mDecode = userM; //assigning the message to a new variable
}
pCode.setMessage(userM);
inFS.close(); //done with file so closing file
size_t i = 0; //decaring and initializing i
char letter;
while (i < userM.length()) {
letter = userM[i] - 3;
mNormal = mNormal + letter;
i++;
}
pDecode.setMessage(mNormal);
cout << "The original text stored in myfile.txt: " << pCode.getMessage() << endl;
ofstream ofile;
ofile.open("oNormal.txt");
ofile << mNormal;
cout << "The decoded text: " << pDecode.getMessage() << endl;
ofile.close();
}
system("pause");
return 0;
}
Please help!!!
Explanation / Answer
Screenshot
---------------------------------------------------------------------------------------------------------------------------------
Program
//header files
#include <iostream>
#include <iomanip>
#include <fstream>
#include<string>
using namespace std;
//Class paragraph
class Paragraph {
//Private string member variables original and decoded messages
string message;
string decodeMessage;
public:
//Default constructor
Paragraph() {
message = "";
decodeMessage = "";
}
//Mutator to get encrypted message
void setMessage(string m) {
message = m;
}
//Accessor for get the encrypted message
string getMessage() {
return message;
}
//Decode each character of the string
string decode() {
for (int i = 0; i < size(message); i++) {
int val = int(message[i]) - 3;
char c = char(val);
decodeMessage+=c;
}
return decodeMessage;
}
//toString method if you want to print data instead of write into a file
string toString() {
decode();
return "Encrypted message : "+message+" Decoded Message : "+decodeMessage;
}
};
//main method
int main() {
//Variables to read and write in files
string line,decoded;
//Read file object
fstream fileName;
fileName.open("C:/Users/deept/Desktop/myFile.txt");
//Write file object
ofstream outputFile;
outputFile.open("C:/Users/deept/Desktop/oNormal.txt");
//Read file and set the paragraph class member for decode
if (!fileName) {
cout << "Error: Can't open the file named data.txt. ";
exit(1);
}
while (fileName>>line) {
Paragraph p;
p.setMessage(line);
decoded = p.decode();
cout << p.toString() << endl;
}
//Write the decoded message into a file
if (!outputFile) {
cout << "Error: Can't open the file named data.txt. ";
exit(1);
}
else {
outputFile << decoded;
}
return 0;
}