I am having trouble with transfering from the main function to the other to other two functions. I get errors on lines 49 and 53. The errors are the same for both lines which is "invalid conversion from 'char' to 'char*'" and "initializing argument 1 of 'void caesar_encrypt(char*, int)'" except for the other error says "void caesar_decrypt" instead.
The reason you are getting errors is because you are passing arrays into functions incorrectly. If you want to pass the entire array to the function you only need the name of it. So something like caesar_encrypt(src, key);
The way you are doing it only passes the 100th element (which in this case doesn't exist, as the array goes from 0 to 99).
You will also have some problems inside your functions. char encrypted = ((src[100] + key) % 26);
This is trying to access the 100th element again (which will cause problems) . However in this case using the entire array at 1 time wont help either (as you can't use % on it). You will probably want to use a for loop and go through each element in your array instead?
void caesar_encrypt(char *src, int key)
{
//Your code in here is wrong
}
void caesar_decrypt(char *src, int key)
{
//your code in here is wrong
}
When you pass an array to a function, you only pass in the address of the first element of that array. From there, you stick it in a for loop (or something) to go through all the elements of the array.
What you have done in your code is trying to access the 100th element which never existed.
Have a look at your dist variable.
It's value is set to a negative value, but you only display output when the counter, initialised at zero, is smaller than dist.
The answer is pretty glaring, but rather than give it to you, I will explain how you can find it yourself. Are you ever calling caesar_encrypt? How about caesar_decrypt? If so, how many times is each loop iterating? For each iteration, are any of the "if(...)" conditions true? You have a single piece of debugging code, in the form of
Put a lot more of these in, even if they just say something silly like cout << "Here1" << endl;. Once you finish writing a bunch of these, what output do you EXPECT to see? What output DO you see? when the output starts to differ from what you expect, you likely have the source of your problem. There is very little as important as good debugging skills.
void caesar_encrypt(char src[100], int key, char dist[100])
{
for (int i=0; i<dist; i++)
mainly the for loop. I don't think it is right. I am almost positive it is the i<dist that is the problem but it has me stumped. It has to do with the distance from 'A' I think. I am just having a problem trying to figure out how to but it into the code correctly. Any pointers?
Thanks for your help. I have pretty much figured everything out except for the higher letters are not wrapping around back to a, b, c and so on. They are giving special characters instead. Any pointers on how to fix this would be appreciated. Thanks.
Sorry for the confusion. I meant the higher letters in the alphabet like w-z both cap and lower case do not wrap around back to a, b, c. They display special characters instead. It looks like I just need to add a +1 somewhere in the code. As when I encrypt abc with a -1 key I get `ab.
edit. After some more testing I am only having problems when my key is a negative number. The text is not wrapping backwards for example a,b,c with a -1 key to z,a,b.
I suspect the problem here is that the modulus operator is returning a negative number (-1 % 26 == -1). I imagine there is a solution for this in the STL, but as a workaround you can write your own modulus wrapper:
1 2 3 4 5 6 7 8
int myModulus(int _dividend, int _divisor)
{
int result = _dividend % _divisor;
if (result < 0)
result += _divisor;
return result;
}
Also, for clarity, it's good to avoid magic numbers such as '65' and '97' - just replace them with 'A' and 'a' respectively.