Integer oveflow error

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
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?
'a' is an array, made up of 2 integers and 3 strings


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



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
What's the output?


still the same integer overflow.
So it's not printing any numbers other than the final value of n? Then it's not entering the loop.
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.
So? What are those values? Anything that looks obviously wrong? What happens if you do the addition by hand?
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.
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.