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 << "<";
}
else if(ch == '>'){
outStream << ">";
}
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 << "<";
}
else if(ch == '>'){
outStream << ">";
}
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);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.