Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi, I am having trouble correcting this syntax in the last line where Engine (e) should execute on a file, the program is should encrypt and decrypt a file depending on (preform)
C++
e.GetHandler(preform)->execute(file, ofile);
there is something wrong in
C++
execute(*file*,*ofile*);


What I have tried:

Full Code:-
C++
#include<iostream>
#include<fstream>
#include<string>
using namespace std;

class Crypt {
private:
	virtual char hash(char c) = 0;
public:
	void execute(ifstream in, ofstream out) {
		while(in.good){
			char ne = hash(in.get);
			out << ne;
		}
	}
};

class Encrypt : public Crypt {
private:
	char hash(char c) {
		char a = c + 5;
		return a;
	}
};

class Decrypt : public Crypt {
	char hash(char c) {
		char a = c - 5;
		return a;
	}
};

class Engine {
public :
	Crypt* GetHandler(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;
	string filename;
	cin >> filename;
	cout << "Please Enter a File Name\n" << endl;
	getline(cin, filename);
	ifstream file(filename, ios::binary);
	string outfile = "new";
	filename.append(filename);
	ofstream outfile(outfile, ios::binary);
	Engine e;
	e.GetHandler(preform)->execute(file, ofile);
}
Posted
Updated 5-Jan-21 7:13am
v2
Comments
Patrice T 5-Jan-21 8:39am    
Exact error message please.

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
C++
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.
C++
#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
C++
#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]
 
Share this answer
 
v3
Comments
MohammedZr 5-Jan-21 13:14pm    
same problem
CPallini 5-Jan-21 16:28pm    
See my updated solution.
by guessing in your code I would expect that the function needs pointers and you have variables written.
C++
e.GetHandler(preform)->execute(&file, &ofile);
You need to understand the pointer syntax better. A pointer is only the address AND NOT the value. And vice versa ...
 
Share this answer
 
This won't even compile: ofile should be outfile.

I would also suggest that execute take its parameters by reference: ifstream& and ofstream&. It might work the way it is, but I don't know because I've never tried to pass them by value (which creates a copy).
 
Share this answer
 
v2
Comments
KarstenK 5-Jan-21 10:34am    
There are only disadvantages in creating copies, esp when complex objects.
MohammedZr 5-Jan-21 13:18pm    
it was a mistake writing 'ofile', Tried with outfile and alias or reference and the same problem.
there are no solutions that work for me any suggestions
 
Share this answer
 
Comments
jeron1 5-Jan-21 13:17pm    
Please just update your original post, as this is not a solution. Be as specific as possible when describing 'no solutions that work', show the full error text.
MohammedZr 5-Jan-21 13:20pm    
I'm truly sorry I should be more aware, I tried all the solutions and I'm still facing the same problem
jeron1 5-Jan-21 13:52pm    
Can you copy and paste the entire syntax error you are seeing, and the current code that has the latest changes, into your original post?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900