Help with encryption algorithm?

Im trying to write a code for a ceaser cipher, and it needs tp be done with arrays or related objects (vectors). Im currently having problems with the encryption process;
I created an array for the alphabet, and each corresponding element is matched to its appropriate lettr in the array with a for loop. After this, the element is reassigned based on a given shift amount. Problem is, it keeps writing the same letter for all elements of the original message, and they dont even correspond to the correct amount of shifts for any of them. Help/advice?
Below is the code Ive written/attempted to write.

if (input == 2){
cout << "Please choose the number of shifts you want: ";
int shift; cin >> shift;

for (int j=0; j<inputcount; j++){
int k =j-shift;
if (k<0){k=26+k; text[j]=codex[k];}
else {text[j]=codex[k];}
}
cout << "The encrypted message is: ";
for (int k=0; k<inputcount;k++){
cout<< text[k];
}

goto MENU;}
if I didn't know the length of the string coming in I guess I would use vectors, arrays imply I know the length of the incoming string for the output string. For example:

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
#include <iostream>
#include <vector>
using namespace std;

int main()
{
      string inComingLine;
      vector<char> encryptedLine;
      int shift;

      cout << "Enter the line to encrypt :" << endl;
      getline(cin, inComingLine);

      cout << "Enter the value of the character shift: " << endl;
      cin >> shift;    
      // for now I am assuming I get an integer.
      
      for(int nIndex = 0; nIndex < inComingLine.length(); nIndex++)
      {
             char nChar = inComingLine[nIndex];
             // preform shift....
             nChar = (char)( (int)nChar + shift ); 
             
             // I might want to check for bounds here....
             encryptedLine.push_back(nChar);
       }

       cout << "Line before: " << endl;
       cout << inComingLine << endl << endl;
     
       cout << "Encrypted Line: " << endl;
       vector<char>::iterator it;
       for(it = encryptedLine.begin(); it == endcryptedLine.end(); it++)
       {
             cout << *it;
       }
       cout << endl;

       return 0;
}


I guess if I wanted to know input counts I would just do lenghts of the incoming string. It almost appears your only wanting to encrypt actual letters and leave the rest alone.

I don't know if this answers what you had in mind but it how I would come close to approaching your problem.
Last edited on
Topic archived. No new replies allowed.