Encryption Matrix Problem

Hello all, I have been assigned to make a program that takes a user inputted message and encrypts it from a 2d array of characters. The program needs to take each character from the message and change it with it's corresponding location in the array.

This is what I have so far: https://gist.github.com/anonymous/606a121b088bd5faa638

I know I need for loops to increment through the array until it finds the matching character, but I'm not quite sure how to pass each character from the message.

Thanks all, any advice is appreciated!



Example output:
MENU
======
E: Encrypt a message
D: Decrypt a message
Q: Quit

Selection: e

Enter a message to encrypt:
Hello there!
4 1 0 4 1 3 1 3 1 6 7 6 2 3 0 7 0 4 2 1 0 4 8 2

MENU
======
E: Encrypt a message
D: Decrypt a message
Q: Quit

Selection: d

Enter the encrypted message numbers, with spaces between
Type "-1 -1" to end:
4 1 0 4 1 3 1 3 1 6 7 6 2 3 0 7 0 4 2 1 0 4 8 2 -1 -1
Hello there!
Last edited on
When you find the matching character, just output your row and column number with a space after each, then look for the next character.
Wow, okay so I just found out you can read strings like arrays. Do i need to use the counter from rows or columns for message[x]?

Edit: Ok so this is the loop I put together but it's only outputting the location of the first character, whats up?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	
int i = 0;

	for (int r = 0; r < NUM_ROWS; r++)
	{
		for (int c = 0; c < NUM_COLS; c++)
		{
			if (message[i] == encryptionMatrix[r][c])
			{
				cout << r << " " << c;
				i++;
			}
		}
	}
}


Last edited on
You need three nested loops - the outermost loop should iterate through each character of the string.
EDIT:

Alright sorry last one, the decryption is working fine, but for some reason when you finish the input it always adds a 'D' at the end.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void decryptMessage(char encryptionMatrix[][NUM_COLS])
{
	int row; //row location
	int col; //column location

	cout << "To decrypt, enter the correct sequence of numbers, each followed by a space.\n";
	cout << "Then once you are finished conclude with -1 -1\n";

	do
	{
		cin >> row >> col;

		cout << encryptionMatrix[row][col];

	} while (row != -1 && col != -1); //stops the loop once a negative 1 in row and column are entered
}
Last edited on
Topic archived. No new replies allowed.