循环崩溃程序与2D数组有关

时间:2011-01-15 23:39:01

标签: c++ arrays loops crash

我正在创建一个编码程序,当我指示程序基于字母表创建一个5X5网格,同时跳过与某些预定义变量匹配的字母(在运行时由用户输入给定值)。我有一个循环,指示循环继续运行,直到访问数组的值超出范围,循环似乎导致问题。此代码是标准化的,因此在另一个编译器中编译它应该没有太大的麻烦。将程序分成函数也会更好吗?这是代码:

#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&lt; 6 || b&lt; 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();
}
} 

我知道那里会有一些四十多岁的家伙会偶然发现这篇文章,他已经编程了20多年了,他会看看我的代码然后说:“什么是这家伙做“。

3 个答案:

答案 0 :(得分:4)

cipherarray是[5]条目长,这意味着它可以用(0,1,2,3,4)中的任何一个索引 - 传入5是超出界限。

修改您的while (b < 6)以阅读while (b < 5)以保持在数组边界内(与您检查索引的其他位置类似)。

答案 1 :(得分:0)

字母表中前五个字母(大小为5)的数组可以这样显示:

Index 0: a
Index 1: b
Index 2: c
Index 3: d
Index 4: e

如您所见,第一个值的索引实际上为零。这可能会让人感到困惑!

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!

运行数组的基本循环可能如下所示:

for (int Index = 0; Index <= 4; Index++)
{
    // ...
}

这是您的代码遇到麻烦的地方。您的while循环使用相当于:

while (Index < 6)

与此相同:

while (Index <= 5)

因此,在循环的最后一个起始位置,Index为5时,它会尝试访问数组范围之外的值。

答案 2 :(得分:0)

此外,对于非动态分配数组,您可以使用sizeof()来了解数组大小:

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; 
}

这只适用于本地数组,sizeof(char *)将返回指针的大小(4/8字节)