i
is not initialised before it’s read. That’s undefined behaviour. Didn’t your compiler warn you of this?
Simple fix: use for (int i = 0; /*etc*/;
More Related Contents:
- Lowercase letters that did not appear in the array, in order [closed]
- For loop with an array
- c++ skipping the if and for loops?
- Don’t know how to add loop to my program…?
- For every character in string
- my first c++ program runtime is super long like 5 – 10 minutes on a fast CPU it's a very basic c++ program [closed]
- How to deal with c-string pointers [closed]
- C++ Reading an unknown number of integers to cin and outputting the sum
- Write a for loop that prints from startNumber to lastNumber
- In .NET, which loop runs faster, ‘for’ or ‘foreach’?
- Forward declaration of nested types/classes in C++
- Declaring variables inside loops, good practice or bad practice?
- For vs. while in C programming?
- foreach vs someList.ForEach(){}
- Endless loop in C/C++ [closed]
- C++ for-loop – size_type vs. size_t
- Does a C++11 range-based for loop condition get evaluated every cycle?
- Iterate through a C++ Vector using a ‘for’ loop
- Pros and cons of using nested C++ classes and enumerations?
- Is the ranged based for loop beneficial to performance?
- Can I declare variables of different types in the initialization of a for loop? [duplicate]
- Is there ever a need for a “do {…} while ( )” loop?
- Reusing thread in loop c++
- Need iterator when using ranged-based for loops
- What does “for(;;)” mean?
- When implementing an infinite loop, is there a difference in using while(1) vs for(;;) vs goto (in C)?
- OpenMP: What is the benefit of nesting parallelizations?
- For loop prints an extra comma
- Add elements to a vector during range-based loop c++11
- Range based for loop in function which passes an array as value [duplicate]