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

C++ Make Up Homework 3 File I/O and Structs To be turned in on the server, in a

ID: 3728728 • Letter: C

Question

C++

Make Up Homework 3 File I/O and Structs To be turned in on the server, in a directory named hw3_makeup 1 - Purpose The purpose of this assignment is to provide extra practice on file I/O and Structs with the use of dynamic arrays 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

Explanation / Answer

#include<iostream>
#include<fstream>
using namespace std;

struct Messages{
int messageNum;
string codedMessage;
string decodedMessage;
};

int main(){
string fileName;
cout<<"please enter the file name with its extension :";
cin>>fileName;

ifstream infile(fileName.c_str());
char arr[26];

if(infile.is_open()){
  
  char firstChar;
  char secondChar;
  char thirdchar;
  
  for(int i=0;i<26;i++){
   infile >>firstChar;
   infile>>secondChar;
   infile>>thirdchar;
   arr[firstChar - 'A'] = thirdchar;
  }
  
  int numberOfMessages = 0;
  infile >>numberOfMessages;
  
  Messages *msg = new Messages[numberOfMessages];
  
  int i =0;
  string line;
  getline(infile, line);
  for ( line; std::getline(infile, line); i<numberOfMessages){
        msg[i].messageNum = i+1;
        msg[i].codedMessage = line;
       
        // TO decode the msg
       
        for(int j=0; j<line.size(); j++){
          if(line[j] - 'A' >= 0 && line[j]- 'A' <26)
           line[j] = arr[line[j]- 'A'];
   }
        msg[i].decodedMessage = line;
        ++i;
    }
  
  
  ofstream outputFile; // Output File
  outputFile.open("output.txt"); // Create and open the output file
  if(outputFile.is_open()){
   for(int i=0;i<numberOfMessages; i++){
    outputFile<<"Message Num: "<<msg[i].messageNum<<" ";
    outputFile<<"Coded Message ";
    outputFile<<msg[i].codedMessage<<" ";
    outputFile<<"Decoded Message ";
    outputFile<<msg[i].decodedMessage<<" ";
   }
  }else{
   cout<<"Unable to open output file"<<endl;
  }
   
}else{
  cout<<"Input File Unable to open"<<endl;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote