'Arithmetic operation resulted in an overflow with very small values
I have an exception saying that 'arithmetic operation resulted in an overflow' although I made a debugging and the values of my variables are small and don't even exceed 10! I really don't know what to do.
bool go = true;
int columns;
int rows;
columns = rows = 0;
int incrementer = 2;
while(go)
{
columns = incrementer;
if(cipherText.Length%columns!=0)
{
incrementer++;
continue;
}
rows = cipherText.Length / columns;
char[,] matrix = new char[rows, columns];
}
The error is in the last line. The debugging showed that the values of 'rows' and 'columns' are somthing like 3, 5 and so on. How do these values exceed the limit of data type 'int'?
Solution 1:[1]
You said it yourself:
The debugging showed that the values of 'rows' and 'columns' are somthing like 3, 5 and so on.
When checking your code, I see:
bool go = true;
...
while (go)
{
...
}
Nowhere in your loop you are saying go=false. So, your loop is starting with 3, 5, and so on, and on, and on, and on, and on, ... until the values get so large that an overflow is reached.
You need to implement a criteria that does something like:
while (go)
{
...
if (criteria)
go = false;
}
... and obviously your criteria needs to be realistic enough to stop your loop in time, before the overflow is reached.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dominique |
