Floating point exception: 8

when i run my program i get the following message:
Floating point exception: 8

this is part of my code:

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
string encrypt(string text){ // Encrypts message

string message;
	char c;
	for (int i = 0, j = 0; i < int(text.length()); i++){
    	c = text[i];
    if (c >= 'a' && c <= 'z')
        c += 'A' - 'a';
    else if (c < 'A' || c > 'Z')
    message += (c + message[j] - 2 * 'A') % 26 + 'A';
        j = (j + 1) % message.length();
    }
    return message;
}

string decrypt(string text){ // Decrypts message

string message;
    char c;
    for (int i = 0, j = 0; i < text.length(); i++){
    	c = text[i];
    if (c >= 'a' && c <= 'z')
    	c += 'A' - 'a';
    else if (c < 'A' || c > 'Z')
    message += (c - message[j] + 26) % 26 + 'A';
    j = (j + 1) % message.length();
    }
    return message;
}


I dont know what Im doing wrong within my functions
Proper indentation:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
string encrypt(string text)  // Encrypts message
{
    string message;
    char c;
    for (int i = 0, j = 0; i < int(text.length()); i++) {
        c = text[i];
        if (c >= 'a' && c <= 'z')
            c += 'A' - 'a';
        else if (c < 'A' || c > 'Z')
            message += (c + message[j] - 2 * 'A') % 26 + 'A';
        j = (j + 1) % message.length();
    }
    return message;
}
If first character of text is alphabetic, then in line 11 message.length() is 0. You cannot have % 0. this leads to error.
Same with decrype.
Topic archived. No new replies allowed.