코드는 다음과 같습니다. 해결책을 찾지 만 시도한 사항이 작동하지 않았습니다. 또한 파일 맨 아래에있는 if 문은 파일이 열렸는지 확인하지 못하고 실패하고 파일을 열 수 없다고 말하지만 그 오류로 무엇을해야할지 모르겠다.ofstream은 파일을 작성하지만 쓰지 않습니다.
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
const int row = 6;
const int col = 8;
void cellTemp (double check[][col], double);
int main()
{
ifstream in;
string input;
ofstream out;
string output;
double grid[row][col];
double t1, t2, t3, t4, tol;
cout << "Enter input file name: \n";
cin >> input;
cout << "Enter output file name: \n";
cin >> output;
in.open(input.c_str());
out.open(output.c_str());
in >> t1 >> t2 >> t3 >> t4 >> tol;
for(int i = 0; i < 6; i++)
{
for(int c = 0; c < 8; c++)
{
grid[i][c] = 0;
}
}
// Initializes top and bottom rows to 0
for(int i = 0; i < 8; i++)
{
grid[0][i] = t1;
grid[7][i] = t3;
}
// Initializes left and right columns to 0
for(int i = 1; i < 5; i++)
{
grid[i][0] = t4;
grid[i][7] = t2;
}
cellTemp(grid, tol);
if(out.is_open())
{
for(int i = 0; i < 6; i++)
{
for(int c = 0; c < 8; c++)
{
out << grid[i][c];
}
out << endl;
}
out.flush();
}
else
{
cout << "Cannot open file. \n";
}
out.close();
in.close();
}
void cellTemp (double check[][col], double tolerance){
double copy[row][col];
double max = 0;
double prev = 0;
double prevMax;
double prevTol = tolerance;
tolerance = -1;
while(prevMax > tolerance)
{
tolerance = prevTol;
// Copies the array before performing actions
for(int i = 0; i < 6; i++)
{
for(int c = 0; c < 8; c++)
{
copy[i][c] = check[i][c];
}
}
// Sets cell values
for(int i = 1; i < 5; i++)
{
for(int c = 1; c < 7; c++)
{
check[i][c] = (check[i-1][c] + check[i+1][c] +
check[i][c-1] + check[i][c+1])/4;
}
}
for(int i = 1; i < 5; i++)
{
for(int c = 1; c < 7; c++)
{
prev = check[i][c] - copy[i][c];
if(prev > max)
{
max = prev;
}
}
}
prevMax = max;
max = 0;
}
}
한계를 벗어났습니다 :'grid [7] [i] = t3;'. – Shibli
path와 함께 파일 이름을 하드 코딩하여 테스트하고 동일한 문제가 발생하는지 확인하십시오. – dmaelect
그것은 무엇입니까, 귀하의 제목이 말한대로 파일을 열거 나 만들거나 귀하의 질문에 말한대로 아닌가요? 출력 파일을 어디에 두려고합니까? –