Could I please get help with this problem. The files needed are in the dropbox f
ID: 3732306 • Letter: C
Question
Could I please get help with this problem. The files needed are in the dropbox folder https://www.dropbox.com/sh/bw46ml6xygudywb/AAADErNXmtGmllg8RbQaIMrWa?dl=0
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 which message number is contained in the struct, and two strings, the coded message and the 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 it in the appropriate struct location (i.e., the string member variable of the struct used to 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.Explanation / Answer
//I have included the explanation of the code in comments like these. If you found this helpful, then please give a good review. The output given in the image and the files do not match as newline characters are missing in the provided output file. I have coded to include the newline characters. If you want to change it, I have marked these places to change.
#include <iostream>
#include <fstream>
#include <sstream> //To convert a string of numbers to int
using namespace std;
struct messages1 //Struct that holds the coded and decoded messages
{
int messageNum;
string codedMessage;
string decodedMessage;
};
string decode(string codmess,string &key1) //Function to decode a given message with respect to a key
{
int idx;
string decmess;
for(int i=0;i<codmess.size();i++) //Traversing through the coded message
{
if(isalpha(codmess[i])) //Checking for alphabet characters
{
if(isupper(codmess[i])) //I am also checking if it is in upper case or lower case in order to make coded message case insensitive
idx=codmess[i]-65; //Converting capital letter to its corresponding number
else
idx=codmess[i]-97; //Converting lower letter to its corresponding number
decmess.push_back(key1[idx]); //Pushing the decoded alphabet to the decoded message
}
else
decmess.push_back(codmess[i]); //If the coded character is not an alphabet, it is simply passed to the decoded message
}
return decmess;
}
int main()
{
int nmess;
ofstream output;
output.open("out.txt"); //Opening output file out.txt
string filename,key,temp;
cout<<"Please enter the input file name with its extension: ";
cin>>filename;
ifstream input(filename); //Opening the given input file
if(input.fail()) //Checking for errors
{
output<<"Input File unable to open. ";
return -1;
}
for(int i=0;i<26;i++) //Extracting the key from input file
{
getline(input,temp);
int j=0;
while(isalpha(temp[j]))// Finding the correct character
j++;
while(!isalpha(temp[j]))
j++;
key[i]=temp[j];
}
getline(input,temp); //Taking the number of messages input from input file
stringstream alph(temp); //Converting the string to int
alph>>nmess;
messages1 *arr=new messages1[nmess]; //Dynamically creating an array of struct messages1
for(int i=0;i<nmess;i++)
{
arr[i].messageNum=i+1; //Processing message number
getline(input,arr[i].codedMessage); //Extracting the coded message form input file
arr[i].decodedMessage=decode(arr[i].codedMessage,key); //Decoding the coded message
//If you do not want newline characters then simply skip " " in the next line.
output<<"Message Num: "<<i+1<<" "<<"Coded Message "<<arr[i].codedMessage<<" Decoded Message "<<arr[i].decodedMessage<<" "; //Outputting final results to the file out.txt
}
input.close();//Closing the opened files
output.close();
return 0;
}