As the title implies, I'm working on a playfair cipher program.
At the moment, I'm only trying to create the LxW 2D character array, using capital letters and the 0-9 digits and a user-inputted key, and get it to display. This being said...
I want to solve all of my logic flaws for myself. Please do not post things that have nothing to do with
I have no idea what is causing this problem, so I have included all code. The program does compile and whenever it breaks it points directly to the line in which the toupper function is written, but even after running once (giving me an incorrect cipher) and is closed, I end the program and re-run it without touching a single line of code, only for it to give me another exception.
All code is below, and the offending line is marked with a comment featuring *s.
Code:
#include <iostream>
#include "string"
using namespace std;
int main()
{
char cipher[6][6], key[36], input[100], choice = 'N', c = '0';
string result;
int row, col, a, b;
while (choice != 'Q')
{
if (choice == 'N') // If creating new cipher
{
// Clears key for new cipher creation
for (int b = 0; b < 36; b++)
key[b] = '\0';
cout << "Using A-Z and/or 0-9, please enter a key for the playfair cipher (Use no more than 36 characters, duplicate letters will be deleted): ";
cin >> key;
cout << endl << endl;
// Converts all letters to upper case
for (int c = 0; c < 36; ++c)
{
// If character is not junk, capitalize it.
if (key[c] != '\0')
key[c] = toupper(static_cast<unsigned char>(key[c])); // *****Problem is here*****
}
// Count involves Key length, co involves cipher col/row
int count = 0, co = 0;
// Create the cipher using Key
while (count < 36)
{
if (key[count] != NULL)
{
while (c != key[count])
{
// Loops to current slot to see if letter is already included
for (a = 0; a <= co; a++)
{
row = a / 6;
col = a % 6;
c = cipher[row][col];
}
// Inserts character
row = co / 6;
col = co % 6;
cipher[row][col] = key[count];
co++;
}
}
// Insert 0-9
for (char insert = '0'; insert <= '9'; insert++)
{
while (c != key[count])
{
// Loops to current slot to see if letter is already included
for (a = 0; a <= co; a++)
{
row = a / 6;
col = a % 6;
c = cipher[row][col];
}
// Inserts character
row = co / 6;
col = co % 6;
cipher[row][col] = key[count];
co++;
}
}
for (char insert = 'A'; insert <= 'Z'; insert++)
{
while (c != key[count])
{
// Loops to current slot to see if letter is already included
for (int a = 0; a <= co; a++)
{
row = a / 6;
col = a % 6;
c = cipher[row][col];
}
// Inserts character
row = co / 6;
col = co % 6;
cipher[row][col] = key[count];
co++;
}
}
count++;
}
}
for (a = 0; a < 6; a++)
{
for (b = 0; b < 6; b++)
cout << cipher[a][b] << " ";
cout << endl;
}
cout << "If you would like to create a new cipher, enter N." << endl;
cout << "If you would like to encode another sentence, enter E." << endl;
cout << "If you would like to quit, enter Q." << endl;
cout << "Please enter a character now: ";
cin >> choice;
choice = (toupper(choice));
while (choice != 'N' && choice != 'E' &&choice != 'Q')
{
cout << "ERROR: Invalid choice. Enter a valid character (N, E, or Q): ";
cin >> choice;
choice = (toupper(choice));
}
}
system("pause");
return 0;
}