Playfair Cipher

So im trying to create a 6 by 6 grid of a playfair cipher code. This is how it works...I enter a keyphrase and that keyphrase is put into a 6 by 6 grid ignoring all repeated letters and numbers. Once the keyphrase has been exhausted, numbers 0-9 are added to the grid if they do not already exist; then letters (if they do not already exist) are added if the grid is still not filled up. This assignment is past due but i still want to learn how to do this. Any ideas? I get a segmentation error when I run this. It could be because fr and fc go beyond 6 by 6. There may be another way of doing this...any idea is greatly appreciated. Thanks!

// Generate a 6 by 6 grid
void createGrid(string keyPhrase, char grid[6][6])
{
// add key
int GRID = 0;
int fr, fc;
bool first = true;
for (int r = 0; r < 6; r++)
{
for (int c = 0; c < 6; c++)
{
grid[r][c] = keyPhrase[GRID];
GRID++;
if(first)
{
fr = r;
fc = c;
first = false;
}
grid[r][c] = '.';
}
}

bool added = false;
// Add extra numbers, then letters if they do not already exist in the phrase
// using ASCII 48 is 0 and 57 is 9
for(int i = 48; i < 58; i++) // pick a number
{
for (int r = 0; r < 6; r++)
{
for (int c = 0; c < 6; c++)
{
if((char) i == grid[r][c])
{
added = true;
break;
}
}
if(!added)
{
grid[fr][fc] = (char) i;
fc++;
if (fc == 6)
{
fr++;
}
}
added = false;
}
}

for(int i = 65; i < 91; i++) // pick a letter
{
for (int r = 0; r < 6; r++)
{
for (int c = 0; c < 6; c++)
{
if((char) i == grid[r][c])
{
added = true;
}
}
if(!added)
{
grid[fr][fc] = (char) i;
fc++;
if (fc == 6)
{
fr++;
}
}
added = false;
}
}

for (int r = 0; r < 6; r++)
{
for (int c = 0; c < 6; c++)
{
cout << grid[r][c];
}
}
return;
}
the problem is this:
1
2
3
4
5
fc++;
if (fc == 6)
{
fr++; // fr is increased, but fc remains 6 -> next iteration = out of bounds
}

so add a line fc = 0;
Topic archived. No new replies allowed.