그래서 어떤 이유로 '처리되지 않은 예외가 계속 발생합니다. 코드를 중단 하시겠습니까?' 내가 이걸 달릴 때마다, 내가 배열 바깥으로 나가는 것 같아.2 차원 배열에 쓸 때 처리되지 않은 예외가 발생했습니다.
Header file:
struct mult_div_values
{
int mult;
float div;
};
void create_table(mult_div_values ** table, int rows, int columns)
{
table = new mult_div_values * [rows];
for (int i = 0; i < columns; i++)
{
table[i] = new mult_div_values [columns];
}
}
void set_mult_values(mult_div_values ** table, int rows, int columns)
{
mult_div_values TableValues;
TableValues.div = 0;
for (int i = 0; i < rows; i++)
{
TableValues.mult = i+1;
table[0][i] = TableValues;
}
for (int i = 1; i < rows; i++)
for (int x = 0; x < columns; x++)
{
if (x == 0)
{
TableValues.mult = i + 1;
table[i][x] = TableValues;
}
else
{
TableValues.mult = (i+1) * (x + 1);
table[i][x] = TableValues;
}
}
};
void set_div_values(mult_div_values ** table, int rows, int columns)
{
mult_div_values TableValues;
for (float i = 0; i < rows; i++)
{
TableValues.div = i+1;
table[0][static_cast<int>(i)] = TableValues;
}
for (float i = 1; i < rows; i++)
for (float x = 0; x < columns; x++)
{
if (x == 0)
{
TableValues.div = i + 1;
table[static_cast<int>(i)][static_cast<int>(x)] = TableValues;
}
else
{
TableValues.div = (i+1)/(x + 1);
table[static_cast<int>(i)][static_cast<int>(x)] = TableValues;
}
}
};
소스 파일 : : 여기에서 아래 파괴하는 전체 코드, 아픈 포스트 비트의
#include <iostream>
#include "mult_div.h"
using namespace::std;
struct mult_div_values;
int main()
{
mult_div_values ** table = 0;
int rows, columns, rowswanted, columnswanted;
cout << "How many rows?\n";
cin >> rows;
cout << "How many columns?\n";
cin >> columns;
cout << "Which row do you want?\n";
cin >> rowswanted;
cout << "Which column?\n";
cin >> columnswanted;
create_table(table, rows, columns);
set_mult_values(table, rows, columns);
set_mult_values(table, rows, columns);
cout << "Mult value: " << table[rowswanted][columnswanted].mult << endl << "Div value: " << table[rowswanted][columnswanted].div;
system("Pause");
}
그리고에서 나누기 : 최대한 빨리 공격으로
void set_mult_values(mult_div_values ** table, int rows, int columns)
{
mult_div_values TableValues;
TableValues.div = 0;
for (int i = 0; i < rows; i++)
{
TableValues.mult = i+1;
table[0][i] = TableValues; }
그 마지막 줄, 그것은 나에게 오류 메시지를 준다. 어떤 아이디어?
'table' 포인터가 유효한지 확인하는 것이 좋습니다. 'rows'와'columns' 변수도 있습니다. –