help with a function meant to create a 2D array

Hello all! This is what I'm trying to do.
Within a function constructMappingArray, I'm trying to create a 2D array 26 character column by n amount of input rows. Within this function, I use an array (keyArray) that I've read in as a constant to create the 2D array. keyArray, (key in main), is being rewritten within constructMappingArray instead of remaining the array I set as constant even though there is no explicit code that rewrites the array.

also within the constructMappingArray function, another variable I've read in (numberOfMappings) that is not explicitly altered in code is being changed from the user's input to the largest number my memory will hold. numberOfMappings is then used in a function that outputs the 2D array and because it's a large number, it pulls random characters to output until I run out of memory space and the program crashes.

#include <iostream>

const int MAX_NUM_MAPPINGS = 25;
const int LETTERS_IN_ALPHABET = 26;

void getMappingData (int &numberOfMappings, char key[]);
void constructMappingArray (char mappingArray[][LETTERS_IN_ALPHABET],
const char keyArray[], int numberOfMappings);

int main ();
{
int numberOfMappings = 0;
char key[MAX_NUM_MAPPINGS] = {' '};
char mappingArray[][LETTERS_IN_ALPHABET] = {' '};

getMappingData (numberOfMappings, key);
constructMappingArray (mappingArray, key, numberOfMappings);
return EXIT_SUCCESS;
}

In this function, I've collected the number of rows (mappings) and what the first character of each row should be (key). (I have also made sure the user only enters capitol letters and that they enter the correct amount of letters)

void getMappingData (int &numberOfMappings, char key[])
{
int validateNumKeys = 0;

cout << "Enter Number of Mappings (2-25): ";
cin >> numberOfMappings;
do
{
cout << "Enter the " << numberOfMappings << " keys: ";
cin >> key;
validateNumKeys = (static_cast<int>(strlen(key)));
} while (validateNumKeys != numberOfMappings);
for (int i = 0; i < validateNumKeys; i++)
{
while ((key[i] < 'A') || (key[i] > 'Z'))
{
do
{
cout << "Enter the " << numberOfMappings << " keys: ";
cin >> key;
validateNumKeys = (static_cast<int>(strlen(key)));
} while (validateNumKeys != numberOfMappings);
}
}
}

In main, I then call the constructMappingArray function and feed in the mapping array to be written, the key array as a **constant**(which I've been led to believe protects it from being changed in memory), and the numberOfMappings.

void constructMappingArray (char mappingArray[][LETTERS_IN_ALPHABET],
const char keyArray[], int numberOfMappings)
{
char letter = ' ';
for (int i = 0; i < numberOfMappings; i++)
{
for (int j = 0; j < LETTERS_IN_ALPHABET; j++)
{
if (j == 0)
{
mappingArray[i][j] = keyArray[i];
letter = mappingArray[i][j];
}
else if (letter < 'Z')
{
mappingArray[i][j] = letter + 1; //when I debug and i>0,this line of code changes the keyArray[i] to letter, rewriting the array.
letter = mappingArray[i][j];
}
else
{
mappingArray[i][j] = letter - (LETTERS_IN_ALPHABET - 1); // when I debug and i>0 this line of code also changes the keyArray[i] to letter, also rewriting the array.
letter = mappingArray[i][j];
}
}
}

Though I've figured out where it changes the keyArray, I don't know why or how as the code doesn't change it. I'm also unsure if my 2D array is being written correctly as when I debug, it shows that when i = 0, the mappingArray array is being created as coded but i after 0 doesn't show that the array is being created.

I also haven't found where the function changes the numberOfMappings integer which is vital to other functions that are meant to encode/decode the characters/a message that is read in via txt file. My professor was also unable to find a reason for the change of the array and the variable and any input would be appreciated.
Last edited on
Topic archived. No new replies allowed.