警告: 私は C++ の初心者であり、これはおそらく非常に基本的な問題です。一連の数字を含む .txt ファイルを読み込んで配列に入れ、配列をチェックして、それが真の魔方陣であるかどうかを確認しようとしています。最初のステップは完了しましたが、ネストされた for ループが、配列内で # を繰り返していると言い続けています。ロジックが間違っているのか、単に間違ったデータをチェックしているだけなのかわかりません。
...
bool flag=1;
int N;
string placeholder = " ";
{
int array[10][10];
ifstream inputFile;
inputFile.open("MAGIC.txt");
inputFile>>N;
for (int x=0;x<N;x++){
for (int y=0;y<N;y++){
inputFile >> array[x][y];
}
}
for (int x=0;x<N;x++){
for (int y=0;y<N;y++){
cout<<array[x][y]<<placeholder;
}
cout<<endl;
}
//Everything above works great.
//The following code changes "flag" to 0 every first loop
//I think it's checking the position instead of the value, but I don't know
for(int row=0;row<N;row++) {
for(int col=0;col<N;col++){
if(array[row]==array[col])
flag=0; break;
}
}
...
if(flag==1)
cout<<"Magic square"<<endl;
else
cout<<"No magic square"<<endl;
return 0;
}