I have the user enter a keyword which is broken up and put into a five by five array, then another function called “getletter” which fills up the rest of the spaces in the array in alphabetical order excluding those letters already included in the keyword. When I try to pass the values of the keyword to the getletter function it doesn’t work.
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
#include<limits>
using namespace std;
string getletter();
char type[81];
char filename[20];
char key [5];
char f[2] = "q";
char g[2] = "q";
char h[2] = "q";
char i[2] = "q";
char j[2] = "q";
char k[2] = "q";
char l[2] = "q";
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
int main(){
string cipherarray[5][5]= {
{"a","f","k","p","v"},
{"b","g","l","r","w"},
{"c","h","m","s","x"},
{"d","i","n","t","y"},
{"e","j","o","u","z"}
};
cout<<"Enter the name of a file you want to create.\n";
cin>>filename;
cout<<"enter your codeword(codeword can have no repeating letters)\n";
cin>>key;
while (key[a] != '\0' ){
while(b <= 4){
cipherarray[b][c] = key[a];
if ( f == "q" ) {
cipherarray[b][c] = f;
}
if ( f != "q" && g == "q" )
{
cipherarray[b][c] = g;
}
if ( g != "q" && h == "q" )
{
cipherarray[b][c] = h;
}
if ( h != "q" && i == "q" )
{
cipherarray[b][c] = i;
}
if ( i != "q" && j == "q" )
{
cipherarray[b][c] = j;
}
if ( j != "q" && k == "q" )
{
cipherarray[b][c] = k;
}
if ( k != "q" && l == "q" )
{
cipherarray[b][c] = l;
}
a++;
b++;
if (key[a] == 0)
break;
}
if (key[a] != 0){
c++;
b = 0;
}
}
while ( c <= 4) {
while ( b <= 4) {
cipherarray[b][c] = getletter();
b++;
}
b = 0;
c++;
}
b = 0;
c = 0;
while ( c <= 4) {
while ( b <= 4) {
cout<<cipherarray[b][c]<<" ";
b++;
}
cout<<endl;
b = 0;
c++;
}
cout<<"now enter some text."<<endl<<"To end this program press Crtl-Z\n";
ofstream outFile;
outFile.open(filename);
outFile<<fixed;
outFile.precision(2);
outFile.setf(ios_base::showpoint);
cin.ignore(std::numeric_limits<int>::max(),'\n');
while(!cin.fail()){
cin.getline(type,81);
outFile<<type<<endl;
}
outFile.close();
}
string getletter() {
string letter;
string cipherarraytemplate[5][5]= {
{"a","f","k","p","v"},
{"b","g","l","r","w"},
{"c","h","m","s","x"},
{"d","i","n","t","y"},
{"e","j","o","u","z"}
};
if (cipherarraytemplate[d][e] == f || cipherarraytemplate[d][e] == g || cipherarraytemplate[d][e] == h || cipherarraytemplate[d][e] == i || cipherarraytemplate[d][e] == j ||
cipherarraytemplate[d][e] == k || cipherarraytemplate[d][e] == l){
d++;
}
else {
letter = cipherarraytemplate[d][e];
}
d++;
if (d == 5){
e++;
d = 0;
}
return letter;
}
I just set a variable that increases by one each time the loop runs through.