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

OPERATORS CPP Write a program will correct a C++ program that has errors in whic

ID: 3840130 • Letter: O

Question

OPERATORS CPP Write a program will correct a C++ program that has errors in which operator, or it uses with cin and cout. The program replaces each (incorrect) occurrence o Cin. with the corrected version Cin and each (incorrect occurrence of cout with the corrected version Clout Allow for the possibility that there may be any number of whitespace characters (one or more) between cin and and between cout and The replacement corrected version has only one blank between the cin or cout and the following operator. You should not correct other whitespace characters in the input file (such as those at the start of a line). Your program should get the source filename as an input from the user. The corrected version should be output to a file called "corrected.txt" (it cannot be called anything else) and the output should also be displayed on the terminal. That is, the output of your program should be exactly same as the contents of the file "corrected.txt Your program should define at least one function that is called a that manipulates the read line from the input file If your program does NOT have at least this function you will not get credit for this part of the assignment, even if your program passes submit cs grading. You will need to use multiple member functions to manipulate strings in this program. In addition, you will have to use the getline0 function in the library in order to read an entire line by ifstream. A session should look like the following example (including whitespace and formatting):

Explanation / Answer

Following is the required C++ code :

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

string improve_line( string line ){
   string result = line;
   int i = 0;
   while( line[i] == ' ' or line[i] == ' ' ){
       i = i + 1;
   }
   if( line.substr(i,4) == "cout" ){
       //got cout
       int start = i;
       i = i + 4;
       string string_to_replace = "cout";
       while( line[i] == ' ' ){
           string_to_replace = string_to_replace + " ";
           i = i + 1;
       }
       string_to_replace = string_to_replace + ">>";
       string replace_with = "cout <<";
       line.replace( start, string_to_replace.size(), replace_with );
   }
   if( line.substr(i,3) == "cin" ){
       //got cin
       int start = i;
       i = i + 3;
       string string_to_replace = "cin";
       while( line[i] == ' ' ){
           string_to_replace = string_to_replace + " ";
           i = i + 1;
       }
       string_to_replace = string_to_replace + "<<";
       string replace_with = "cin >>";
       line.replace( start, string_to_replace.size(), replace_with );
   }
   return line;
}

int main(){
   string filename;
   cout << "Enter filename:" << endl;
   /*cin >> filename;*/
   filename = "file.txt";
   ifstream in( filename );

   ofstream out( "./corrected.txt" );
   string line;
   while( getline(in,line) ){
       line = improve_line( line );
       cout << line << endl;
       out << line << endl;
   }
}