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

My c++ code will not compile and I have no idea why. Can someone help me? /*This

ID: 3928591 • Letter: M

Question

My c++ code will not compile and I have no idea why. Can someone help me?

/*This program will convert a
c++ txt file to html ready format*/

#include <iostream>
#include <fstream>

using namespace std;

   void convert(ifstream inStream, ofstream outStream){
  
   char ch;
   string filename;

  
   inStream.open("pre-html.txt");
  
       if(inStream.fail()){
           cout << "File failed to open";
           exit(1);
       }
      
  
  
   cout<< endl << "Enter the name of the new file" << endl;
   cin >> filename;
  
   filename = filename + ".html";
  
   outStream.open(filename.c_str());
  
   //writes <PRE> to the new file
   outStream << "<PRE>";
  
  
   //reads the data from the file till the end
   //converts the specified characters to new ones
   //writes the data to a new file of type .html
   while(inStream >> ch){
       cout << ch;
      
       if(ch == '<'){
       outStream << "&lt";
       }
      
       else if(ch == '>'){
           outStream << "&gt";
       }
      
       else(outStream << ch);
   }
  
   outStream << "</PRE>" << endl;
  
   inStream.close();
   outStream.close();
}

int main(){
  
   //create the i/o class objects
   ifstream inStream;
   ofstream outStream;
  
   //call the method convert
   convert(inStream, outStream);
}

Explanation / Answer

Hi, you need to pass stream class object : "by reference".

Please find my corrected code:

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

void convert(ifstream &inStream, ofstream &outStream){
  
char ch;
string filename;
  
inStream.open("pre-html.txt");
  
if(inStream.fail()){
cout << "File failed to open";
exit(1);
}
  
  
  
cout<< endl << "Enter the name of the new file" << endl;
cin >> filename;
  
filename = filename + ".html";
  
outStream.open(filename.c_str());
  
//writes <PRE> to the new file
outStream << "<PRE>";
  
  
//reads the data from the file till the end
//converts the specified characters to new ones
//writes the data to a new file of type .html
while(inStream >> ch){
cout << ch;
  
if(ch == '<'){
outStream << "&lt";
}
  
else if(ch == '>'){
outStream << "&gt";
}
  
else(outStream << ch);
}
  
outStream << "</PRE>" << endl;
  
inStream.close();
outStream.close();
}
int main(){
  
//create the i/o class objects
ifstream inStream;
ofstream outStream;
  
//call the method convert
convert(inStream, outStream);
}

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