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

Write a program that will correct a C++ program that has errors in which operato

ID: 3666007 • Letter: W

Question

Write a program that will correct a C++ program that has errors in which operator, << or >>, it uses with cin and cout. The program replaces each (incorrect) occurrence of

with the corrected version

and each (incorrect) occurrence of

with the corrected version

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.

Your program should get the source filename as an input from the user. The corrected version should be output to a file with name“corrected.txt” and it should also be displayed on the terminal. That means output of your program (after getting the filename) should be exactly same as the contents of the file “corrected.txt”. Your program should define a function that is called with the input- and output-file streams as arguments.

A session should look like the following example (including whitespace and formatting):

File “corrected.txt” consists of the above output except first two lines.

Explanation / Answer

Please check below code for reference:

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cmath>
#include <string>
#include <iomanip>
#include <cstring>

using std::exit;
using std::cin;
using std::cout;
using std::ostream;
using std::istream;
using std::ifstream;
using std::ofstream;
using std::endl;
using std::pow;
using std::sqrt;
using std::getline;
using std::string;
using std::ios;
using std::setw;

const void correct(ifstream& fin,ofstream& fout);
int main()
{
   ifstream fin;
   ofstream fout;
   cout << "AverageLengthFromFile" << endl;

   fin.open("corrected.txt");

   if(fin.fail())
   {
       cout << "Fin.Fail()";
       exit(1);
   }

   fout.open("cincoutaft.txt");

   if(fout.fail())
   {
       cout << "Fout.Fail()";
       exit(1);
   }

   correct(fin,fout);

   fin.close();
   fout.close();

   return 0;
}

const void correct(ifstream& fin,ofstream& fout)
{
   char next;
   cout << "CinCoutCorrection" << endl;
   fin.get(next);
   while(!fin.eof())
   {
       if(next == 'c')
       {
           fin.get(next);
           if(next == 'i')
           {
               fin.get(next);
               if(next == 'n')
               {
                   fin >> next;
                   if(next == '<')
                   {
                       fin >> next;
                       if(next == '<')
                       {
                           fout << "cin >>";
                           fin.get(next);
                       }
                       else
                       {
                           fout << "cin >" << next;
                           fin.get(next);
                       }
                   }
                   else
                   {
                       fout << "cin " << next;
                       fin.get(next);
                   }
               }
               else
               {
                   fout << "ci" << next;
                   fin.get(next);
               }
           }
           else if(next == 'o')
           {
               fin.get(next);
               if(next == 'u')
               {
                   fin.get(next);
                   if(next == 't')
                   {
                       fin >> next;
                       if(next == '>')
                       {
                           fin.get(next);
                           if(next == '>')
                           {
                               fout << "cout <<";
                               fin.get(next);
                           }
                           else
                           {
                               fout << "cout >" << next;
                           }
                       }
                       else
                       {
                           fout << "cout " << next;
                           fin.get(next);
                       }
                   }
                   else
                   {
                       fout << "cou" << next;
                       fin.get(next);
                   }
               }
               else
               {
                   fout << "co" << next;
                   fin.get(next);
               }
           }
           else
           {
               fout << "c" << next;
               fin.get(next);
           }
       }
       else
       {
           fout << next;
           fin.get(next);
       }
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote