So I am writing a code that encrypts and input by switching the letter to the one after it, using the formula ai = letter ( (position(ai )+ position(ai+1) ) mod k ) where k is the number of letters in the alphabet.
Now here is my code;
using namespace std;
class Encrypt {
private:
char letters[27];
char *String = new char[500];
char letters_cipher[25];
unsigned int i, k;
public:
Encrypt() :letters{ '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' } {
for (unsigned int i = 0; i < strlen(String); i++) {
String[i] = '\0';
}
}
~Encrypt() {
delete[] String;
}
void GetString() {
cout << "Enter String : ";
std::cin >> String;
}
void encrypt() {
for (i = 1; i <= 26; i++) { // Run 26 times (1 for each letter)
letters_cipher[i] = letters[(i + (i + 1)) % 26];
cout << letters_cipher[i] << endl;
}
for (i = 0; i <= (strlen(String) - 1); i++) { // individual characters of string x can be referenced by x[0] etc
for (k = 0; k <= 25; k++) {
if (String[i] == letters[k]) {
cout << letters_cipher[k];
}
}
}
}
void Print() {
for (unsigned int i = 0; i < strlen(letters_cipher); i++) {
cout << letters_cipher[i];
}
}
};
I get the following error
Exception thrown: read access violation. this->String was 0x128F112.
for the line:
if (String[i] == letters[k])
Any idea how I can fix this?
EDIT:
I have now made some edits on the code and it looks like the following;
class Encrypt {
private:
char letters[27];
char *String = new char[500];
char letters_cipher[27];
unsigned int i, k;
public:
Encrypt() :letters{ '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' } {
for (unsigned int i = 0; i < 500; i++) {
String[i] = '\0';
}
}
~Encrypt() {
delete[] String;
}
void GetString() {
cout << "Enter String : ";
std::cin >> String;
}
void encrypt() {
for (i = 1; i <= 26; i++) { // Run 26 times (1 for each letter)
letters_cipher[i] = letters[(i + (i + 1)) % 26];
cout << letters_cipher[i] << endl;
}
for (i = 0; i <= (sizeof(String) - 1); i++) { // individual characters of string x can be referenced by x[0] etc
for (k = 0; k <= 25; k++) {
if (String[i] == letters[k]) {
cout << letters_cipher[k];
}
}
}
}
void Print() {
for (unsigned int i = 0; i < sizeof(letters_cipher); i++) {
cout << letters_cipher[i];
}
}
};
The error is no longer there, the program runs but closes with an error;
'ConsoleApplication5.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel.appcore.dll'. Cannot find or open the PDB file. 'ConsoleApplication5.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcrt.dll'. Cannot find or open the PDB file. 'ConsoleApplication5.exe' (Win32): Loaded 'C:\Windows\SysWOW64\rpcrt4.dll'. Cannot find or open the PDB file. 'ConsoleApplication5.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sspicli.dll'. Cannot find or open the PDB file. 'ConsoleApplication5.exe' (Win32): Loaded 'C:\Windows\SysWOW64\cryptbase.dll'. Cannot find or open the PDB file. 'ConsoleApplication5.exe' (Win32): Loaded 'C:\Windows\SysWOW64\bcryptprimitives.dll'. Cannot find or open the PDB file. 'ConsoleApplication5.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sechost.dll'. Cannot find or open the PDB file. The program '[20764] ConsoleApplication5.exe' has exited with code 0 (0x0).