-3

クロスワードを解くための簡単なプログラムを書いています。getline を使用して入力からクロスワードをロードしていますが、奇妙な「セグメンテーション違反」メッセージが表示され、どうすればよいかわかりません。コードは次のとおりです。

#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;


int** allocateCross(int rows, int columns)
{
    int ** cross = new int*[rows];
    for(int i=0;i<rows;i++)
    {
        cross[i] = new int[columns];    
    }
    return cross;
}
void changeValue(int **cross, int rowPosition, int columnPosition, int value)
{
    cross[rowPosition][columnPosition] = value;
}
void printCross(int ** cross, int rows, int columns)
{
    for(int i=0;i<rows;i++){
        for(int j=0;j<columns;j++){
            cout << cross[i][j];
        }
        cout << endl;
    }

}
void setCross(int ** cross, int rows,int columns, int spaces[], int numberOfSpaces)
{
        for(int i=0;i<6;i++){
        for(int j=0;j<6;j++){
            changeValue(cross, i, j, 0);

        }
        for(int i=0;i<(numberOfSpaces*2);i=i+2)
        {
            changeValue(cross, spaces[i], spaces[i+1],1);
        }

    }
}
int testString(string stringToTest, const string allowedChars)
{
    size_t found = stringToTest.find_first_not_of(allowedChars);
    if(found != string::npos)
        return false;
    else
        return true;
}

void wrongInput()
{
    cout << "Wrong input." << endl;
    exit(0);
}
int main()
{
    string line;
    string top;
    string line_r;
    bool stringTester;
    int columns = 0;
    int rows = 0;
    getline(cin,line);
    if(line!="Enter crossword:")
        wrongInput();
    getline(cin,line);
    stringTester = testString(line,"+-");
    if(stringTester != true)
        wrongInput();
    top = line;
    top.erase(top.begin());
    top.erase(top.begin()+(top.size()-1));
    rows = top.size();
    top = line;
    getline(cin,line);
    while(line != top)
    {
        columns++;
        getline(cin,line);
    }


    int ** cross = allocateCross(rows,columns);
    //int spaces[]={};
    setCross(cross,rows,columns,spaces,0);
    printCross(cross,rows,columns);


    return 0;
}

唯一の重要な部分は while サイクルです。この入力を入力すると:

Enter crossword:
+----+
|  * |
|    |
| *  |
+----+

スクリプトが停止する 2 番目の +----+ を入力するまで、すべて問題ありません。その後、セグメンテーション違反が発生します。誰か助けてくれませんか?

4

2 に答える 2

4

まず、書かれたコメントを読んでください。

問題はここにあるようです:

for(int i=0;i<6;i++){
    for(int j=0;j<6;j++){
        changeValue(cross, i, j, 0);
    }
}

この値 ( 6) がハードコーディングされているのはなぜですか? なぜ6ではないの42ですか?この例のクロスワードには 3 行 4 列 (3x4) がありますが、サイクルは 6x6 マトリックスを通過します。

于 2012-11-23T15:32:33.473 に答える
0

次のコードは間違っているようです。それが実際のクラッシュを引き起こすかどうかはわかりませんが、最初+に一番上の行の最初と最後から を削除し、その後、変更された一番上の行と同じ行が来るのを待ちます。

top.erase(top.begin());
top.erase(top.begin()+(top.size()-1));
....
while(line != top)

さらに、ブール値とは何か、およびそれらがどのように機能するかについて読んでみてください。関数は次のtestStringように記述する必要があります。

bool testString(string stringToTest, const string allowedChars)
{
    return stringToTest.find_first_not_of(allowedChars) != string::npos;
}

これらの 3 行:

stringTester = testString(line,"+-");
if(stringTester != true)
    wrongInput();

に簡略化できます

if(!testString(line,"+-"))
    wrongInput();
于 2012-11-23T15:36:13.547 に答える