2 - The Problem You will be provided with an input file that has a series of enc
ID: 3731724 • Letter: 2
Question
2 - The Problem You will be provided with an input file that has a series of encoded messages. You will need to read in the file and decode the messages. You must check to make sure that the input file has been opened. If not, you need to print "Input File unable to open." to the output file The first 26 lines of the input file will tell you what each coded letter should equal. For example: 'A = Z' means that for every A in a coded message, it should be replaced with a Z. You should store this information in an array for easy decoding. This set of information will always be presented A through Z so that you may assign an array position to a letter, i.e., index 0 will always be A. The next number in the file will be the number of coded messages that will be contained in the file. This number should be used to make a dynamic array of the structs (i.e., the number is the size of the dynamic array). You will need to create a struct that contains an integer type of variable that keeps track of decoded message struct messages ( int messageNum string codedMessage string decodedMessage: Then, using the getline function, you should read in a whole line of the file at a time and store the coded message). You will then decode the message and store it in the appropriate struct location. A function is recommended for decoding, but not required. Once all the messages are decoded, you should go over the dynamic array and print each element with its corresponding message number, the coded message, and the decoded message, to the file out.txt. 3 - Example Input and Output The following is the message seen on your screen: Please enter the input file name with its extension: testinput.txt Process finished with exit code 0Explanation / Answer
#include <iostream>
#include <sstream>
#include <string>
#include <fstream>
using namespace std;
struct message{
int messageNum;
string codedMessage;
string decodedMessage;
};
int main() {
string fileName;
cout<<" Please enter the input file name with its extension";
getline(cin, fileName);
ifstream infile(fileName);
if(infile.fail()){
cout<<" Error opening file";
}
string line;
char array[26];
int numberOfLines = 0;
int numberOfData = 0;
int i=0;
struct message *m;
//read file line by line
while(getline(infile, line))
{
istringstream iss(line);
//first 26 non null lines contains decoding rules
if(numberOfLines < 26){
char a,b;
if(!(iss>>a>>b)) {break;}
array[numberOfLines++] = b;
}
// 26th non null line contains number of messages
if(numberOfLines == 26)
{
if(!(iss>>numberOfData)) {break;}
m = malloc(sizeof(struct message) * numberOfData);
numberOfLines++;
}
if(numberOfLines > 26)
{
int n;
string c;
string d;
if(!(iss>>n>>c>>d)) {break;}
m[i].messageNum = n;
m[i].codedMessage = c;
for(char&ch : c){
if(!ch) {break;}
d = d + (array[ch-'A']);
}
m[i].decodedMessage = d;
i++;
}
}
//print the data
for( int j = 0; j<i; j++){
cout<<" message num coded message decoded message";
cout<<" "<<m[j].messageNum<<" "<<m[j].codedMessage<<" "<<m[j].decodedMessage;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.