Encrypting message

Hello, I don't know why my code is not working, could anybody help? I am taking a message from the user and encrypting it. The shift offset is +5, and I have to increment +3 per letter position.

Here is what I have:
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
#include <iostream>
#include <string>
using namespace std;


int main()
{
   string encrypted;
   string decrypted;
 
   // Get input from the user
   string myArray = "";
   cout << "Enter the message you would like to encrypt: ";
   getline(cin, myArray);

   // Alphabet Array
   char arr[30] = { ' ', 'a','b','c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 
      'p', 'q' , 'r' , 's', 't', 'u', 'v', 'w', 'x', 'y' , 'z', '.', '?', ',' };
  

   // Encrypt
   for (int i = 0; i < myArray.length(); i++)
   {
      int index = -1;
      for (int j = 0; j < 30; j++)
      {
         if (myArray[i] == arr[j])
         {
            index = j; 
         }
         index = index + 5;
         index = index + (3 * i);
         index = index % 30; 
         encrypted[i] = arr[index];
      }
      cout << encrypted[i] << endl;
   }
Last edited on
The shift offset is +5, and I have to increment +3 per letter position

so if the input string was "XmasYoungZoo" what would be the expected output?
That would take me a while to do by hand. But basically the letters are all lowercase. So 'x' is in element 24 on the alphabet array in my code. Now to encrypt I have to add 5, which will be 29. Then increment +3 per letter means 29 + (3*0). So the answer I am left with is 29, now I look for that char in my code. 'm' -> 13 in my alphabet. Add 5 then it equals 18. Increment by 3 -> 18 + (3*1). 'a' -> 1 on my alphabet. Add 5 then it equals 6.
Increment -> 6 + (3*2)

Do you need me to keep going?
So basically I am adding three to every letter I input. For example if I have 6 letter and the last letter is 'n' then I look for it in my alphabet. Add 5. And whatever that gives me I have to add (3*6) to it.
closed account (48T7M4Gy)
line 31 should be index = j + 5;
Topic archived. No new replies allowed.