Function Help

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.

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
45
46
47
48
49
50
51
#include <iostream>
#include <cstring>
using namespace std;

void caesar_encrypt(char src[100], int key)
{
    char encrypted = ((src[100] + key) % 26);
    cout << "Result:" << endl;
    cout << encrypted << endl;
    return;
}

void caesar_decrypt(char src[100], int key)
{
    char decrypted = ((src[100] - key) % 26);
    cout << "Result:" << endl;
    cout << decrypted;
    return;
}

main ()
{
    char caesar[10];
    static const char encrypt[] = "encrypt";
    static const char decrypt[] = "decrypt";
    int key;
    char src[100];
    int result1;
    int result2;



    cout << "Enter operation: encrypt or decrypt" << endl;
    cin >> caesar;
    cout << "Enter key" << endl;
    cin >> key;
    cout << "Enter text to encrypt/decrypt" << endl;
    cin >> src;

    result1 = strcmp (caesar, encrypt);
    result2 = strcmp (caesar, decrypt);

    if(result1 == 0)
    {
        caesar_encrypt(src[100], key);
    }
    if(result2 == 0)
    {
        caesar_decrypt(src[100], key);
    }
}
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?
1
2
3
4
5
6
7
8
9
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.
Thanks for your help. I have made some changes but now I am not getting any output.

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <cstring>
using namespace std;

void caesar_encrypt(char src[100], int key, int dist)
{
    for (int i=0; i<dist; i++)
    {
        if (src[i] >= 'A' && src[i] <= 'Z')
        {
            src[i] = (char)(((src[i] + key - 65) % 26) + 65);
            cout << "Result:" << endl;
            cout << src[i] << endl;
        }
    }
}

void caesar_decrypt(char src[100], int key, int dist)
{
    for (int i=0; i<dist; i++)
    {
        if (src[i] >= 'A' && src[i] <= 'Z')
        {
            src[i] = (char)(((src[i] - key - 65) % 26) + 65);
            cout << "Result:" << endl;
            cout << src[i] << endl;
        }
    }
}

main ()
{
    char caesar[10];
    static const char encrypt[] = "encrypt";
    static const char decrypt[] = "decrypt";
    int key;
    char src[100];
    int result1;
    int result2;
    int dist = -1;



    cout << "Enter operation: encrypt or decrypt" << endl;
    cin >> caesar;
    cout << "Enter key" << endl;
    cin >> key;
    cout << "Enter text to encrypt/decrypt" << endl;
    cin >> src;

    result1 = strcmp (caesar, encrypt);
    result2 = strcmp (caesar, decrypt);

    if(result1 == 0)
    {
        caesar_encrypt(src, key, dist);
    }
    if(result2 == 0)
    {
        caesar_decrypt(src, key, dist);
    }
}
closed account (o3hC5Di1)
Hi there,

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.

Hope that helps.

All the best,
NwN
Last edited on
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

1
2
cout << "Result:" << endl;
cout << src[i] << endl;


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.
Thanks for the reply. I don't know how how to use std::cout statements. I thought
1
2
3
  src[i] = (char)(((src[i] - key - 65) % 26) + 65);
            cout << "Result:" << endl;
            cout << src[i] << endl;


would display the results back onto the screen.
closed account (o3hC5Di1)
Hi,

Sorry, I was mistaken - I've updated my previous post with some more information.

All the best,
NwN
I have narrowed it down to this part of the code
1
2
3
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?
closed account (o3hC5Di1)
Hi there,

1
2
//line 40
int dist = -1;


When will zero be smaller than -1 ? :)

All the best,
NwN
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.

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <cstring>
using namespace std;

void caesar_encrypt(char src[100], int key, int dist)
{
    for (int i=0; i < dist; i++)
    {
        if (src[i] >= 'A' && src[i] <= 'Z')
        {
            src[i] = (char)(((src[i] + key - 65) % 26) + 65);
        }
        else if (src[i] >= 'a' && src[i] <= 'z')
        {
            src[i] = (char)(((src[i] + key - 97) % 26) + 97);
        }
    }
}

void caesar_decrypt(char src[100], int key, int dist)
{
    for (int i=0; i < dist; i++)
    {
        if (src[i] >= 'A' && src[i] <= 'Z')
        {
            src[i] = (char)(((src[i] - key - 65) % 26) + 65);
        }
        else if (src[i] >= 'a' && src[i] <= 'z')
        {
            src[i] = (char)(((src[i] - key - 97) % 26) + 97);
        }
    }
}

main ()
{
    char caesar[10];
    static const char encrypt[] = "encrypt";
    static const char decrypt[] = "decrypt";
    int key;
    char src[100];
    int result1;
    int result2;
    int dist;



    cout << "Enter operation: encrypt or decrypt" << endl;
    cin >> caesar;
    cout << "Enter key" << endl;
    cin >> key;
    cout << "Enter text to encrypt/decrypt" << endl;
    cin >> src;

    dist = strlen (src);

    result1 = strcmp (caesar, encrypt);
    result2 = strcmp (caesar, decrypt);

    if(result1 == 0)
    {
        caesar_encrypt(src, key, dist);
    }
    if(result2 == 0)
    {
        caesar_decrypt(src, key, dist);
    }

    cout << "Result:" << endl;
    cout << src << endl;
}
Last edited on
closed account (o3hC5Di1)
except for the higher letters are not wrapping around back to a, b, c


Just to clarify, by higher letters you mean capital letters?

All the best,
NwN
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.
Last edited on
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.
Last edited on
Topic archived. No new replies allowed.