encryption

closed account (4wpL6Up4)
Hi,

this is an encryption program.
It seems to run, as a matter of fact,
the command prompt opens and after allowing me to input a string,
I obtain the desired output.
However, the compiler blocks and a window attached to a red X
appears, saying

"Run-Time Check Failure #2 - Stack around the variable 'alph_cipher' was corrupted."

Anyone knowing why?
Thanks.


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
  #include "pch.h"

#include <iostream>
#include <vector>
#include <string>


using namespace std;
int main(int argc, char *argv[]) {

	char alphabet[] =
	{ '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' }; 
	int i, k; 
	char alph_cipher[25];

	for (i = 0; i <= 25; i++) { 

		k = (i + 3) % 26; 

		alph_cipher[i] = alphabet[k]; 
	}

	string str;
	cout << "enter a word" << endl;
	getline(cin, str);
	cout << "the encryption of the word is" << endl << endl;
	int string_length = str.size();

	for (i = 0; i <= (string_length - 1); i++)
	{ 
		for (k = 0; k <= 25; k++) 
		{
			if (str[i] == alphabet[k]) 
			{
				cout << alph_cipher[k];
			}
		}
	}

	return 0;
}
> char alph_cipher[25];
This has 25 elements.

> for (i = 0; i <= 25; i++) {
This loops 26 times.

> alph_cipher[i] = alphabet[k];
This is your "Run-Time Check Failure #2 - Stack around the variable 'alph_cipher' was corrupted."
Which happens on the last iteration of your loop.
Topic archived. No new replies allowed.