Write a program that will correct a C++ program that has errors in which operato
ID: 657528 • 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.
The program to be corrected is in a file with name
Explanation / Answer
#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 corrct(ifstream& fin,ofstream& fout);
int main()
{
ifstream fin;
ofstream fout;
cout << "AvgLengthFile" << endl;
fin.open("cincoutbef.txt");
if(fin.fail())
{
cout << "Fin.Fail()";
exit(1);
}
fout.open("cincoutaft.txt");
if(fout.fail())
{
cout << "Fout.Fail()";
exit(1);
}
corrct(fin,fout);
fin.close();
fout.close();
return 0;
}
const void corrct(ifstream& fin,ofstream& fout)
{
char next;
cout << "CinCoutCorrction - By Kudy" << 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);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.