Encryption based on a key

I'm trying to encrypt the text inside a file based on a 5 uppercase letter key entered by the user.

Here's what I have so far:

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
void encrypt(ifstream& in_s, ofstream& out_s)
{
	char key[4], c, k, m;
	int i = 0;

	cout << "Enter a 5 letter key: ";
	cin >> key;

	while(in_s.get(m))
	{
		for(i; i<5; i++)
			if(isupper(m))
			{
				c = (m + key[i]) % 90;
				out_s << c;
			}
			else if (islower(m))
			{
				c = (m + key[i]) % 122;
				out_s << c;
			}
			else
			{
				in_s.ignore();
				i--;
				out_s << " ";
			}

			if (i == 4)
			{
				i = 0;
			}
	}
}


The program never stops for some reason. Can someone please help me.
Lines 29-31: You reset your loop variable to 0. How do you ever expect to reach the termination condition on line 11 (i<5)?

Line 11: It's poor style to assume i was properly initialized. You should use the beginning condition (i=0) to assure that i is reset to 0 each time through the outer while loop. I'm assuming this is what you we trying to do (incorrectly) with lines 30-32.
Last edited on
Topic archived. No new replies allowed.