Encryption program/char arrays

I'm not sure why I keep getting garbage values when I run the program. My prompt states:
"The program should read a line of text into a c-string (array of characters), separate the odd-numbered characters from the even-numbered characters, and produce a new encrypted text."

When I run and compile, it just gives my random numbers. Here's my code so far

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
  int main()
{
  char message[100];
  char evens[50];
  char odds[50];

  cout << "Enter the text to encode:" << endl;
  cin >> message;

  for(int i = 0; i < 100; i = i + 2)
    {
      for(int j = 0; j < 100; j++)
        {
          message[i] = odds[j];
        }
    }
  for(int x = 1; x < 100; x = x + 2)
    {
      for(int z = 0; z < 100; z++)
        {
          message[x] = evens[z];
        }
    }

  for(int a = 0; a < 100; a++)
    {
      cout << "Your message is: " << endl;
      cout << odds[a] << evens[a] << endl;
    }
  return 0;
}
You request input into your message array, then overwrite every other character in that message array with every character in the uninitialized odds array (one at a time, ending on the final 99th element), then do the same thing for the evens array for the other half.

In other words, every even character in message is overwritten with the last element in the odds array, and every odd character in message is overwritten with the last element in the evens array.

Once you've done that you output "Your Message Is: " followed by a character from both of your uninitialized arrays.

I don't think that's quite what you're going for.
1
2
 char evens[50];
  char odds[50];

These are what you output, and they are random values because you never set them to anything.
Topic archived. No new replies allowed.