Errounous output

The program I have listed below is for a very simple Encryption and Decryption. When it access's the file and encrypts it, it not only changes the file info, but prints weird characters. The file I am reading only contains 104 characters, but the output is printing 511. So the 1st 104 is good, and the rest is garbage. Any thought on how to cut out the garbage?

Input
Hi. This is a tet file. If this works, after you encrypt this file, you should not be able to read it.

Output

®Þœ†ÉÖÏèŽÏèŽÇ•âËéŽÌÞÚË£Ž†¾Ô†éÖÏèŽÝäàÑ蚆ÖÔÚÚà†îÝÛ•ÓÔØàßåâ†éÖÏèŽÌÞÚË¡Žßäã†èÖÕêÚÊ•ÜÕéŽÈÚŽÇ×ÚË•âÕ•àËÖÒ†Þâ”t:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2A:2v


I want to get rid of the :2A.....

Here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <iostream>										
#include <string>										//Written by: Mark S H Jr
#include <fstream>										//Last compiled on 11/21/2008
#include <iomanip>										//Written and compiled using: Microsoft Visual C++ 2008
#include <stdlib.h>

using namespace std;

int main()
{
	int opt;

	opt=0;

	cout << "Enter the option number you wish to execute: " << endl;
	cout << "1=Encryption" << endl;
	cout << "2=Decryption" << endl;
	cout << "3=EXIT" << endl;
	cin >> opt;

	switch (opt)
	{
	case 1:
		{
			char a[500], pw[20];  //inf[50], outf[50],  b[500],
			int z,i,j,x,y,e,xx;
	
			j=0;
			z=0;
	
	
//	cout << "Enter the input root directory and file name:  ";
//	cin >> inf;
//	cout << endl;

			ifstream infile;
//	infile.open (inf);
			infile.open("c://1st.txt");

			if (!infile)
			{	
				cout << "CANNOT OPEN THE FILE!!" << endl;
				return 1;
			}

//	cout << "Enter the output root directory and file name:  ";
//	cin >> outf;
//	cout << endl;

			ofstream outfile;
//	outfile.open(outf);
			outfile.open("c://2nd.txt");

			cout << "Enter the Password; a 3 letter combination: ";
			cin >> pw;

			while ((a[z++]=infile.get()) != EOF);
			{
				x=strlen(a);
				y=strlen(pw); 
	
				for (i=0; i<x; i++, j++)
				{
					if (j>=y) j=0;
					a[i]=a[i]+pw[j];
				}				
			}

			cout << endl << a;
			outfile << a;
			cout <<	endl << "Encryption is done" << endl;
		
			infile.close();
			outfile.close();
			break;
		}
	case 2:
		{
			char a[500], pw[20];  //inf[50], outf[50],  b[500],
			int z,i,j,x,y,e;
	
			j=0;
			z=0;
	
	
//	cout << "Enter the input root directory and file name:  ";
//	cin >> inf;
//	cout << endl;

			ifstream infile;
//	infile.open (inf);
			infile.open("c://2nd.txt");

			if (!infile)
			{	
				cout << "CANNOT OPEN THE FILE!!" << endl;
				return 1;
			}

//	cout << "Enter the output root directory and file name:  ";
//	cin >> outf;
//	cout << endl;

			ofstream outfile;
//	outfile.open(outf);
			outfile.open("c://3rd.txt");

			cout << "Enter the Password; a 3 letter combination: ";
			cin >> pw;

			while ((a[z++]=infile.get()) != EOF);
			{
				x=strlen(a);
				y=strlen(pw); 
	
				for (i=0; i<x; i++,j++)
				{
					if (j>=y) j=0;
					a[i]=a[i]-pw[j];
				}
			}

			cout << endl << a;
			outfile << a;
			cout <<	endl << "Decryption is done" << endl;
		
			infile.close();
			outfile.close();
			break;
		}
	case 3:
		break;
	}
	
	return 0;
}

At line 25 i suggest a string rather that a char array
Topic archived. No new replies allowed.