Reputation: 103
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".
Upvotes: 0
Views: 472
Reputation: 640
Also, with non dynamic allocate array, you can use sizeof() to know the array size :
const char tab[] = "hello world";
for (unsigned int i = 0; i < sizeof(tab); ++i) { //then you can easily iterate on each, including \0
std::cout << tab[i] << std::endl;
}
This only works on local array, sizeof(char*) will return the size of a pointer (4/8 bytes)
Upvotes: 0
Reputation: 25572
An array with the first five letters of the alphabet (a size of 5) can be visualized like so:
Index 0: a
Index 1: b
Index 2: c
Index 3: d
Index 4: e
As you can see, the index of the first value is actually zero. This can get confusing!
char MyCharacterArray[5]; // Initializes a one-dimensional character array with a size of five.
MyCharacterArray[0] = 'a'; // Sets the first value to a.
MyCharacterArray[4] = 'e'; // Sets the last value to b.
MyCharacterArray[5] = `f`; // This will generate errors, as the last index of MyCharacterArray is 4!
A basic loop to run through an array might look like this:
for (int Index = 0; Index <= 4; Index++)
{
// ...
}
This is where your code runs into trouble. Your while
loop uses the equivalent of this:
while (Index < 6)
Which is the same as this:
while (Index <= 5)
So on the loop's last runthrough, where Index
is 5, it tries to access a value that is outside the array's range.
Upvotes: 0
Reputation: 57902
cipherarray is [5] entries long, which means it can be indexed with any of (0, 1, 2, 3, 4) - passing in a 5 is out of bounds.
Modify your while (b < 6)
to read while (b < 5)
to keep within the array bounds (and similar in the other places where you check your indices).
Upvotes: 4