Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

IN C++ You will be provided with an input file that has a series of encoded mess

ID: 3909945 • Letter: I

Question

IN C++

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.

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 array of the structs (i.e., the number is the size of the 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.

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 array of struct 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

You have been provided with 5 input files for each test case in Black board(BB) and a sample output file too. BB consist of 5 input files namely input1.txt for test case 1, input2.txt for test case 2, input3.txt for test case 3, input4.txt for test case 4, input5.txt for test case 5.

You need to provide input file name in zybooks, for example : input1.txt

Contents of input1.txt

Please note in your output file print a newline (outToFile<<endl;) at the end of the output in the output file. Zybooks will look for a newline printed in the file at the end of output file.

Please note: There are a total of 10 test cases. You have been provided with 5 test cases that are available to you and you can test your code against the 5 test cases. The other 5 test cases are not available to you that we will use to test your code.

You need to follow the following naming methodology otherwise test case will fail.

struct messages int messageNum; string codedMessage; string decodedMessage;

Explanation / Answer

Below is the C++ code I hope that i have provided sufficient comments for your better understanding

#include <bits/stdc++.h>
using namespace std;

struct msg
{
int msgnumber;
string codedmsg;
string decodedmsg;
};

int main()
{
int i;
char a[26];
char ch;

cout<<"Enter new character for every character"<<endl;
for(ch='A';ch<='Z';ch++)
{
cout<<ch<<" = ";
cin>>a[ch-'A'];
}

int n,j,len;

//Take user's input
cout<<"Enter number of messages : ";
cin>>n;

msg m[n];

string s;
for(i=0;i<n;i++)
{
cout<<"Enter coded message : ";
getline(cin,s);
m[i].codedmsg=s;

len=s.size();

//Traverse and decode the coded message
s="";
for(j=0;j<len;j++)
{
ch=m[i].codedmsg[j];

//replace the character
if(ch>='A'&&ch<='Z')
{
s=s+a[ch-'A'];
}
else
s+=m[i].codedmsg;
}
m[i].decodedmsg=s;
cout<<"Decoded message is : "<<m[i].decodedmsg<<endl;
}
return 0;
}


Hope i have answered your question satisfactorily.Leave doubts in comment section if any.