My code can transfer word from input file to output file, but it misses the ';', 'return 0;' when I submit to test. I am not sure how to fix it. Can anyone give me some idea? This is the link for the error: http://ift.tt/2a3t1IE.
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <cctype>
#include <fstream>
#include <string>
using namespace std;
void print(ifstream& input,ofstream& output)
{ std::string command;
while(std::getline(input, command, ';'))
{ // loop until no more input to read or input fails to be read
if (command.find("cin")!= std::string::npos)
{ // found cin somewhere in command. This is too crude to work. See below
size_t pos = command.find("<<"); // look for the first <<
while (pos != std::string::npos)
{ // keep replacing and looking until end of string
command.replace(pos, 2, ">>"); // replace with >>
pos = command.find("<<", pos); // look for another
}
}
else if (command.find("cout")!= std::string::npos)
{ // same as above, but other way around
size_t pos = command.find(">>");
while (pos != std::string::npos)
{
command.replace(pos, 2, "<<");
pos = command.find(">>", pos);
}
}
output << command; // write string to output
cout << command << endl;
}
}
int main()
{
ifstream input;
ofstream output;
char filename[16];
cout << "Enter filename:" << endl;
cin >> filename;
input.open(filename);
if(input.fail()) {
cout << "Could not open the file " << endl;
exit(0);
}
output.open("corrected.txt");
if (output.fail()) {
cout << "Could not open the file " << filename << endl;
exit(1);
}
print(input, output);
input.close();
output.close();
return 0;
}
Aucun commentaire:
Enregistrer un commentaire