So for some reason I keep getting a 'unhandled exception, would you like to break the code?' whenever i run this, like it thinks I'm going outside the array. Here's the whole code, and ill post the bit that it breaks at below:
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;
}
}
};
Source file:
#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");
}
And it breaks at:
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; }
as soon as i hit that last line, it gives me a error message. Any ideas?
You are iterating incorrectly in the function that creates your array:
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];
}
}
The loop should be over rows
, not columns
.
Also, in set:
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;
// ^^^
}
..
}
The i
there corresponds to a column index, but you're iterating up to rows
. So either that should be table[i][0] = TableValues
or the loop should iterate over columns
There are likely more problems than just this, but the immediate issue is that in your create_table() method, you're passing the array pointer by value. As a result, the pointer to the 2d array that the client passes to create_table() remains NULL, and causes the crash when set_mult_values() is called.
If I may editorialize briefly, I would highly recommend simply stepping through this kind of code using a debugger before asking the question. Doing so, you would have seen the obvious NULL pointer being passed to set_mult_values().
Secondarily, consider using STL types instead of raw arrays for this kind of thing. It will make your life approximately nine-hundred times easier.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.