String subscript out of range

When i run this simple code, I get the error: String Subscript out of range

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
//Declarations

int main ()
{
string text;
char Key = 'x';
getline (cin,text);
	
ofstream File ("File.txt");
File << "The encrypted text is -  " << text;
File.close();

string file;
ifstream File1 ("File.txt");
	while (File1.good())
	{
		getline (File1,file);
	}
File1.close();

ofstream EnFile ("EncryptedFile.txt");
for (int x = 0; x < file.size();x++)
{
	file[x] ^= Key;
	EnFile << file[x];
}
EnFile.close();

string decrypt;
ifstream DeFile ("EncryptedFile.txt");
if (DeFile.is_open())
{
	while(!DeFile.eof()){
	getline (DeFile,decrypt);
	for (int x = 0; x < file.size();x++)
	{			
		decrypt[x] ^= Key;
		cout << decrypt[x];
	}}
}
else
	cout << "Unable to open file \n\n";

//Termiantion 



Can someone please help me with this problem??

EDIT: The Decryption part gives [output]The enc[/Output], and then the error occurs. And yes, I saw the previous post, but I haven't even come to classes yet, So I think this is better for basicrogramming.
Last edited on
File << "The encrypted text is - " << text; There's the error. You shouldn't have extra letters there.
Thanks! Now i works.
But is there any way to make it work for the string literals?
I thought that by putting it in a file, then, retrieving it into another string, and then doing the encryption to the single string would work!
Topic archived. No new replies allowed.