Each time i run my program i keep getting expression: string subscript out of range. I'm not sure why it keeps happeneing, the code looks fine to me, it compiles ok.
// Decipher program by JM
#include <iostream>
#include <string>
usingnamespace std;
int main ()
{
cout << "Please enter the ciphertext and press enter: \n";
string plainText;
string inputArrayy;
string star;
cin >> inputArrayy;
plainText = inputArrayy;
int n = 0;
int i = 0;
int z = 0;
int p = 0;
int arrayLength = inputArrayy.length();
for (p; p<arrayLength; p++){
star[p] = '*';
}
for (z; z<arrayLength; z++)
{
while (inputArrayy[n] == '*'){
if (inputArrayy == star){
cout << plainText;
}
else {
n++;
}
}
plainText[i] = inputArrayy[n];
inputArrayy[n] = '*';
n = n+4%(arrayLength);
i++;
}
return 0;
}
The program deciphers any inputted text using a transposition cipher of +4.
Take a look at line 27. Where did you increase the length of that string star? You cannot add new characters to a string using operator[]. You have to use a member function that inserts in that case.