0

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: ";
	cin.getline(outfile[100];
	
	ifstream file1(infile);
	ofstream file2;
	file2.open(outfile, ios::app);
	
	if(file1.is_open());
	{
		while(file1.good())
		{
			getline(file1, line);
			for(int i = 0; i < line.size(); i++)
			{
				encrypted += line[i] ^ (int(key)+i)
			}
			file2<<encrypted;
		}
	}
	file2.close()
	return 0;
}

Thank you.
:?:

3
Contributors
2
Replies
5
Views
5 Years
Discussion Span
Last Post by vmanes
0

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);
file2<<encrypted;

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.

0

The loop control is the big problem in the logic.

while(file1.good())

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.