Integer oveflow error

Mar 20, 2017 at 4:43pm
I simplified the code for just so it's easeir to understand. 'a' is an array, made up of 2 integers and 3 strings, n is of course an integer. The problem is that the result that's shown on screen is an overflown integer witha value of -863838, the usual stuff. I think i screwed up the addition syntax, because i'm uisng visual studio 2013, but used the 2015 version before that.

Also do not mind that the code is missing a few things, this is just a portion.

1
2
3
4
5
6
  n = 0;
for (i = 0; i < db; i++)
{
	n=n+a[i].n;
}
cout << n;
Last edited on Mar 20, 2017 at 4:44pm
Mar 20, 2017 at 4:54pm
Run this:
1
2
3
4
5
6
7
n = 0;
for (i = 0; i < db; i++)
{
    std::cout << a[i].n << std::endl;
    n=n+a[i].n;
}
cout << n;
What's the output?
Mar 20, 2017 at 4:59pm
'a' is an array, made up of 2 integers and 3 strings


Which one is it?
int a[2] or string a[3]


Mar 20, 2017 at 5:05pm

Which one is it?
int a[2] or string a[3]


sorry, not an array but a structure.
it looks like
1
2
3
4
5
struct e
{
int a, n;
string b, c, d;
}
Last edited on Mar 20, 2017 at 5:08pm
Mar 20, 2017 at 5:09pm
What's the output?


still the same integer overflow.
Mar 20, 2017 at 5:29pm
So it's not printing any numbers other than the final value of n? Then it's not entering the loop.
Mar 20, 2017 at 6:07pm
So it's not printing any numbers other than the final value of n? Then it's not entering the loop.


sorry for the late reply, i reply right away but c++.com didnt send it, it does show all components in the array.
Mar 20, 2017 at 6:16pm
So? What are those values? Anything that looks obviously wrong? What happens if you do the addition by hand?
Mar 20, 2017 at 6:28pm
the value i get varies but it usually looks something like this: -863838, and i don't think that adding positive integers usually end up to that much.
Mar 20, 2017 at 8:04pm
Solved it myself, the file that i was getting the values of 'a' from had an extra, empty row, so the overflow happened there first.
Topic archived. No new replies allowed.