Encrypt Message via Array

Could anyone tell me why this code is placing arbitrary trash in certain inputs but not others?

For example, these inputs:
* * ** * * ** * ** *** ** ** **
**************************************************************************************
**********
hello world how are you doing?

Give these outputs:
*** * * **** ***** * **** ******* ~ ~ 
***************************************************************************************************Wa āˆš āˆš  
** ****** *** ā‰ˆ 
ard u rld eoD*?gowohello h iā”¬ā•Ø**Ƅ~& q q 


But these inputs:
hello world, how are you?
this is a test message!!


Give correct outputs:
hra ,worloe?uoy wd hello
stssees a a*!!egmtithis



I just don't understand why it is doing this at, what seems to be, random.

The object of the code is to take a message, put it in an array, and the "spin out" of the array clockwise (right, down, left, up, right, etc.) and print the message in scrambled order. If the message isn't big enough to fill the array, excess spaces are filled with the "*" symbol. The given array will not be larger that [33][33].


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 <string>
#include <cmath>

using namespace std;

int main()
{
    string message;
    int direction=0; //right = 0, down = 1, left = 2, up = 3
    int n, size, i = 0;
    char data[33][33];
    int x, y;
    int stepCount=1, numStep =0;


    getline(cin, message);
    n = message.size();

    size = sqrt(n);

    x = y = size/2 + 0.5;

    if(size*size < n){
        size++;
    }
    if(size%2 == 0){
        size++;
    }

    for(int r=0; r < size; r++){
        for(int c=0; c < size; c++){
            data[r][c] = message[i];
            if(!message[i]){
                data[r][c] = '*';
            }
            i++;
        }
    }

    for(int a=0; a<size*size; a++){
       cout << data[x][y];


       switch(direction){
            case 0: y += 1;
                    break;
            case 1: x+= 1;
                    break;
            case 2: y -= 1;
                    break;
            case 3: x -= 1;
                    break;
       }


       numStep++;
       if(numStep == stepCount){
        direction = (direction+1)%4;
        numStep = 0;
       }
       if(x == y){
        stepCount++;
       }


    }

    return 0;
}
Print out the coordinates for each letter in the cout section (add an end line so it prints letter, x, y for each line) and see if it goes out of bounds. It looks like it may be going outside the array.
Thank you! I don't know why I didn't think to test that first. As you guessed, I was overflowing because size = sqrt(n) was truncating the double. I knew it was truncating, but I didn't think it would be an issue until I was able to look at the overflow. Because it was truncating, my size on a few was too small to hold message.
experience. Many, many, many years of it. You will think of it next time :)
Topic archived. No new replies allowed.