### Array - exeption thrown, stack around variable was corrupted What the hell is with arrays, I thought I Am doing that correctly already, but apparently not. It is one of these things I can't wrap my head around...

 ``123456789101112131415161718`` ``````#include int main() { int arraya; int total{ 0 }; std::cin >> arraya; // calculate total for (int i = 0; i < 5; i++) { total += arraya[i]; } std::cout << total; }``````

I thought that when I declare array of  it will go from 0-4 indices. So when I create for loop and set condition i < 5. It will start from 0 run once, then increment 1, so after 5th run  index, it will get value 5, but won't run again because i must be lower than 5!

I checked this site and it is same here, I also tried i < 4 and i < 6 doesn't work either, but I Am pretty sure i < 5 is correct! What I Am doing wrong? Again don't understand why it doesn't work! > I thought that when I declare array of  it will go from 0-4 indices.
Yes that is correct.

So what are you doing here?
std::cin >> arraya;
You just lost the game.

The following for loop is off in the weeds even if it would otherwise be correct.

What it doesn't do, which the rest of the code would seem to imply, is input 5 numbers.
It just inputs one number, and that's outside the array.

If you want to input 5 numbers, use a loop for that as well.

Last edited on a couple of points:
1: total should be initialized like this:
int total = 0;
instead of with the {}
use the brackets when initializing values to an array, like this:
int arraya { 1, 2, 3, 4, 5 };

2: assuming you are trying to initialize the indices of the array from user input and then print the sum you can use code that looks like this:
#include <iostream>

 ``123456789`` ``````... // initialize aaraya and sum total from user input for (int i = 0; i < 5; i++) { std::cin >> arraya[i]; total += arraya[i]; } //print total std::cout << total;`````` @Spamfilter, there is nothing wrong with using the brackets {} to do initialisation. In fact, there are a lot of c++ programmers who prefer it. @Spamfilter.

1). No. The 'modern' way to initialise is to use {} or {a}. If {} is used than a default value for the type is used to initialise. Note that {} doesn't do a narrowing conversion whereas = does. But really this is a preference (unless you want an initialisation to the type default value).

Last edited on Spamfilter wrote:
1: total should be initialized like this:
int total = 0;
instead of with the {}

You might want to study up on C++11's uniform initialization:
https://mbevin.wordpress.com/2012/11/16/uniform-initialization/

It can be used in for loops:
 ``12131415`` `````` for (int i { }; i < 5; i++) { total += arraya[i]; }``````

It is still possible to go out of bounds with the operator[] indexing. Modern C++ has range-based for loops (AKA for-each loops). Works with C++ containers and regular arrays as long as the array hasn't decayed to a pointer (as happens when passing it to a function):
 ``1234`` `````` for (int i : arraya) { total += i; }``````

https://www.learncpp.com/cpp-tutorial/for-each-loops/ You can pass an array to a function without it decaying to a pointer.

Consider:

 ``1234567891011121314`` ``````#include #include template void fun1(const int (&a)[N]) { std::cout << std::size(a) << '\n'; } int main() { int b {}; fun1(b); }``````

 ``` 9 ```

Last edited on
Topic archived. No new replies allowed.