1)You don't have any constructors for your class? I think you should have a constructor that asks for the string and sets the key. That's what constructors are for--initializing data members.
2) What's the purpose of this line:
enc = line;
3) You have to check each character you are encrypting to see whether the 4 character shift has gone beyond the end of the alphabet. Where do you check for that?
Code:
for (int i = 0; i < length; i++) {
*enc += key;
enc++;
}
When you add 4 to a character, you are adding 4 to what's called its "ascii code". ascii codes are numerical synonyms for characters. A computer can only store numbers--it has no idea what a character is. So, there are numerical ascii codes assigned to each character so that you can store them on a computer. Try this:
Code:
char ch = 'a';
int myVar = ch;
cout<<myVar<<endl;
The number you see displayed is the ascii code for the character 'a'. The ascii codes for capital letters run from 65-88 and the ascii codes for the lower case letters run from 97-122. A char type actually stores an integer value, i.e an ascii code, but when you try to display a char variable, the integer is automatically converted to the corresponding character. If you assign a char variable to an integer variable, the integer value stored in the char variable is copied into the integer variable; and when you display an integer variable, no character conversion takes place. A char type tells the compiler that the integer stored in the variable is actually an ascii code and therefore it should be converted to a character when displayed.
The result is if you do this:
Code:
char ch = 97;
int n = 97;
cout<<ch<<endl;
cout<<n<<endl;
the character 'a' will be displayed in the first case, and the number 97 will be displayed in the second case.
In your program, when you add 4 to a character's ascii code and the result is a number higher than 122, you have gone past the end of the alphabet. The characters just past the end of the alphabet are '{', '}', |, ~, and delete. What you need to do is check each character to see if adding 4 to it yields a number higher than 122. If so, you need to wrap around. How can you check to see if a char is higher than 122? You can use an if statement like this:
if(line[i]>122)...
or like this:
if(line[i] > 'z')
If the code is higer than 122, you can use the remainder(or modulus) operator to find out how much greater than 122 like this:
int wrap = line[i]%122;
Then, you can add wrap to 65( = 'A') to get your encrypted char. However, you are going to run into problems. Once again, the letters of the alphabet have the ascii codes:
caps: 65-88
lowercase: 97-122
What if you add 4 to a capital 'Z'(= 88)? You will get an ascii code of 92, which is not a letter of the alphabet. Looking at an ascii table, 92 is the character '\'. So, you not only have to wrap, you have to "leap" over the ascii codes that aren't letters in the alphabet.
Of course, you don't have to use only letters of the alphabet, but if that is what you want to do, then Prelude posted a possible solution:
1) Set up a string or a cstring with all the letters you want to use for your encryption.
2) Get a letter from the string you want to encode.
3) Assign the letter to an int var to get it's ascii code(necessary because of step 5)
4) Add 4 to the ascii code.
5) Use the result in 4 as the index position for the letter you will obtain from the encryption string in 1), e.g.:
encryption_string[resultCode]
Ok, but you are still left with the problem of wrapping (but now you don't have to worry about leaping). That can be solved as discussed above with an if statement or the remainder operator. If you use the lower case letters of the alphabet as your encryption string, then the index positions of that string will run from 0-25 (there are 26 letters in the alphabet). Now, as a first pass you might think of using:
encryption_string[resultCode % 25]
but if the resultCode is 25, then resultCode%25 will be equal to 0, so you will have wrapped too early and you won't ever select index position 25, which is the 'z'. So, you want to wrap when the resultCode has gone one past the end of the encryption string or when resultCode is 26. That gives you:
encryption_string[resultCode % 26]
If resultCode is 25, then 25 % 26 is 25, and if resultCode is 26, then 26 % 26 is 0, so you will have wrapped back to index position 0. That means your possible index positions will run from 0-25 which are all the possible index positions in the encryption string.