So im running into problems when trying to execute the encrypt function of my program.
The error im getting is Unhandled exception at 0x00bb185e in Assignment 2.exe: 0xC0000005: Access violation reading location 0x00000001.
what i think the problem is, is that the function can't access the cypherbet array for some reason.
I tried to make it static thinking that it was because the array got thrown away when the function ended aswell as making it dynamic but neither of these fixed it.
im also getting CXX0030: Error: expression cannot be evaluated and bad ptr errors in the debugger when i break and check the locals.
so im thinking there may be something wrong with how im creating the array or something.
i've included my entire program incase i messed up somewhere earlier and thats messing this part up.
but the array is made in the makeCypher function then passed to the encrypt function.
oh the all the arrays work previously as they all print info out at some point its just in the encrypt function they mess up.
#include <iostream>
usingnamespace std;
int getLength(char* formattedArray)
{
int arraySize =0;
for(int f = 0; f < formattedArray[f] && formattedArray[f] != '/0'; f++)
{
arraySize++;
}
cout << arraySize;
return arraySize;
}
void makeCypher(int shiftVal)
{
char alphabet[27] = "abcdefghijklmnopqrstuvwxyz";
staticchar *cypherBet = NULL;
cypherBet = newchar[27];
int loopNum;
cout << "The plain text alphabet is: \n";
for(int g = 0; g < 26; g++)
{
cout << alphabet[g];
}
cout << endl;
cout << "This is the Cypertext alphabet: \n";
for (int h=0; alphabet[h]; h++)
{
if((alphabet[h] +shiftVal) > 'z')
{
loopNum = ((alphabet[h]+shiftVal)-'z');
cypherBet[h] = alphabet[loopNum-1];
}
else
{
cypherBet[h] = alphabet[h] + shiftVal;
}
cout << cypherBet[h];
}
cout<<endl;
}
void encrypt(char *formattedArray, char *cypherBet,int groupCount)
{
for (int i=0; i < formattedArray[i] && formattedArray[i] !='/0'; i++)
{
formattedArray[i] = formattedArray[i]+(cypherBet[i]-formattedArray[i]);
cout << formattedArray[i];
}
}
int main (char *cypherBet)
{
constint maxSize = 256;
//char input [maxSize];
char *input =NULL;
int d = 0;
int shiftVal, arraySize,groupCount;
//char formattedArray[maxSize];
char *formattedArray = NULL;
input = newchar[maxSize];
formattedArray = newchar[maxSize];
//Takes the input from the user and stores it into the input array
cout << "Please enter your text to be encrypted \n";
cin.getline(input, maxSize);
//loops through input array and stops at a null terminator
for(int b = 0; b < input[b] && input[b] !='/0'; b++)
{
//this checks if the current char is upper case and
//converts to lowercase if it is
if((input[b] >= 'A' ) && (input[b] <= 'Z'))
{
input[b] = input[b] +32;
}
//this checks if the current char is a space and
//adds it to a new array if it isn't
if(input[b] != 32 && input[b] != '/0')
{
formattedArray[d] = input[b];
d++;
}
}
cout << "Here is your input as plain text: \n";
for(int e= 0;e < formattedArray[e] && formattedArray[e] != '/0'; e++)
{
cout<<formattedArray[e];
}
cout << endl;
cout << "Here is the length of your array: \n";
arraySize = getLength(formattedArray);
cout << endl;
cout << "Please enter your shift value \n";
cin >> shiftVal;
makeCypher(shiftVal);
cout << "Please enter your group count \n";
cin >> groupCount;
encrypt(formattedArray, cypherBet, groupCount);
//formattedArray = encrypt(formattedArray, cypherBet, groupCount);
cin.ignore(2);
}
Line 56: You can't pass cypherbet as an argument to main. Arguments to main are always int main (int argc, constchar* argv[]); where argc is the number of command line arguments and argv list an array of pointers to those command line arguments.
Line 19/44: You don't return the pointer to cypherbet to the caller. This also creates a memory leak because you don't delete cypherbet anywhere.
yeh the passing to main was because i was trying to get cypherbet to be able to be viewed by main so i could call the encrypt function from it, as it comes up as cypherbet is undefined otherwise.
yeh i havn't done the return functions yet as i don't fully understand how to return an entire array yet but the function needed to return something.