In
C++
you have to use round brackets in method calls.
Quote:
while(in.good){
char ne = hash(in.get);
out << ne;
}
Should be instead
while(in.good()){
char ne = hash(in.get());
out << ne;
}
And (as suggested in other answers) there are other errors in you code. For instance streams must be passed by reference.
[update]
This should fix your code.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Crypt
{
virtual char hash(char c) = 0;
public:
void execute(ifstream & in, ofstream & out)
{
char c;
while ( in.get(c) )
{
out.put(hash(c));
}
}
};
class Encrypt : public Crypt
{
char hash(char c)
{
return (c+5);
}
};
class Decrypt : public Crypt
{
char hash(char c)
{
return (c-5);
}
};
class Engine
{
public :
Crypt & GetHandler(const string & k)
{
if (k == "encrypt")
{
static Encrypt E;
return E;
}
else
{
static Decrypt D;
return D;
}
}
};
int main()
{
cout << "Please Select a mode\n" << endl;
string preform;
cin >> preform;
cout << "Please Enter a File Name\n" << endl;
string infilename;
cin >> infilename;
ifstream infile(infilename, ios::binary);
string outfilename = "new";
outfilename.append(infilename);
ofstream outfile(outfilename, ios::binary);
Engine e;
e.GetHandler(preform).execute(infile, outfile);
}
However, being in inspired mood, you could also write
#include <iostream>
#include <fstream>
#include <iterator>
#include <algorithm>
#include <functional>
using namespace std;
int main ()
{
string mode, infilename;
cout << "choose mode\n";
cin >> mode;
cout << "choose file\n";
cin >> infilename;
ifstream infile(infilename, ios::binary);
infile >> noskipws;
ofstream outfile("new"+infilename, ios::binary);
istream_iterator<char> eos;
istream_iterator<char> iit (infile);
ostream_iterator<char> oit(outfile);
function <char(char)>hash = mode == "encrypt" ? [](char c){ return c+5;} : [] (char c){ return c-5;};
transform( iit, eos, oit, hash);
}
[/update]