I am creating an encoding program and when I instruct the program to create a 5X5 grid based on the alphabet while skipping over letters that match up to certain pre-defined variables(which are given values by user input during runtime). I have a loop that instructs the loop to keep running until the values that access the array are out of bounds, the loop seems to cause the problem. This code is standardized so there shouldn’t be much trouble compiling it in another compiler. Also would it be better to seperate my program into functions? here is the code:
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
#include<limits>
using namespace std;
int main(){
while (!cin.fail()) {
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 = 1;
int b = 1;
int c = 1;
int d = 1;
int e = 1;
string cipherarraytemplate[5][5]= {
{"a","b","c","d","e"},
{"f","g","h","i","j"},
{"k","l","m","n","o"},
{"p","r","s","t","u"},
{"v","w","x","y","z"}
};
string cipherarray[5][5]= {
{"a","b","c","d","e"},
{"f","g","h","i","j"},
{"k","l","m","n","o"},
{"p","r","s","t","u"},
{"v","w","x","y","z"}
};
cout<<"Enter the name of a file you want to create.\n";
cin>>filename;
ofstream outFile;
outFile.open(filename);
outFile<<fixed;
outFile.precision(2);
outFile.setf(ios_base::showpoint);
cin.ignore(std::numeric_limits<int>::max(),'\n');
cout<<"enter your codeword(codeword can have no repeating letters)\n";
cin>>key;
while (key[a] != '\0' ){
while(b < 6){
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++;
}
c++;
b = 1;
}
while (c < 6 || b < 6){
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 {
cipherarray[b][c] = cipherarraytemplate[d][e];
d++;
b++;
}
if (d == 6){
d = 1;
e++;
}
if (b == 6){
c++;
b = 1;
}
}
cout<<"now enter some text."<<endl<<"To end this program press Crtl-Z\n";
while(!cin.fail()){
cin.getline(type,81);
outFile<<type<<endl;
}
outFile.close();
}
}
I know there is going to be some mid-forties guy out there who is going to stumble on to this post, he’s have been programming for 20-some years and he’s going to look at my code and say: “what is this guy doing”.
An array with the first five letters of the alphabet (a size of 5) can be visualized like so:
As you can see, the index of the first value is actually zero. This can get confusing!
A basic loop to run through an array might look like this:
This is where your code runs into trouble. Your
whileloop uses the equivalent of this:Which is the same as this:
So on the loop’s last runthrough, where
Indexis 5, it tries to access a value that is outside the array’s range.