I have wrote this program in C++. But it doesnt have the output that it should. Instead of putting out +-++-+ it puts out ++-+++-+++++++-+++-+++--+++. That is just an example it will ofcourse have defirent output but it makes more characters then there is in the origanal string when i uncrypt the encrypted. If you could please post fix for this program I will apreciate it.

#include <iostream>
#include <fstream>
#include <string.h>

using namespace std;

int main()
	string line;
	char key = 'x'
	string encrypted;
	char infile[20];
	char outfile[20];
	cout<<"File for input: ";
	cin.getline(infile, 19);
	cout<<"File for output: ";
	ifstream file1(infile);
	ofstream file2;
	file2.open(outfile, ios::app);
			getline(file1, line);
			for(int i = 0; i < line.size(); i++)
				encrypted += line[i] ^ (int(key)+i)
	return 0;

Thank you.

Recommended Answers

All 2 Replies

Line 18 doesn't make any sense. Are you sure this program compiles and runs?

Line 22 -- why are you appending to the file instead of overwriting?

Lines 31 and 33.

encrypted += line[i] ^ (int(key)+i);

I imagine "encrypted" is going to end up having all sorts of unprintable characters, yet you're printing a string with the << operator.

Forget about the files. One step at a time. Have the user enter a string, then encrypt it and spit it out. Make sure THAT works first before messing with the files.

The loop control is the big problem in the logic.


won't stop the process till the file reading fails. If the file is ended properly (with a newline) the loop will process all lines, then will enter again, read the blank line, have nothing to process but output the encrypted line again. The .good( ) won't halt the process till you've attempted to read past end of file.

Better to use the success of the reading attempt to control the processing

while( getline(file1, line) )
   //do processing

Then go find all the semicolons in places they don't belong and delete them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.