How would I get my for loop to go back to my starting value on the ASCII table? Since the user can use any characters in the range of 33-122 on the ASCII table, how would I get my code to shift back to 33 if it surpasses 122?
Since the user can use any characters in the range of 33-122 on the ASCII table, how would I get my code to shift back to 33 if it surpasses 122?
Probably the easiest fix would be that the number you modulo should correspond to the range of inputs. So if 33-122 (inclusive?) is valid, that's a total of 122-33+1 values, so you should do mod (122-33+1) and then offset it by 33 after the mod.
for (int i = 0; i < n; i++)
{
int offset = 122;
int cipheredLetter = (((int)userPass[i] - offset + userKey) % 33) + offset;
cout << (char)cipheredLetter;
}
could be changed to..
1 2 3 4 5 6 7 8
for (int i = 0; i < n; i++)
{
int offset = 122;
int cipheredLetter = (((int)userPass[i] - offset + userKey) % 33) + offset;
if(cipheredLetter>122) // Added this line
cipheredLetter = 32+userKey; // And this one
cout << (char)cipheredLetter;
}
You could make line 43 to be cout << endl; so the ending text is on a separate line. Aesthetics, really.