Debug assertion failed

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.

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

// Decipher program by JM
#include <iostream>
#include <string>
using namespace 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.

Any ideas guys? its really doing my head in.
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.

Instead of a for loop try resize to add the '*' characters to star.
http://cplusplus.com/reference/string/string/resize/
Last edited on
Topic archived. No new replies allowed.