gpt4 book ai didi

c++ - 代码在 OS X 上运行,Codeblocks Vbox 中的段错误

转载 作者:行者123 更新时间:2023-11-30 03:52:03 28 4
gpt4 key购买 nike

所以当我在 OSX 中运行它时它工作正常,但我的类(class)要求它在使用代码块的 Xubuntu VMbox 中工作。当我尝试在 Codeblocks 或 VMbox 的终端中运行它时,我收到错误“段错误(核心已转储)”。这可能与需要导入不同的库有关吗?我想我以前有过这个问题。

#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;

int main(int argc, char const *argv[])
{
string filetoopen;
ifstream sudokutxtfile;
string txtline;
string sudokubox[9];
bool goodsudoku = true;
int i, j, row, column;

// Terminal input or default
if (argc == 2)
filetoopen = argv[1];
else
filetoopen = "sudokuboard.txt";
// Read in file, save to array, close file
sudokutxtfile.open(filetoopen);
while (getline(sudokutxtfile,txtline))
{
sudokubox[row] = txtline;
row++;
}
sudokutxtfile.close();
// Valid solution check
for (i = 0; i < 9; i++)
{
for (j = 0; j < 9; j++)
{
// check whether sudokubox[i][j] is valid at the i's row
for (column = 0; column < 9; column++)
if (column != j && sudokubox[i][column] == sudokubox[i][j])
goodsudoku = false;

// check whether sudokubox[i][j] is valid at the j's column
for (row = 0; row < 9; row++)
if (row != i && sudokubox[row][j] == sudokubox[i][j])
goodsudoku = false;

// check whether sudokubox[i][j] is valid in the 3-by-3 box
for (row = (i / 3) * 3; row < (i / 3) * 3 + 3; row++)
for (column = (j / 3) * 3; column < (j / 3) * 3 + 3; column++)
if (row != i && column != j && sudokubox[row][column] == sudokubox[i][j])
goodsudoku = false;
}
}
// Output
if (goodsudoku == true)
cout << "valid" << endl;
else
cout << "invalid" << endl;
return 0;
}

最佳答案

你声明这些变量

int i, j, row, column

然后你在这里做作业

while (getline(sudokutxtfile,txtline))
{
sudokubox[row] = txtline;
row++;
}

row 从未被初始化,因此无论您尝试写入 sudokubox[row] 的任何索引都可能超出范围(或负数,或绿色,或谁知道呢)

关于c++ - 代码在 OS X 上运行,Codeblocks Vbox 中的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30915955/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com